struts2总结(4)自定义拦截器

在strut2中 个人认为最重要的还是自定义拦截器的使用

首先要定义拦截器 以<interceptors>包括起来 在这之中 可以定义拦截器 也可以定义拦截器栈 进行使用

  <interceptor name = "first" class = "com.kevinswift.first"></interceptors>
   <interceptor name = "second" class = "com.kevinswift.second"></interceptors>
   <interceptor-stack name = "mystack">
   <interceptor-ref name = "first"></interceptor-ref>
   <interceptor-ref name = "second"></interceptor-ref>
   </interceptor-stack>
要记住拦截器栈的使用也是需要先定义拦截器的 

随后在action中进行调用

     <action name = "firstAction" class = "com.kevinswift.action.FirstAction">
  <!-- 指定action结果类型为chain -->
<result type = "chain">secondAction</result>
<interceptor-ref name = "mystack"></interceptor-ref>
  </action>
  <action name = "secondAction" class = "com.kevinswift.action.SecondAction">
  <result>success.jsp</result>
定义为拦截器链

在拦截器指定的类中 常用的是继承自abstractInterceptor接口 只需调用一个intercept方法即可 

package com.custom.action;

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

public class FirstAction extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("第一个拦截器的使用" );
		//调用下一个拦截器
		String result = arg0.invoke();
		System.out.println("第一个拦截器的调用 2");
		
		return result;
	}

}
invoke方法表示下一拦截器的使用 当没有拦截器可以使用的时候 就默认调用action的类了

secondAction中也同样如此

package com.custom.action;

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

public class SecondAction extends AbstractInterceptor {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("第二个拦截器的调用1");
		String res = arg0.invoke();
		System.out.println("第二个拦截器的调用2");
		return res;
	}

}

大神建议struts看源码 还是大神比较靠谱 大笑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值