struts 二 拦截器
struts 2 拦截器
在struts.xml配置拦截器:
在struts.xml使用拦截器:
对于常用的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>
如需查看源代码,点此下载