【问题标题】:HttpServletRequest is null although I'm implementing ServletRequestAware尽管我正在实现 ServletRequestAware,但 HttpServletRequest 为空
【发布时间】:2015-05-29 16:46:00
【问题描述】:

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

我使用了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. 不使用两个实例变量为空;
  4. 我无法在 JSP 页面中获取 ActionMessage。

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>

我对@9​​87654326@的数据类型进行了一些修改,之前它是Integer,并在JSP页面中添加了&lt;s:if&gt;标签。

【问题讨论】:

  • 我希望 getter 和 setter 是公开的并且有正确的命名。您能否也发布相同的代码
  • 我已经添加了完整的 Action 类,并给出了相关的修改 JSP 标签。添加了另一个问题“无法在 JSP 页面中获取 ActionMessage”
  • 不要使用request来获取变量。可能问题出在你的拦截器堆栈中,显示出来。

标签: java jsp parameters struts2 httprequest


【解决方案1】:
  1. 您可能正在使用单个拦截器,而you should use an entire stack 中包含您的拦截器。

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

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

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

【讨论】:

  • @AleksandrM 我用过脊椎套?!好的,今天是星期五:D
  • 我有一个使用相同结构的登录页面。那么在其他情况下为什么不呢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-28
  • 2017-03-21
  • 2019-08-18
  • 2019-03-23
  • 2010-12-17
  • 2015-05-25
相关资源
最近更新 更多