使用tokenSession Interceptor的Struts 2在提交后无法获取表单值

时间:2012-09-06 06:13:59

标签: struts2 token

我有以下简单的登录代码和&安全登录。对于安全登录,我在表单提交时使用tokenSession拦截器。在success.jsp中,如果我提交secureLogin,则无法获取userId值,但登录时可以正常。任何人都可以帮我解释它背后的原因。

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
 <body><LOGIN.JSP>
  <s:form action="login" >
  <s:textfield name="userId" label="Login Id"/><br>
  <s:password name="password" label="Password"/><br>
     <s:submit value="Login" align="center"/>
 </s:form>
  <s:form action="secureLogin" >
  <s:textfield name="userId" label="Login Id"/><br>
  <s:password name="password" label="Password"/><br>
  <s:token />
     <s:submit value="secureLogin" align="center"/>
  </s:form>
 </body>
</html>



<struts>
  <package name="default" extends="struts-default">
     <interceptors>
       <interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
       </interceptors>
   <global-results>
       <result name="invalid.token">/error.jsp</result>
    </global-results>
   <action name="secureLogin" class="com.actions.HelloAction" method="secureLogin">
     <result name="success">/success.jsp</result>
     <result name="failure">/login.jsp</result>
     <interceptor-ref name="tokenSession" />
   </action>
   <action name="login" class="com.actions.HelloAction" method="login">
      <result name="success">/success.jsp</result>
      <result name="failure">/login.jsp</result>
   </action>
   </package>   
 </struts>


  package com.actions;

  public class HelloAction {
    private String userId;
    private String password;

    public String execute() {
    return "success";
    }

   public String login(){
    return "success";
   }

   public String secureLogin(){
    return "success";
   }

   public String getUserId() {
    return userId;
   }

   public void setUserId(String userId) {
    this.userId = userId;
   }

   public String getPassword() {
    return password;
   }

   public void setPassword(String password) {
    this.password = password;
   }

     }


  <%@ taglib prefix="s" uri="/struts-tags" %>

 <html>
 <head>
       <title>SUCCESS.JSP</title>
 </head>
  <body>

   Welcome <b><s:property value="userId"/></b> , you have loged  in. <br />
          </body>
 </html>

1 个答案:

答案 0 :(得分:0)

您需要interceptor-stack

<struts>
  <package name="default" extends="struts-default">
     <interceptors>
       <interceptor name="tokenSession" class = "org.apache.struts2.interceptor.TokenSessionStoreInterceptor" />
       <interceptor-stack name="yourStack">
          <interceptor-ref name="defaultStack"/>
          <interceptor-ref name="tokenSession"></interceptor-ref>
       </interceptor-stack>
       </interceptors>
       <default-interceptor-ref name="yourStack"/>
       <global-results>
           <result name="invalid.token">/error.jsp</result>
        </global-results>
   </package>   
 </struts>

这是一个链接:Interceptors