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

Struts2学习笔记12:Struts2的拦截器【续】

2008-09-23 12:34 477 查看
Struts2学习笔记12:Struts2的拦截器【续】<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

第十一讲

内容很多,零碎的知识点也是比较多的

用到文件存放位置:

struts2-core-2.0.11.2.jar中

com.open.symphony.xwork.interceptor包中的Interceptor类
需要实现的方法:

void destroy();

void init();

String intercept(ActionInvocation invocation) throws Exception;
多个过滤器可以组成一个过滤器链

多个拦截器可以组成一个拦截器栈

拦截器与拦截器栈的功能是完全等价的
在一个package中只能有一个默认拦截器栈
在interceptor中设置param属性值为静态调用

在interceptor-ref中设置param属性值为动态调用(此优先级高)
在此讲中仅仅做一个例子来演示拦截器的应用,所以方法中仅打印一句话。

新建一个Java文件 interceptor.RegisterInterceptor 实现Interceptor接口,代码如下:

package interceptor;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.Interceptor;

public class RegisterInterceptor implements Interceptor {

private static final long serialVersionUID = -7376312407476270387L;

private String hello;

public String getHello() {

return hello;

}

public void setHello(String hello) {

this.hello = hello;

}

public void destroy() {

System.out.println("destroy");

}

public void init() {

System.out.println("init");

}

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("interceptor");

System.out.println(hello);

String result = invocation.invoke();

return result;

}

}

说明:解释下invocation.invoke();从API文档中拷贝过来的,英文:

Invokes the next step in processing this ActionInvocation.If there are more Interceptors, this will call the next one. If Interceptors choose not to short-circuit ActionInvocation processing and return their own return code, they will call invoke() to allow the next Interceptor to execute. If there are no more Interceptors to be applied, the Action is executed. If the ActionProxy getExecuteResult() method returns true, the Result is also 

executed.

配置拦截器

打开struts.xml文件

 1)在package标签中添加新元素,配置拦截器,用于拦截action

<interceptors>

<interceptor name="自定义" class="处理类(含包名)">

</interceptor>

</interceptors>

 2)在action标签中配置,习惯写于result元素之下。

<interceptor-ref name="自定义"></interceptor-ref>

这里的“自定义”对应于上面的“自定义”字符串

 3)运行tomcat,点击按钮,会看到如下网页:

 

 

 4)分析原因:

如果一个action手工加入了一个拦截器,则struts2不再将默认的拦截器附加到action

中,如果要附加,在interceptors元素中导入struts2默认的拦截器

关于struts2默认的拦截器

Struts2-core-2.0.11.2.jar/sturts-default.xml

打开该文件找到一行代码:

 <interceptor-stack name="defaultStack">

这里设置的是拦截器栈

通过下面代码将这个拦截器栈设置为默认拦截器栈:

 <default-interceptor-ref name="defaultStack"/>

要定义自己的默认拦截器栈

 1)在interceptors中添加interceptor-stack元素

     建议:在自定义的拦截器栈中加入struts2的默认拦截器栈

 2)在package中添加default-interceptor-ref元素,name属性值对应于上面

所定义的拦截器栈的值

对自定义struts.xml文件中package代码的解释

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

主要是针对 extends="struts-default" 代码

与Java中的继承有些类似,将继承struts-default.xml文件中的所有配置信息

在Interceptor和Interceptor-ref元素中有param属性,通过此属性可以设置

拦截器中的字段值

<param name="hello">world</param>

 

在RegisterInterceptor.java中添加私有字段hello,并设置其get和set方法。private String hello;

public String getHello() {

return hello;

}

public void setHello(String hello) {

this.hello = hello;

}

修改intercept方法为:

public String intercept(ActionInvocation invocation) throws Exception {

System.out.println("interceptor");

System.out.println(hello);  //添加的代码

String result = invocation.invoke();

return result;

}

运行tomcat后,点击按钮,则会打印信息:

interceptor

world

如果没有必要用到init和destroy方法,则让自定义的拦截器类,继承

AbstractInteceptor类,只需要重写intercept方法

拦截器栈中拦截器的执行顺序:

第一步:先配置的拦截器先实行。

第二步:在返回时,先配置的拦截器后执行。

如下图所示:

                                                                     

 

 

                                                                    

对于拦截器不仅可以拦截action也可以拦截处理action中的某个方法

需要用到的类:MethodFilterInterceptor

新建RegisterInterceptor3.java 代码如下:

package interceptor;

import com.opensymphony.xwork2.ActionInvocation;

import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class RegisterInterceptor3 extends MethodFilterInterceptor {

private static final long serialVersionUID = 1402540287268761843L;

@Override

protected String doIntercept(ActionInvocation invocation) throws Exception {

System.out.println("MethodFilterInterceptor");

String result = invocation.invoke();

return result;

}

}

而对于给拦截器类的struts.xml文件中的声明,加入param属性设置:

<param name="excludeMethods">方法名</param> <!--排除拦截的方法名-->

<param name="includeMethods">方法名</param> <!--包含拦截的方法名-->

多个方法名之间使用逗号隔开

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