虽然我实现了ServletRequestAware

时间:2015-05-29 03:59:53

标签: java jsp parameters struts2 httprequest

我在JSP中有一个包含两个字段的表单,在操作类中,每个字段都有一个实例变量,但是当执行操作类时,这些属性为null。

我使用的validate()方法甚至没有执行。

JSP

<s:form action="addAuthority">
    <table>
        <caption> <b><big>Add New Authority</big></b>

        </caption>
        <s:if test="hasActionErrors()">
            <tr>
                <td><s:actionerror />
                </td>
            </tr>
        </s:if>
        <s:if test="hasActionMessages()">
            <tr>
                <td><s:actionmessage />
                </td>
            </tr>
        </s:if>
        <tr>
            <td></td>
            <td>
                <s:textfield name="role" label="Authority Name"></s:textfield </td>
                <td></td>
                <td>
                    <s:select name="dependentAuthority" list="#request.authorityList" label="Dependent Authority" listKey="roleId" listValue="role"></s:select>
                </td>
                <td>
                    <s:submit value="Add"></s:submit>
                </td>
        </tr>
    </table>
</s:form>

动作

public class AddAuthorityAction extends ActionSupport {
private String dependentAuthority;
private String role;
private Map<String, Object>  session;
private HttpServletRequest request;
public String execute() throws Exception{
    HttpServletRequest request = ServletActionContext.getRequest();
    //System.out.print(role + "  " + dependentAuthority+"  ");

    role = request.getParameter("role");
    dependentAuthority = request.getParameter("dependentAuthority");
    //System.out.print(role+"  "+ dependentAuthority);

    //insert the data
    int count = new DBInsert().addRoleDependency(role, Integer.parseInt(dependentAuthority));
    if(count==0){
        addActionError("There is some error while inserting. Please try again");
    }else{
        addActionMessage("Information successfully inserted");
    }
    return SUCCESS;
}
@SuppressWarnings("unchecked")
public String moveAddAuthority() {
    Map request = (Map) ActionContext.getContext().get("request");

    List<Role> authorityList = new DBSelect().getAuthorityId();
    request.put("authorityList", authorityList);

    List<Role> roleWithDependency = new DBSelect().getRoleWithDependence();
    request.put("roleWithDependency", roleWithDependency);
    return SUCCESS;
}

public void validate() {
    if (role == null || role.trim().equals("")) {
        addFieldError("role", "The name is required");
    }
}   
public String getDependentAuthority() {
    return dependentAuthority;
}
public void setDependentAuthority(String dependentAuthority) {
    this.dependentAuthority = dependentAuthority;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}}
  1. 当我使用HttpServletRequest request = ServletActionContext.getRequest();时,我可以获得值;
  2. 但通过实现ServletRequestAware请求变为null;
  3. 不使用两个实例变量都为null;
  4. 我无法在JSP页面中获取ActionMessage。
  5. struts.xml中

    <action name="addAuthority" class="nodue.action.AddAuthorityAction" 
          method="execute" >
            <interceptor-ref name="authonticateInterceptor"></interceptor-ref>
            <result name="success" type="redirect">moveAddAuthority</result>
        </action>
        <action name="moveAddAuthority" class="nodue.action.AddAuthorityAction" 
             method="moveAddAuthority">
            <interceptor-ref name="authonticateInterceptor"></interceptor-ref>
            <result name="success">/authority.jsp</result>
        </action>
    

    我对dependentAuthority的数据类型进行了一些修改,之前它是Integer,并在JSP页面中添加了<s:if>标记。

1 个答案:

答案 0 :(得分:0)

  1. 您可能正在使用单个拦截器,而you should use an entire stack则使用拦截器。

  2. 您将返回应用于访问外部网址或非操作网址的redirect结果。要重定向到操作you should use redirectAction结果。

  3. 无论是redirect还是redirectAction,当您重定向时,您都会丢失请求参数,这就是您不会看到操作错误和消息的原因。 There are different solutions to this

  4. 您使用validate()方法,当事情向南时返回INPUT结果。但是你还没有为你的行为定义任何INPUT结果; 请务必理解how the INPUT result works ,以及ValidationInterceptorParameterInterceptor