Struts2拦截器
来源:互联网 发布:windows player手机版 编辑:程序博客网 时间:2024/05/18 23:28
拦截器,在AOP(Aspect-Oriented Programming)中用于在某个方法或字段被访问之前,进行拦截然后在之前或之后加入某些操作。拦截是AOP的一种实现策略。
在Webwork的中文文档的解释为——拦截器是动态拦截Action调用的对象。它提供了一种机制可以使开发者可以定义在一个action执行的前后执行的代码,也可以在一个action执行前阻止其执行。同时也是提供了一种可以提取action中可重用的部分的方式。
谈到拦截器,还有一个词大家应该知道——拦截器链(Interceptor Chain,在Struts 2中称为拦截器栈Interceptor Stack)。拦截器链就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。
实现原理:
Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器,如下图所示。
已有的拦截器
Struts 2已经为您提供丰富多样的,功能齐全的拦截器实现。大家可以到struts2-all-2.0.1.jar或struts2-core-2.0.1.jar包的struts-default.xml查看关于默认的拦截器与拦截器链的配置。
以下部分就是从struts-default.xml文件摘取的内容:
< interceptor name ="alias" class ="com.opensymphony.xwork2.interceptor.AliasInterceptor" /> < interceptor name ="autowiring" class ="com.opensymphony.xwork2.spring.interceptor.ActionAutowiringInterceptor" /> < interceptor name ="chain" class ="com.opensymphony.xwork2.interceptor.ChainingInterceptor" /> < interceptor name ="conversionError" class ="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor" /> < interceptor name ="createSession" class ="org.apache.struts2.interceptor.CreateSessionInterceptor" /> < interceptor name ="debugging" class ="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /> < interceptor name ="external-ref" class ="com.opensymphony.xwork2.interceptor.ExternalReferencesInterceptor" /> < interceptor name ="execAndWait" class ="org.apache.struts2.interceptor.ExecuteAndWaitInterceptor" /> < interceptor name ="exception" class ="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor" /> < interceptor name ="fileUpload" class ="org.apache.struts2.interceptor.FileUploadInterceptor" /> < interceptor name ="i18n" class ="com.opensymphony.xwork2.interceptor.I18nInterceptor" /> < interceptor name ="logger" class ="com.opensymphony.xwork2.interceptor.LoggingInterceptor" /> < interceptor name ="model-driven" class ="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor" /> < interceptor name ="scoped-model-driven" class ="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor" /> < interceptor name ="params" class ="com.opensymphony.xwork2.interceptor.ParametersInterceptor" /> < interceptor name ="prepare" class ="com.opensymphony.xwork2.interceptor.PrepareInterceptor" /> < interceptor name ="static-params" class ="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor" /> < interceptor name ="scope" class ="org.apache.struts2.interceptor.ScopeInterceptor" /> < interceptor name ="servlet-config" class ="org.apache.struts2.interceptor.ServletConfigInterceptor" /> < interceptor name ="sessionAutowiring" class ="org.apache.struts2.spring.interceptor.SessionContextAutowiringInterceptor" /> < interceptor name ="timer" class ="com.opensymphony.xwork2.interceptor.TimerInterceptor" /> < interceptor name ="token" class ="org.apache.struts2.interceptor.TokenInterceptor" /> < interceptor name ="token-session" class ="org.apache.struts2.interceptor.TokenSessionStoreInterceptor" /> < interceptor name ="validation" class ="com.opensymphony.xwork2.validator.ValidationInterceptor" /> < interceptor name ="workflow" class ="com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor" /> < interceptor name ="store" class ="org.apache.struts2.interceptor.MessageStoreInterceptor" /> < interceptor name ="checkbox" class ="org.apache.struts2.interceptor.CheckboxInterceptor" /> < interceptor name ="profiling" class ="org.apache.struts2.interceptor.ProfilingActivationInterceptor" />
配置和使用拦截器
在struts-default.xml中已经配置了以上的拦截器。如果您想要使用上述拦截器,只需要在应用程序struts.xml文件中通过“<include file="struts-default.xml" />”将struts-default.xml文件包含进来,并继承其中的struts-default包(package),最后在定义Action时,使用“<interceptor-ref name="xx" />”引用拦截器或拦截器栈(interceptor stack)。一旦您继承了struts-default包(package),所有Action都会调用拦截器栈 ——defaultStack。当然,在Action配置中加入“<interceptor-ref name="xx" />”可以覆盖defaultStack。
JSP:
<form action="test_del" method="post"><input type="text" name="uname"><input type="submit" value="确定"></form>
拦截器使用的例子。首先,新建Action类action/TestAction.java,内容如下:
package action;import com.opensymphony.xwork2.ActionSupport;public class TestAction extends ActionSupport{ private static final long serialVersionUID = 1L; private String uname; public String add() throws Exception { //获取当前方法名称 :String methodName = Thread.currentThread().getStackTrace()[1].getMethodName(); System.out.println("进入add方法......"); System.out.println("uname =" + uname); return "success"; } public String del() throws Exception { System.out.println("进入del方法......"); System.out.println("uname =" + uname); return "success"; } public String edit() throws Exception { System.out.println("进入edit方法......"); System.out.println("uname =" + uname); return "success"; } public String find() throws Exception { System.out.println("进入find方法......"); System.out.println("uname =" + uname); return "success";} public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; }}
其次,新建interceptor类interceptor/TestInterceptor,内容如下:
package interceptor;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.AbstractInterceptor;import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;public class TestInterceptor extends MethodFilterInterceptor{private static final long serialVersionUID = 1L;/** * @method:方法拦截器MethodFilterInterceptor: 控制器可以拦截单个、多个方法 */@Overrideprotected String doIntercept(ActionInvocation invacation) throws Exception {//获取JSP中的URL:String jspURLName = invacation.getInvocationContext().getName();//获取Action中拦截的方法String methodName = invacation.getProxy().getMethod();System.out.println("开始记录--"+methodName+"方法--日志...");String result = invacation.invoke();System.out.println("result =" + result);System.out.println("结束记录--"+methodName+"方法--日志...");return result;}/** * @method 抽象类拦截器AbstractInterceptor--控制器可以拦截所有方法 * @param invacation * @return * @throws Exception *//*@Overridepublic String intercept(ActionInvocation invacation) throws Exception {System.out.println("开始记录日志...");String result = invacation.invoke();System.out.println("result =" + result);System.out.println("结束记录日志...");return result;}*/}
在Struts2.XML中配置action,配置文件如下:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN""http://struts.apache.org/dtds/struts-2.3.dtd"><struts> <package name="default" extends="struts-default" namespace="/"> <!-- 注入拦截器 --> <interceptors> <!-- 配置单个拦截器 --> <interceptor name="testInterceptor" class="interceptor.TestInterceptor"> <!-- includeMethods: 拦截单个、多个方法 (比如:add多个方法之间用,隔开) --> <!-- <param name="includeMethods">add</param> --> <!-- includeMethods: 不拦截单个、多个方法 (比如:add多个方法之间用,隔开) --> <param name="excludeMethods">add,edit,find</param> </interceptor> <!-- 配置拦截器栈 --> <interceptor-stack name="myStack"> <interceptor-ref name="testInterceptor"/> <interceptor-ref name="defaultStack"/> </interceptor-stack> </interceptors> <!-- 配置action --> <action name="test_*" class="action.TestAction" method="{1}"> <interceptor-ref name="myStack"/> <result name="success">/success.jsp</result> </action> </package></struts>
- 【Struts2】Struts2中拦截器
- struts2拦截器
- Struts2拦截器
- struts2.0拦截器
- struts2.0 拦截器
- struts2拦截器学习
- struts2 拦截器
- Struts2拦截器(Interceptor)
- struts2.0拦截器
- 用上struts2拦截器
- struts2拦截器简介
- Struts2:拦截器概述
- Struts2拦截器(Interceptor)
- struts2 拦截器
- 关于struts2拦截器
- Struts2的拦截器
- struts2拦截器
- Struts2拦截器总结
- Unity3D计时器四种写法
- iOS代理不回调的多种原因
- hdu 1269
- android活动的启动模式
- linux c语言之pipe()函数
- Struts2拦截器
- git命令大全--常用命令
- 检测到目标URL存在http host头攻击漏洞
- 【算法学习】一行核心代码实现阶乘函数和Fibonacci数列
- Leetcode之Non-decreasing Array 问题
- CentOS7 升级到7.4
- Reversing Linked List
- appium在android7.0真机上测试程序时报错command failed shell “ps ‘uiautomator’”的解决方式
- C++运算符重载