2018.12.17 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.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.devMode" value="true"></constant>
<package name="test" namespace="/" extends="struts-default">
<!-- 1.注册拦截器 -->
<interceptors>
<interceptor name="MyInterceptor3" class="com.legend.a_interceptor.MyInterceptor3"></interceptor>
<!-- 2.注册拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 自定义拦截器(建议放在20个拦截器之前) -->
<interceptor-ref name="MyInterceptor3">
<!-- 指定那些方法是不拦截的 -->
<!-- <param name="excludeMethods">find,add</param>-->
<!-- 指定那些方法需要拦截的 -->
<param name="includeMethods">find,add</param>
</interceptor-ref>
<!-- 引用默认的拦截器栈(20个) -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!-- 3.指定默认拦截器栈 -->
<default-interceptor-ref name="myStack"></default-interceptor-ref>
<global-exception-mappings>
<exception-mapping result="error" exception="java.lang.RuntimeException"></exception-mapping>
</global-exception-mappings>
<!-- 配置Action -->
<action name="Demo1Action_*" class="com.legend.a_interceptor.Demo1Action" method="{1}">
<!-- 为Action单独走那个拦截器栈 -->
<!--<interceptor-ref name="myStack"></interceptor-ref>-->
<result name="success">/success.jsp</result>
<result name="error">/error.jsp</result>
</action>
</package>
<!-- struts2标签库 -->
<package name="tag" namespace="/" extends="struts-default">
<action name="Demo2Action" class="com.legend.b_tag.Demo2Action" method="execute">
<result name="success">/tag1.jsp</result>
</action>
</package>
</struts>
MyInterceptor3.java
package com.legend.a_interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
/**
* 拦截器第三种创建方法----继承MethodFilterInterceptor方法过滤拦截器 ,
* 定制哪些方法需要拦截,定制哪些方法不需要拦截
* @author qichunlin
*功能:定制拦截器拦截方法
*/
public class MyInterceptor3 extends MethodFilterInterceptor{
private static final long serialVersionUID = 1L;
@Override
protected String doIntercept(ActionInvocation invocation) throws Exception {
//前处理
System.out.println("MyInterceptor3前处理");
// 放行
invocation.invoke();
//后处理
System.out.println("MyInterceptor3后处理");
return "success";
}
}