Struts2之拦截器

Struts2的拦截器和Servlet的Filter过滤器及其相似,Struts2的拦截器只会处理action类,而servlet的过滤器可处理 servlet,jsp,html等等

拦截器可以说是Struts2的核心,大部分功能都是通过拦截器来实现的,只要我们的包继承了 struts-default

<package name="struts2" extends="struts-default">,就可以使用struts-defaul 里的拦截器

 

自定义拦截器的步骤

1) 编写拦截器类,需要实现 Interceptor接口,并实现该接口的三个方法:init ,intercept ,destroy

       或者继承 AbstractInterceptor抽象类 ,并实现 intercept 方法就够了

       或者继承 MethodFilterInterceptor抽象类,并实现 doIntercept方法

 

       分析Interceptor接口 AbstractInterceptor抽象类MethodFilterInterceptor抽象类,三者是存在关系的,

                  MethodFilterInterceptor抽象类继承  AbstractInterceptor抽象类 实现  Interceptor接口                 

2) 在struts.xml 文件中定义拦截器(action标签外)

3) 在struts.xml 文件中 的 action标签内引用 拦截器

 

如果使用了自定义的拦截器,则在拦截器的最后还要加个

默认拦截器 <interceptor-ref name="defaultStack"></interceptor-ref> ,当自定义拦截器执行了,则默认拦截器不会执行

如果在action内没加任何拦截器,则默认使用默认拦截器

 

举例:

1.1自定义拦截器的第一种方式:实现 Interceptor接口

 

package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; //定义一个拦截器1 public class Interceptor1 implements com.opensymphony.xwork2.interceptor.Interceptor { @Override public void destroy() { // TODO Auto-generated method stub } @Override public void init() { // TODO Auto-generated method stub } @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor1 before"); String str = invocation.invoke() ; System.out.println("Interceptor1 after"); return str; } }

 

1.2自定义拦截器的第二种方式:继承AbstractInterceptor抽象类

 

package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; //定义一个拦截器2 public class Interceptor2 extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor2 before"); String str = invocation.invoke() ; System.out.println("Interceptor2 after"); return str; } }
1.3自定义拦截器的第三种方式:继承MethodFilterInterceptor抽象类

 

 

package com.struts2.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor; //定义一个拦截器3,可以拦截自定义的方法,而其他拦截器只拦截 execute方法 public class Interceptor3 extends MethodFilterInterceptor { @Override protected String doIntercept(ActionInvocation invocation) throws Exception { System.out.println("Interceptor3 before"); String str = invocation.invoke() ; System.out.println("Interceptor3 after"); return str; } }
2.在struts.xml 文件中定义拦截器(action标签外)

 

 

<struts> <package name="struts2" extends="struts-default"> <!-- 定义拦截器 --> <interceptors> <interceptor name="interceptor1" class="com.struts2.interceptor.Interceptor1"> </interceptor> <interceptor name="interceptor2" class="com.struts2.interceptor.Interceptor2"> </interceptor> <interceptor name="interceptor3" class="com.struts2.interceptor.Interceptor3"></interceptor> </interceptors>

 

3.在struts.xml 文件中 的 action标签内引用 拦截器

 


假如用第三种方式定义拦截器的话,引用拦截器的时候还可以指定拦截的方法,excludeMethods 指定排除方法的拦截,includeMethods 把要拦截的方法包含进来,二者都可以排除多个方法,和包含多个方法,方法名用逗号‘ ’隔开。

 

就如上面引用拦截器的第三个来说,action 是执行自定义的myExecute()方法的,但 includeMethods包含是execute方法,所以interceptor3拦截器不会执行

如果没有指定拦截器的参数,默认会拦截全部方法

 

应用一(登录,判断用户是否登录):

1.定义一个拦截器类:

2.在struts.xml中定义 拦截器,和一个拦截器栈,注意 拦截器栈后也要加一个默认拦截器,

接着再把我们定义的拦截器栈,设为默认的拦截器,这样的作用是拦截全部action,很显然拦截全部action不是我们所要的,我们还需要一些action不被拦截,上面的拦截器已经处理了,就在 intercept方法内,加了

if(LoginAction.class == invocation.getAction().getClass() )
        {
            return invocation.invoke();
        }

就可以不对其进行 判断有没session

 

在struts.xml中定义 拦截器,和一个拦截器栈默认的拦截器

注意 默认拦截器 定义的位置,是在interceptor外的


3.定义一个全局的结果,在package标签下定义:

这个全局结果 <result name="login">/logingo.jsp</result> 会被 LoginInterceptor拦截器的  Action.LOGIN;  找到

 

4.具体处理登录的action类


5.登陆界面logingo.jsp



 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-04-27 16:25  spring学习笔记  阅读(4416)  评论(0编辑  收藏  举报