struts2-interceptor
前言:在使用struts2的自定义拦截器时,struts2默认的18个拦截器会失效所以我们需要在配置完自定义拦截器时把自定义拦截器也加上
- interceptor类
- 实现MethodFilterInterceptor类
- invoke() 放行
- struts.xml配置
- 定义拦截器
- 使用拦截器
hwwefb
dscsdj
- interceptor类
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
public class textController extends MethodFilterInterceptor {
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
System.out.println("进入拦截器");
//需要放行 如果不放行的话被拦截的请求是不能通过的 会一直在这卡着
arg0.invoke();
System.out.println("成功放行 离开拦截器");
return null;
}
}
- 定义拦截器
<interceptors>
<!-- 定义拦截器 -->
<interceptor name="text_01" class="com.yjy.controller.textController"> </interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="text-stack-01">
<!-- 继承之前定义的拦截器 -->
<interceptor-ref name="text_01">
<!-- 只拦截textRequestMethod的请求 -->
<param name="includeMethods" >textRequestMethod</param>
</interceptor-ref>
<!-- 继承struts2自定义的18个拦截器 -->
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
1. interceptors:所有我们自己定义的拦截器都需要写在这里面
2. interceptor:自定义拦截器 只能写在interceptors标签里面
- name:给拦截器取一根唯一名字
3. interceptor-stack:拦截器栈,可以把多个拦截器组合在一起 也只能写在interceptors标签里面
- name:给拦截器栈取一根唯一名字
4. interceptor-ref:按照name继承拦截器,
- name:之前定义了的interceptor的名字
5. param:加上这个可以指定拦截哪些请求
- name:includeMethods包含什么什么请求的
excludeMethods不包含什么什么请求的
- 使用拦截器
<action name="text_*" class="com.yjy.action.TextAction" method="{1}">
<result name="SUCCESS-text">/SUCCESS.jsp</result>
<!-- 引入 拦截器 -->
<interceptor-ref name="text-stack-01"></interceptor-ref>
</action>