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

本文详细介绍了 Struts2 中的拦截器概念及其应用。包括如何创建自定义拦截器、配置拦截器栈、使用 MethodFilterInterceptor 来拦截特定方法等内容。同时探讨了拦截器的执行顺序和如何通过参数配置拦截器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

第十一讲

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

用到文件存放位置:

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> <!--包含拦截的方法名-->

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值