struts2.0拦截器的用法

 struts2.0拦截器的用法:

配置文件:

 <package name="default" extends="struts-default">

        <interceptors>
         <!-- 定义拦截器 loginInterceptor 继承default的package中action都可以引用该拦截器-->
            <interceptor name="loginInterceptor" class="com.cetc.util.CheckLoginInterceptor" />
            <!-- 定义拦截器堆栈 loginInterceptorStack -->
            <interceptor-stack name="loginInterceptorStack">
             <!-- 引用默认拦截器 defaultStack -->
                <interceptor-ref name="defaultStack" />
                <!-- 引用自定义拦截器 loginInterceptor -->
                <interceptor-ref name="loginInterceptor" />
            </interceptor-stack>
        </interceptors>
      
       
        <!-- 重定义默认拦截器堆栈 这里不要使用,若定义后凡是继承了default的package都回执行该拦截器-->
     <!--<default-interceptor-ref name="loginInterceptorStack"/>-->
     
        
    </package> 

 

 <package name="login" extends="default">
        <action name="login" class="com.cetc.struts.action.t00hy.LoginAction" method="login">
            <result name="input">/pages/home/login.jsp</result>
            <result name="to_zbr">/pages/t02qy/util/index_zbr.jsp</result>
            <result name="to_tbr">/pages/t02qy/util/index_tbr.jsp</result>
            <result name="to_zj">/pages/t01zj/util/index_zj.jsp</result>
            <result name="to_admin">/pages/home/index_admin.jsp</result>
            <!-- 引用拦截器 loginInterceptorStack -->
            <interceptor-ref name="loginInterceptorStack"></interceptor-ref>
        </action>
        <action name="qyLogin" class="com.cetc.struts.action.t00hy.LoginAction" method="qyLogin">
            <result name="input">/pages/home/login.jsp</result>
            <result name="to_zbr">/pages/t02qy/util/index_zbr.jsp</result>
            <result name="to_tbr">/pages/t02qy/util/index_tbr.jsp</result>
            <result name="to_zj">/pages/t01zj/util/index_zj.jsp</result>
            <result name="to_admin">/pages/home/index_admin.jsp</result>
            <!-- 引用拦截器 loginInterceptorStack -->
            <interceptor-ref name="loginInterceptorStack"></interceptor-ref>
        </action>
  <action name="logout" class="com.cetc.struts.action.t00hy.LoginAction" method="logout">
            <result name="success">/pages/home/login.jsp</result>
        </action>
  <action name="goLogin" class="com.cetc.struts.action.t00hy.LoginAction" method="goLogin">
            <result name="success">/pages/home/login.jsp</result>
        </action>
    </package>
   

 <package name="t00hy" namespace="/t00hy" extends="default">
  <action name="updateTWzHyzh" class="com.cetc.struts.action.t00hy.TWzHyzhAction" method="updateTWzHyzh">
   <result name="success" type="dispatcher">/pages/home/modifySuccess.jsp</result>
   <result name="input" type="dispatcher">/pages/home/modify_password.jsp</result>
            <!-- 引用拦截器 loginInterceptorStack -->
            <interceptor-ref name="loginInterceptorStack"></interceptor-ref>
  </action>
  <action name="goModifyPassword" class="com.cetc.struts.action.t00hy.TWzHyzhAction" method="goModifyPassword">
   <result name="success" type="dispatcher">/pages/home/modify_password.jsp</result>
            <!-- 引用拦截器 loginInterceptorStack -->
            <interceptor-ref name="loginInterceptorStack"></interceptor-ref>
  </action>

 </package>

 

抽象类:


import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.ActionInvocation;


public abstract class AbstractInterceptor implements Interceptor {

 public void destroy() {
  // TODO Auto-generated method stub
  System.out.println("LoginInterceptor.destroy()!");
 }

 public void init() {
  // TODO Auto-generated method stub
  System.out.println("LoginInterceptor.init()!");
 }

 public abstract String intercept(ActionInvocation invocation) throws Exception;
  // TODO Auto-generated method stub

}

自定义拦截器:


import java.util.Map;

import com.cetc.struts.action.t00hy.LoginAction;
import com.opensymphony.xwork2.ActionInvocation;

public class CheckLoginInterceptor extends AbstractInterceptor {

 private static final long serialVersionUID = -8624767071049356602L;
 public static final String LOGIN_KEY = "LOGINED";
    public static final String LOGIN_PAGE = "login";

    public String intercept(ActionInvocation actionInvocation) throws Exception {

        System.out.println("begin check login interceptor!");
        // 对LoginAction不做该项拦截
        Object action = actionInvocation.getAction();
        if (action instanceof LoginAction) {
            System.out.println("exit check login, because this is login action.");
            return actionInvocation.invoke();
        }

        // 确认Session中是否存在LOGIN
        Map session = actionInvocation.getInvocationContext().getSession();
        String login = (String) session.get(LOGIN_KEY);
        if (login != null && login.length() > 0) {
            // 存在的情况下进行后续操作。
            System.out.println("already login!");
            return actionInvocation.invoke();
        }else{
            // 否则终止后续操作,返回LOGIN
            System.out.println("no login, forward login page!");
            return LOGIN_PAGE;
        }
    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值