我在struts。xml里加入了拦截器,如下:
         <interceptors>
        <interceptor name="auth" class="com.aa.common.interceptor.AuthorizationInterceptor" />
<interceptor-stack name="authStack">
<interceptor-ref name="auth" />
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors> <default-interceptor-ref name="authStack" />
<global-results>
<result name="login" type="redirect">/index.jsp</result>
</global-results>
我的类AuthorizationInterceptor如下:
public class AuthorizationInterceptor extends AbstractInterceptor { private static final long serialVersionUID = -1689526894183598040L; @Override
public String intercept(ActionInvocation ai) throws Exception {
Map session = ai.getInvocationContext().getSession();
SessionInfo sessionInfo = (SessionInfo) session.get("LoginInfoKey");
if (sessionInfo != null) {
return ai.invoke();
} else {
return Action.LOGIN;
}
}
}
我的目的就是如果用户没有登录,但他想访问系统里的页面,让他跳转到登录页,如果登录了,就继续执行。但是按这样配置了,连登录了也给拦截了(因为登录时,在session里肯定没有数据)。请大家给一个解决方案,谢谢。最好说的细点。

解决方案 »

  1.   

        <interceptors>
    <interceptor name="auth" class="cn.com.softwise.dailynet.interceptor.AuthorizationInterceptor"/>
    <interceptor-stack name="myStack">
    <interceptor-ref name="auth"/>
    <interceptor-ref name="defaultStack"/>
    </interceptor-stack>
    </interceptors>
    在struts.xml中 <include file=" struts-login.xml" />
    struts-login.xml 不加<default-interceptor-ref   name="authStack"   /> 这句.
    例如:
       <!-- ##########   ログイン画面    ############ -->
            <action name="l000000Action" class="cn.com.softwise.dailynet.action.login.L000000Action" method="init">
                  <result name="success_init">/login/L000000.jsp</result>
                  <result name="success" type="redirect">l001000Action.action</result>
                  <result name="input">/login/L000000.jsp</result>
            </action>       
            
            <!-- ##########   メインメニュー画面    ############ -->
            <action name="l001000Action" class="cn.com.softwise.dailynet.action.login.L001000Action">
            <interceptor-ref name ="myStack" />
                  <result name="login">/login/L000000.jsp</result>
                  <result>/login/L001000.jsp</result>
            </action>
      

  2.   

    public String intercept(ActionInvocation ai) throws Exception {
    // セッションを取得する。
    Map session = ai.getInvocationContext().getSession();
    Object role = session.get(CodeConstant.SESSION_CURRENT_USER);

    if (null != role) {
    // 登録して場合
    return ai.invoke();
    } else {
    // 登録しなっかた場合
    return BaseAction.ERROR_SESSION;
    }
    }在其他画面的struts-xxx.xml上加上<default-interceptor-ref   name="authStack"   /> 
    这句
      

  3.   

    你登陆的页面应该是知道的吧?你可以在这个拦截器里从request.getRequestURI(),获取当前的URI,如果恰巧是你的登陆的JSP页面,就跳过if else条件分支语句!
      

  4.   

    呵呵  你代码写错了
    你仔细看下
    if (sessionInfo != null) { 
    return ai.invoke(); 
    }  在你登陆前当然为空了所以得修改啊
    Object o = ai.getAction(); 获得Action的名称
    if (o instanceof LoginAction)  如果是登陆的Action那么跳过 让其登陆  {
         return ai.invoke();