Struts2 拦截器 电子书(02)

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().如果要终止后续的执行,可以直接返回一个结果码,框架将根据这个结果码来呈现对应的结果视图。


package cn.happy.struts07.interceptor;

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

import java.util.Map;

/**
 * Created by linlin on 2017/10/25.
 */
public class MyInterceptor implements Interceptor {
    public void destroy() {

    }

    public void init() {
        System.out.println("拦截器已经初始化成功了。。。。");
    }

    public String intercept(ActionInvocation invocation) throws Exception {
        System.out.println("对象"+invocation);
   Object action=invocation.getAction();
        System.out.println("action===="+action);
   String value;
        Map<String,Object> session= ActionContext.getContext().getSession();
        Object name=session.get("uname");
        String actionName=invocation.getProxy().getActionName();
        invocation.getProxy().getNamespace();
        System.out.println("===actionName="+actionName);
        if(actionName.equals("login")){

            System.out.println("======action login==");
            value=invocation.invoke();
        }else if(name!=null){
            value=invocation.invoke();
            String method=invocation.getProxy().getMethod();
            System.out.println("方法:"+method);
        }else{

            value="login";
        }
        System.out.println("逻辑视图名"+value);
        return value;
    }
}


为了简单拦截器用于输出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文件中定义。



使用拦截器注解的缺点:

不利于代码的服用

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值