struts 2 拦截器

对于常用的MVC框架来说,他们都会完成一些通用的控制逻辑,例如解析请求参数,类型转换,权限控制等,struts 2的拦截器采取的是可插拔式的设计,如果我们需要使用某个拦截器,只需要在配置文件中应用改拦截器即可;如果不需要使用该拦截器,只需要在配置文件中取消应用该拦截器——不管是否应用某个拦截器,对于struts 2框架不会有任何影响。具体实现如下:

实现一个拦截器:

public class TestInterceptor extends AbstractInterceptor{
	
	@Override
	public String intercept(ActionInvocation actionInvocation) throws Exception {
		System.out.println("进入Action之前,先拦截一次");
		String result = actionInvocation.invoke();
		System.out.println("执行完action方法之后,再拦截一次");
		return result;
	}

}

在struts.xml配置拦截器:

<interceptors>
			<interceptor name="testInterceptor" class="com.durendong.action.TestInterceptor.intercept"></interceptor>
		</interceptors>

在struts.xml使用拦截器:

<action name="login" class="com.durendong.action.LoginAction"> 			<!--应用拦截器--> 			<interceptor-ref name="testInterceptor" /> 			<result name="success">/WEB-INF/content/login.jsp</result> 
		</action>

如需查看源代码,点此下载

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值