0

我有以下用于登录和安全登录的简单代码。对于安全登录,我在提交表单时使用了 tokenSession 拦截器。在success.jsp 中,如果我提交secureLogin 但对Login 没问题,我将无法获得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>
4

1 回答 1

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>

这是一个链接:拦截器

于 2012-09-06T07:24:54.727 回答