自定义一个拦截器的步骤

1.类

继承interceptor接口或者继承AbstractInterceptor抽象类

package cn.com.app;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
public class MyInterceptor extends AbstractInterceptor {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Override
	public String intercept(ActionInvocation actioninvocation) throws Exception {
		// TODO Auto-generated method stub
		System.out.println(\"init.....\");
		String result = actioninvocation.invoke();
		System.out.println(\"destory.....\");
		return result;
	}

}

2.配置文件

配置自定义的拦截器

	<interceptors>
			<interceptor name=\"myinterceptor\" class=\"cn.com.app.MyInterceptor\"></interceptor>
		</interceptors>
<action name=\"texttoken\" class=\"cn.com.app.TextToken\">
			<interceptor-ref name=\"myinterceptor\"></interceptor-ref>
			<interceptor-ref name=\"tokenSession\"></interceptor-ref>
			<interceptor-ref name=\"defaultStack\"></interceptor-ref>
			<result>/success.jsp</result>
			<!-- <result name=\"invalid.token\">/error.jsp</result> -->
		</action>

结果如下

\"\"


 如果自定义的拦截器里面没有String result = actioninvocation.invoke();的话,就不会执行之后的拦截器了。


拦截器栈里面有多个拦截器,都是按次序进行的

\"\"

 

收藏 打印