Struts2 拦截器电子书

来源:互联网 发布:土耳其 知乎 编辑:程序博客网 时间:2024/06/04 22:47

Struts2 拦截器 

拦截器:可以在Action执行之前和执行之后拦截调用。

Struts2将他的核心功能放到拦截器中实现而不是分散到Action中实现,有利于系统的解耦

,使得功能的实现类似于个人电脑的组装,变成可 插拔的。

拦截器的工作方式:围绕Action  和Result


在Action和Result执行之前,为Action配置的拦截器将首先被执行


Action和Result执行之后,拦截器将重新获得控制权,然后按照与先前 调用相反顺序依次z执行,在整个执行过程中,任何一个拦截器都可以选择直接返回,从而终止余下的拦截器,Action Result的执行。



编写拦截器类:

void init():

初始化拦截器资源  该方法只执行一次。

void destroy():

被销毁之前调用,用于释放在init()方法中分配的资源 ,该方法只执行一次。


String intercept(ActionInvocation invocation) throws Exception

该方法在Action执行之前被调用。拦截器为Action提供的附加功能在该方法中实现。

利用invocation参数,可以获取action执行的状态。

在intercept()方法中,如果要继续执行后续的部分(包括余下的应用于Action的拦截器,Action 和Result),可以调用invocation.invoke().如果要终止后续的执行,可以直接返回一个结果码,框架将根据这个结果码来呈现对应的结果视图。


[java] view plain copy
  1. package cn.happy.struts07.interceptor;  
  2.   
  3. import com.opensymphony.xwork2.ActionContext;  
  4. import com.opensymphony.xwork2.ActionInvocation;  
  5. import com.opensymphony.xwork2.interceptor.Interceptor;  
  6.   
  7. import java.util.Map;  
  8.   
  9. /** 
  10.  * Created by linlin on 2017/10/25. 
  11.  */  
  12. public class MyInterceptor implements Interceptor {  
  13.     public void destroy() {  
  14.   
  15.     }  
  16.   
  17.     public void init() {  
  18.         System.out.println("拦截器已经初始化成功了。。。。");  
  19.     }  
  20.   
  21.     public String intercept(ActionInvocation invocation) throws Exception {  
  22.         System.out.println("对象"+invocation);  
  23.    Object action=invocation.getAction();  
  24.         System.out.println("action===="+action);  
  25.    String value;  
  26.         Map<String,Object> session= ActionContext.getContext().getSession();  
  27.         Object name=session.get("uname");  
  28.         String actionName=invocation.getProxy().getActionName();  
  29.         invocation.getProxy().getNamespace();  
  30.         System.out.println("===actionName="+actionName);  
  31.         if(actionName.equals("login")){  
  32.   
  33.             System.out.println("======action login==");  
  34.             value=invocation.invoke();  
  35.         }else if(name!=null){  
  36.             value=invocation.invoke();  
  37.             String method=invocation.getProxy().getMethod();  
  38.             System.out.println("方法:"+method);  
  39.         }else{  
  40.   
  41.             value="login";  
  42.         }  
  43.         System.out.println("逻辑视图名"+value);  
  44.         return value;  
  45.     }  
  46. }  


为了简单拦截器用于输出Action执行花费的时间

在invocation.invoke()调用的前后,你可以添加自己的逻辑代码

invocation.invoke()调用之前的代码将在Action执行之前执行,invocation.invoke()调用

之后的代码将在Action执行之后执行。

为了简化 添加抽象类:

AbstractInterceptor

我们可以实现Interceptor接口,并给出了init()和destory()方法的空实现


我们编写的拦截器类也可以选择继承AbstractInterceptor类

如果不需要init()  destory()方法  name你只需重写抽象intercept()方法就可以。


Struts2 提供了一个特殊的拦截器抽象基类:

MethodFilterInterceptor

这个拦截器可以指定要拦截或排除的方法列表。

如果拦截所有方法 有时候对于某个方法会出现一些问题

struts2  中MethodFilterInterceptor  继承的拦截器类:

TokenInterceptor

TokenSessionStoreInterceptor

DefaultWorkflowInterceptor

ExecuteAndWaitInterceptor

ValidationInterceptor

ParametersInterceptor

PrepareInterceptor

MethodFilterInterceptor  通过指定included/excluded方法列表来选择拦截或排除的方法

设置参数:

excludeMethods-----要排除的方法、

includeMethods------要拦截的方法




在设置拦截或排除的方法是,如果有多个方法,那么以,分隔

如果一个方法的名字同时出现在excludeMethods和includeMEthid参数中,那么它会被当做兖兰街的方法,

也就是说 includeMethods优先于excludeMethods.

在编写拦截器类的时候要注意,对拦截器 必须是无状态的,换句话说,在拦截器类汇总不该有实例变量


Struts2  自带的拦截器、

Struts2本身提供了很多的拦截器  这里在struts.default.xml中定义

Web应用程序可以直接引用这些拦截器。

Strtus2提供的拦截器主要有下面这些:

alias

在请求之间转换名字不同的相似 参数。

chain

result type="chain"


checkbox

添加自动的复选框处理代码

当检测带复选框为复选时,将使用的默认值(false)

复选框添加到参数中,这个拦截器使用一个特殊的命名隐藏子墩来检测为提交的复选框

cookie

这个拦截器 基于cookie的名/值 对设置Action的属性

。。。。很多的



配置拦截器:


 <!--配置拦截器  拦截器栈--><interceptors>    <interceptor name="myInter" class="cn.happy.struts07.interceptor.MyInterceptor">    </interceptor>    <interceptor-stack name="myStack">        <interceptor-ref name="defaultStack"></interceptor-ref>   <interceptor-ref name="myInter"></interceptor-ref>    </interceptor-stack></interceptors><default-interceptor-ref name="myStack"></default-interceptor-ref>


PreResultListener接口

使用PreResultListener只有一个方法

void beforeResult(ActionInvocation invocation,String resultCode)

resultCode 是Action的返回码 这个方法将在action执行后,Result执行被调用

PreResultListener实例需要手工注册,在拦截器调用invocation.invoke()方法之前,

用invocation.addPreResultListener()方法注册PreResultListener实例



输出结果:



安全验证的拦截器


看上一篇博客的实例 


使用拦截器注解

在com.opensymphony.xwork.interceptor.annotations包中定义3个拦截器注解类型。

让你可以不用编写拦截类,直接通过注解的方式来指定action执行之前和之后需要调用的方法。

3个级别

Before

在Action主要方法之前,如果标注的方法有返回值,并且不为null,那么它的返回值将作为Action的结果代码。

After

在Action的主要方法以及result执行之后调用。如果标注的方法有返回值,那么这个返回值将被忽略。

BeforeResult

标注一个Action方法,该方法将在Action的主要方法调用之后,在result执行之前调用,如果标注的方法有返回值,那么这个返回值将被忽略。

这3个注解类型都有一个同名的参数,该参数的作用也是相同的。



要使用拦截器注解,还需要配置AnnotationWorkflowInterceptor拦截器。

这个拦截器在strtus.default.xml文件中并未定义,因此需要在自己的struts.xml文件中定义。



使用拦截器注解的缺点:

不利于代码的服用

执行效率差(通过反射机制来调用)

原创粉丝点击