struts2-interceptor

struts2-interceptor

前言:在使用struts2的自定义拦截器时,struts2默认的18个拦截器会失效所以我们需要在配置完自定义拦截器时把自定义拦截器也加上

  • interceptor类
    • 实现MethodFilterInterceptor类
    • invoke() 放行
  • struts.xml配置
    • 定义拦截器
    • 使用拦截器

hwwefb
dscsdj

  1. 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;
	}
}
  1. 定义拦截器
<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不包含什么什么请求的
  1. 使用拦截器
<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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值