您的位置:首页 > 编程语言 > Java开发

struts2 Intercept 实现原理

2011-03-29 13:21 323 查看
一、理解Struts2拦截器

1.
Struts2拦截器是在访问某个Action或Action的某个方法,字段之前或之后实施拦截,并且Struts2拦截器是可插拔的,拦截器是AOP的一种实现.

2. 拦截器栈(Interceptor
Stack)。Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。

二、实现Struts2拦截器原理

Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts
2会查找配置文件,并根据其配置实例化相对的
拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器

三、定义Struts2拦截器。

Struts2规定用户自定义拦截器必须实现com.opensymphony.xwork2.interceptor.Interceptor接口。该接口声明了3个方法:

void init();

void destroy();

String intercept(ActionInvocation invocation) throws Exception;


其中,init和destroy方法会在程序开始和结束时各执行一遍,不管使用了该拦截器与否,只要在struts.xml中声明了该Struts2拦截器就会被执行。

intercept方法就是拦截的主体了,每次拦截器生效时都会执行其中的逻辑。

不过,struts中又提供了几个抽象类来简化这一步骤,如下所示:

public abstract class AbstractInterceptor implements Interceptor;

public abstract class MethodFilterInterceptor extends AbstractInterceptor;

都是模板方法实现的。

其中AbstractInterceptor提供了init()和destroy()的空实现,使用时只需要覆盖intercept()方法;

而MethodFilterInterceptor则提供了includeMethods和excludeMethods两个属性,用来过滤执行该过滤器的action的方法。可以通过param来加入或者排除需要过滤的方法。

一般来说,拦截器的写法都差不多。看下面的示例:

package interceptor;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

public class MyInterceptor implements Interceptor {

public void destroy() {

// TODO Auto-generated method stub

}

public void init() {

// TODO Auto-generated method stub

}

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("Action执行前插入 代码");

//执行目标方法 (调用下一个拦截器, 或执行Action)

final String res = invocation.invoke();

System.out.println("Action执行后插入 代码");

return res;

}

}

四、配置Struts2拦截器

Struts2拦截器需要在struts.xml中声明,如下struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"

"http://struts.apache.org/dtds/struts-2.0.dtd
">

<struts>

<constant name="struts.objectFactory" value="spring" />

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

<interceptors>

<interceptor name="MyInterceptor" class="interceptor.MyInterceptor"></interceptor>

<interceptor-stack name="myInterceptorStack">

<interceptor-ref name="MyInterceptor"/>

<interceptor-ref name="defaultStack"/>

</interceptor-stack>

</interceptors>

<action name="loginAction" class="loginAction">

<result name="fail">/index.jsp </result>

<result name="success">/success.jsp</result>

<interceptor-ref name="myInterceptorStack"></interceptor-ref>

</action>

</package>

</struts>

拦截器



名字



说明



Alias Interceptor

alias

在不同请求之间将请求参数在不同名字件转换,请求内容不变

Chaining Interceptor

chain

让前一个
Action
的属性可以被后一个
Action
访问,现在和
chain
类型的
result


)结合使用。

Checkbox Interceptor

checkbox

添加了
checkbox
自动处理代码,将没有选中的
checkbox
的内容设定为
false
,而
html
默认情况下不提交没有选中的
checkbox



Cookies Interceptor

cookies

使用配置的
name,value
来是指
cookies

Conversion Error Interceptor

conversionError

将错误从
ActionContext
中添加到
Action

的属性字段中。

Create Session Interceptor

createSession

自动的创建
HttpSession
,用来为需要使用到
HttpSession

的拦截器服务。

Debugging Interceptor

debugging

提供不同的调试用的页面来展现内部的数据状况。

Execute and Wait Interceptor

execAndWait

在后台执行
Action

,同时将用户带到一个中间的等待页面。

Exception Interceptor

exception

将异常定位到一个画面

File Upload Interceptor

fileUpload

提供文件上传功能

I18n Interceptor

i18n

记录用户选择的
locale

Logger Interceptor

logger

输出
Action

的名字

Message Store Interceptor

store

存储或者访问实现
ValidationAware
接口的
Action

类出现的消息,错误,字段错误等。

Model Driven Interceptor

model-driven

如果一个类实现了
ModelDriven
,将
getModel
得到的结果放在
Value
Stack

中。

Scoped Model Driven

scoped-model-driven

如果一个
Action
实现了
ScopedModelDriven
,则这个拦截器会从相应的
Scope
中取出
model
调用
Action

setModel
方法将其放入
Action

内部。

Parameters Interceptor

params

将请求中的参数设置到
Action

中去。

Prepare Interceptor

prepare

如果
Acton
实现了
Preparable
,则该拦截器调用
Action
类的
prepare

方法。

Scope Interceptor

scope


Action
状态存入
session

application

的简单方法。

Servlet Config Interceptor

servletConfig

提供访问
HttpServletRequest

HttpServletResponse
的方法,以
Map

的方式访问。

Static Parameters Interceptor

staticParams


struts.xml
文件中将
中的
中的内容设置到对应的
Action

中。

Roles Interceptor

roles

确定用户是否具有
JAAS
指定的
Role

,否则不予执行。

Timer Interceptor

timer

输出
Action

执行的时间

Token Interceptor

token

通过
Token

来避免双击

Token Session Interceptor

tokenSession


Token
Interceptor
一样,不过双击的时候把请求的数据存储在
Session



Validation Interceptor

validation

使用
action-validation.xml

文件中定义的内容校验提交的数据。

Workflow Interceptor

workflow

调用
Action

validate
方法,一旦有错误返回,重新定位到
INPUT

画面

Parameter Filter Interceptor

N/A

从参数列表中删除不必要的参数

Profiling Interceptor

profiling

通过参数激活
profile

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: