Struts2(9):开发自己的Struts2拦截器(编码与配置)

     要开发自己的Struts2拦截器类,需要实现Interceptor接口,并实现相应的三个方法 。或继承AbstractInterceptor类,只实现doIntercept方法。这两个类都在xwork.jar包中,路径为com.opensymphony.xwork2.interceptor。

 

拦截器类MyInterceptor

public class MyInterceptor implements Interceptor {
	

	public void destroy() {
		System.out.println("destroy...");
	}

	public void init() {
		System.out.println("init...");
	}

	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("my interceptor...");
		String result = invocation.invoke();
		return result;
	}
}

 init()方法会在服务器启动时自动执行,只会执行一次。

 

写好自己的拦截器类后,在struts2的配置文件struts.xml文件中也要做相应的配置,在package那层下,与其它action同级

<interceptors> 
		<interceptor name="myInterceptor" class="com.interceptor.MyInterceptor"></interceptor>	
</interceptors> 

 在action中使用刚配置好的自定义拦截器类

<action name="register" class="com.test.action.RegisterAction">
		<result name="success">/success.jsp</result>
		<result name="input">/register.jsp</result>	
		<interceptor-ref name="myInterceptor"></interceptor-ref>
</action>

 

当程序在调用register action时,会执行MyInterceptor的intercept方法。

注意:在action中使用自定义的拦截器后,Struts2中自带的默认拦截器都将失效,需手工将默认的拦截器也加入进来。如下,

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

 

默认的拦截器在struts2-core.jar包的根目录下,名称为struts-default.xml

 

我们在定义一个struts.xml文件时,都会声明此配置文件继承struts-default,如下,只有在使用了自定义的拦截器后,action才不会再调用默认的拦截器,所以需要手工将其再配置一次。

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

 

2,在拦截器中定义变量hello,在配置文件中可定义此变更的值,可以声明拦截器处赋值,也可在使用的action处赋值,如
在声明处

<interceptor name="myInterceptor" class="com.interceptor.MyInterceptor">
	<param name="hello">world</param>
</interceptor>	

 

在action使用处,此处的赋值会覆盖上面的赋值

<action name="register" class="com.test.action.RegisterAction">
	<result name="success">/success.jsp</result>
	<result name="input">/register.jsp</result>	
	<interceptor-ref name="myInterceptor">
		<param name="hello">world2</param>		
	</interceptor-ref>
</action>

 

3,拦截器栈:它和拦截器类似,在一个拦截器栈中可以引用多个拦截器或多个拦截器栈。

<interceptor-stack name="myStack">
	<interceptor-ref name="myInterceptor"></interceptor-ref>		
	<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>

 拦截器栈采用的是先进后出的操作顺序,即在执行目标对象方法前,先按栈中拦截器的顺序执行拦截器中的方法,再执行目标对象的方法,执行完再按逆序执行栈中的方法。

interceptor1
interceptor2
execute(目标对象的方法)
interceptor2
interceptor1

 

4,改变默认拦截器,如下配置,如下可以声明一个默认拦截器,它与action平级

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

如果一个action中没有配置一个自定义地拦截器,则它会默认使用默认拦截器。注意:只能有一个默认的拦截器

 

5,方法过滤拦截器:需要继承MethodFilterInterceptor并实现doIntercept方法

自定义的拦截器会拦截action中的所有方法(除了javabean方法,即get,set方法)

public class MyMethodInterceptor extends MethodFilterInterceptor {
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		System.out.println("myMethodFilterInterceptor....");
		return invocation.invoke();
	}

 

在配置文件中声明此拦截器。

<interceptor name="myMethodInterceptor" class="com.interceptor.MyMethodInterceptor">

 

在action中配置,注意必须定义在其它拦截器前

<action name="register" class="com.test.action.RegisterAction">
	<result name="success">/success.jsp</result>
	<result name="input">/register.jsp</result>
	<interceptor-ref name="myMethodInterceptor">
		<param name="includeMethods">test1,execute</param> <!-- 包含,-->
		<param name="excludeMethods">test2,execute</param> <!-- 不包含,当一个方法在includeMethods和excludeMethods都有声明时,则includeMethods优先-->
	</interceptor-ref>
	<interceptor-ref name="myStack"></interceptor-ref>	
</action>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值