2010年1月1日金曜日

struts2拦截器概述

struts2拦截器概述

Struts2的拦截器和Servlet过滤器类似。在执行Action的execute方法之前,Struts2会首先执行在struts.xml中引用的拦截器,在执行完所有引用的拦截器的intercept方法后,会执行Action的execute方法。

Struts2拦截器类必须从com.opensymphony.xwork2.interceptor.Interceptor接口继承,在Intercepter接口中有如下三个方法需要实现:
void destroy();
void init();
String intercept(ActionInvocation invocation) throws Exception;

其中 intercept方法是拦截器的核心方法,所有安装的拦截器都会调用这个方法。在Struts2中已经在struts-default.xml中预定义了一些自带的拦截器,如timer、params等。如果在标签中继承struts-default,则当前 package就会自动拥有struts-default.xml中的所有配置。代码如下:

...
在struts-default.xml中有一个默认的引用,在默认情况下(也就是中未引用拦截器时)会自动引用一些拦截器。这个默认的拦截器引用如下:




















dojo\..*



input,back,cancel,browse


input,back,cancel,browse



上面在defaultStack中引用的拦截器都可以在中不经过引用就可以使用(如果在中引用了任何拦截器后,要使用在defaultStack中定义的拦截器,也需要在中重新引用,在后面将详细讲解)。

下面我们来看几个简单的拦截器的使用方法。

一、记录拦截器和execute方法的执行时间(timer)
???? timer是Struts2中最简单的拦截器,这个拦截器对应的类是com.opensymphony.xwork2.interceptor.TimerInterceptor。它的功能是记录execute方法和其他拦截器(在timer后面定义的拦截器)的intercept方法执行的时间总和。如下面的配置代码所示:






由于在timer后面没有其他的拦截器定义,因此,timer只能记录execute方法的执行时间,在访问first动作时,会在控制台输出类似下面的一条信息:

信息: Executed action [/test/first!execute] took 16 ms.

在使用timer拦截器时,需要commons-logging.jar的支持。将logger引用放到timer的后面,就可以记录logger拦截器的intercept方法和Action的execute方法的执行时间总和,代码如下:

????





大家可以使用如下的Action类来测试一下timer拦截器:

package action;

import com.opensymphony.xwork2.ActionSupport;

public class FirstAction extends ActionSupport

{
public String execute() throws Exception

{
Thread.sleep(1000); // 延迟1秒
return null;
}

}

如果只记录execute方法的执行时间,一般会输出如下的信息:

信息: Executed action [/test/first!execute] took 1000 ms.

二、通过请求调用Action的setter方法(params)

当客户端的一个form向服务端提交请求时,如有一个textfield,代码如下:






在提交后,Struts2将会自动调用first动作类中的setName方法,并将name文本框中的值通过setName方法的参数传入。实际上,这个操作是由params拦截器完成的,params对应的类是com.opensymphony.xwork2.interceptor.ParametersInterceptor。由于params已经在defaultStack中定义,因此,在未引用拦截器的中是会自动引用params的,如下面的配置代码,在访问first动作时,Struts2是会自动执行相应的setter方法的。



... ...



但如果在中引用了其他的拦截器,就必须再次引用params拦截器,Struts2才能调用相应的setter方法。如下面的配置代码所示:






三、通过配置参数调用Action的setter方法(static-params)
static-params拦截器可以通过配置标签来调用Action类的相应的setter方法,static-params拦截器对应的类是com.opensymphony.xwork2.interceptor.StaticParametersInterceptor。
下面配置代码演示了如何使用static-params拦截器:




比尔




如果first动作使用上面的配置,在访问first动作时,Struts2会自动调用setWho方法将“比尔”作为参数值传入setWho方法。

四、使用拦截器栈
为了能在多个动作中方便地引用同一个或几个拦截器,可以使用拦截器栈将这些拦截器作为一个整体来引用。拦截器栈要在标签中使用和子标签来定义。代码如下:











比尔




可以象使用拦截器一样使用拦截器栈,如上面代码所示。



public class AuthorityInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1358600090729208361L;

//拦截Action处理的拦截方法
public String intercept(ActionInvocation invocation) throws Exception {
// 取得请求相关的ActionContext实例
ActionContext ctx=invocation.getInvocationContext();
Map session=ctx.getSession();
//取出名为user的session属性
String user=(String)session.get("user");
//如果没有登陆,或者登陆所有的用户名不是aumy,都返回重新登陆
if(user!=null && user.equals("aumy")){
return invocation.invoke();
}
//没有登陆,将服务器提示设置成一个HttpServletRequest属性
ctx.put("tip","您还没有登录,请登陆系统");
return Action.LOGIN;
}
}


"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">





/login.jsp
/error.jsp
/welcome.jsp


/qurey.jsp








class="com.aumy.struts.example.intercepter.AuthorityInterceptor"
name="authority"/>












/login.jsp


method="show">
/show.jsp


method="add">
/add.jsp



0 件のコメント: