struts2自定义拦截器和方法拦截器

一.自定义拦截器

使用Interceptor接口或继承AbstractInterceptor类

Interceptor接口要实现init,intercept,destroy三个方法

AbstractInterceptor类只需要实现intercept方法,init和destroy方法已经在AbstractInterceptor中实现

1.使用Interceptor接口


package com.zucc.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class TestInterceptor1 implements Interceptor{
	
	public TestInterceptor1(){
		System.out.println("TestInterceptor1 constructor....");
	}

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

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

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("TestInterceptor1 intercept....");
		String resultName = invocation.invoke();
		return resultName;
	}

}

2. 继承AbstractInterceptor类


package com.zucc.interceptor;

import org.w3c.dom.views.AbstractView;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class TestInterceptor2 extends AbstractInterceptor{
	
	public TestInterceptor2(){
		System.out.println("TestInterceptor2 constructor....");
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		System.out.println("TestInterceptor2 intercept....");
		String resultName = invocation.invoke();
		return resultName;
	}
}


在struts2.xml中配置(先在<interceptors></interceptors>中把拦截器配置好,然后再需要使用的地方用<interceptor-ref name=""></interceptor-ref>进行使用)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    
 	<package name="default" namespace="/" extends="struts-default">
 		<interceptors>
 			<interceptor name="testInterceptor1" class="com.zucc.interceptor.TestInterceptor1"></interceptor>
 			<interceptor name="testInterceptor2" class="com.zucc.interceptor.TestInterceptor2"></interceptor>
 		</interceptors>
 		<action name="test" class="com.zucc.action.TestAction">
 			<result>/Hello.jsp</result>
 			<interceptor-ref name="testInterceptor1"></interceptor-ref>
 			<interceptor-ref name="testInterceptor2"></interceptor-ref>
 		</action>
    </package>
</struts>



二. 自定义方法拦截器

继承MethodFilterInterceptor抽象类,实现MethodFilterInterceptor抽象类中的doIntercept方法可以对需要拦截的方法做一些处理,然后在struts.xml中进行配置。


MethodFilterInterceptor实现类

package com.zucc.interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

public class TestMethodFilterInterceptor extends MethodFilterInterceptor{

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("TestMethodFilterInterceptor doIntercept...");
		String resultCode = invocation.invoke();
		return resultCode;
	}

}


struts.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

	<constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <constant name="struts.devMode" value="true" />
    
 	<package name="default" namespace="/" extends="struts-default">
 		<interceptors>
 			<interceptor name="testMethodFilterInterceptor" class="com.zucc.interceptor.TestMethodFilterInterceptor"></interceptor>
 		</interceptors>
 		<action name="test" class="com.zucc.action.TestAction">
 			<result>/Hello.jsp</result>
 			<result name="error">/error.jsp</result>
 			<interceptor-ref name="testMethodFilterInterceptor">
 				<!-- 配置哪些方法不需要拦截 -->
 				<param name="excludeMethods">list,execute</param>
 				<!-- 配置哪些方法需要拦截 -->
 				<param name="includeMethods">add,del</param>
 			</interceptor-ref>
 		</action>
    </package>
</struts>

注意:excludeMethods和includeMethods一般只需要写一个就行,不用两个都写。在excludeMethods和includeMethods里也可以使用通配符,如add*表示所有以add开头的方法。在excludeMethods和includeMethods都写的情况下:如果某个方法同时在excludeMethods和includeMethods里,以includeMethods为准,即被拦截;如果某个方法在excludeMethods和includeMethods里都没有,以excludeMethods为准,即不被拦截。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值