猿问

struts 不执行对应方法,请求无法到后台?

用的是ssh

主要是其中的save方法,其他方法可以正确执行。正常情况下点击提交,Action类的方法应该能被执行才对,我在save方法中向控制台打印了一句话,然而控制台并没有打印这句话,不清楚哪里错了?

下面是struts配置文件

<?xml version="1.0" encoding="UTF-8"?>  
<!DOCTYPE struts PUBLIC  
            "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"  
    "http://struts.apache.org/dtds/struts-2.5.dtd">  
<struts>  	    
    <!-- 开发模式下使用,可以打印出更详细的错误信息 -->  
    <constant name="struts.devMode" value="true"></constant>  
  
    <!-- 是否开启动态方法调用 -->  
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />  
    
    <package name="default" namespace="/" extends="struts-default">
    
        
        <!-- 定义新的拦截器栈,配置prepare拦截器栈的alwaysInvokePrepare 参数值为false -->
        <interceptors>
            <interceptor-stack name="sshStack" >
                <interceptor-ref name="paramsPrepareParamsStack">
                    <param name="prepare.alwaysInvokePrepare">false</param>
                </interceptor-ref>
            </interceptor-stack>             
        </interceptors>
        
        <!-- 定使用新的拦截器栈 -->
        <default-interceptor-ref name="sshStack"></default-interceptor-ref>
        
        <global-allowed-methods>remove</global-allowed-methods>
        
        <action name="emp-*" class="employeeAction" method="{1}">
            <result name="list">/WEB-INF/views/emp-list.jsp</result>
            <result type="stream" name="remove">
                <param name="contentType">text/html</param>
                <param name="inputName">inputStream</param>
            </result>
            <result name="input">/WEB-INF/views/emp-input.jsp</result>
            <result name="save" type="redirect">/emp-list</result>
            
        </action>
        
    </package>
     
</struts>

emp-input.jsp页面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

    <h4>Employee Input Page</h4>
    <s:form acton="emp-save" method="post">
        <s:textfield name="lastName" label="姓名"></s:textfield>
        <s:textfield name="email" label="邮箱"></s:textfield>
        <s:textfield name="birth" label="出生日期"></s:textfield>
        
        <s:select list="#request.departments" listkey="id" listValue="departmentName" name="department.id" label="部门">
        </s:select>
        
        <s:submit></s:submit>
    </s:form>
</body>
</html>

EmployeeAction类

public class EmployeeAction extends ActionSupport implements RequestAware,
ModelDriven<Employee>,Preparable{
    static{
        System.out.println("是否进入此类?-------------------------");
    }
    private static final long serialVersionUID = 1L;
    
    private EmployeeService employeeService;
    
    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }
    
    private DepartmentService departmentService;
    
    public void setDepartmentService(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }
    
    public String input() {
       request.put("departments", departmentService.getAll());
       System.out.println("====================input方法被执行==================="); 
       return INPUT;
    }
    
    public String list() {
        request.put("employees", employeeService.getAll());
        System.out.println("====================list方法被执行===================");
        return "list";
    }
    
    public String save(){
        System.out.println("====================save方法被执行===================");
        model.setCreateTime(new Date());
        employeeService.saveOrUpdate(model);
        System.out.println(model.toString());
        return "save";
    }
    
    /**
     * prepareble拦截器需要??
     */
    public void prepareSave() {
        model = new Employee();
    }
    
    private Integer id;
    
    public void setId(Integer id) {
        this.id = id;
    }
    
    private InputStream inputStream;
      
    public InputStream getInputStream() {
        return inputStream;
    }
    
    public String remove() {
        try {
            employeeService.remove(id);
            inputStream = new ByteArrayInputStream("1".getBytes("UTF-8"));
        } catch (Exception e) {
            try {
                inputStream = new ByteArrayInputStream("0".getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        return "remove";
    }
    
    private Map<String , Object> request;
    /* 
     * @see org.apache.struts2.interceptor.RequestAware#setRequest(java.util.Map)
     */
    @Override
    public void setRequest(Map<String, Object> arg0) {
        this.request = arg0;
    }
    
    private Employee model;
    
    @Override
    public Employee getModel() {
        return model;
    }

    @Override
    public void prepare() throws Exception {}
    
    
}


124839
浏览 1732回答 2
2回答

Find_The_Hard

Debug 走一下就知道了  哪出错了

慕粉4075985

要不试一下写成两个action
随时随地看视频慕课网APP

相关分类

Java
我要回答