Struts2拦截器
Struts2工作原理
1、客户端发出请求
2、请求经过一系列的过滤器 包括Struts2的过滤器FilterDispatcher
3、FilterDispatcher会询问ActionMapper
3.1、当前请求不需要调用某个Action 则将该请求交给Servlet容器去处理
3.2、当前请求确定需要调用某个Action, 则将该请求的处理交给ActionProxy
4、
ActionProxy通过ConfigurationManager 获取struts.xml 中有关被调用Action的信息
5、ActionProxy根据信息创建ActionInvocation实例
6、ActionInvocation实例使用命令模式来调用拦截器和Action
7、ActionInvocation实例根据struts.xml中找到对应的返回结果并回应
Struts2拦截器介绍
1、可以实现横切功能并使这些实现相对action甚至Struts2框架保持独立
2、可以实现和使用自己所需的特性且不用修改框架的底层代码
3、使用拦截器可以达到以下目的
在调用Action之前,提供预处理逻辑
与Action进行交互,提供执行信息
在调用Action之后,提供后处理逻辑
修改返回的结果,进而修改呈现给用户的内容
捕获异常从而替换可执行的处理过程或返回一个不同结果
常见的拦截器和拦截器栈(详见struts-default.xml)
<interceptors> <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="clearSession" class="org.apache.struts2.interceptor.ClearSessionInterceptor" /> <interceptor name="createSession" class="org.apache.struts2.interceptor.CreateSessionInterceptor" /> <interceptor name="debugging" class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" /> <interceptor name="externalRef" 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="modelDriven" class="com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor"/> <interceptor name="scopedModelDriven" class="com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor"/> <interceptor name="params" class="com.opensymphony.xwork2.interceptor.ParametersInterceptor"/> <interceptor name="actionMappingParams" class="org.apache.struts2.interceptor.ActionMappingParametersInteceptor"/> <interceptor name="prepare" class="com.opensymphony.xwork2.interceptor.PrepareInterceptor"/> <interceptor name="staticParams" class="com.opensymphony.xwork2.interceptor.StaticParametersInterceptor"/> <interceptor name="scope" class="org.apache.struts2.interceptor.ScopeInterceptor"/> <interceptor name="servletConfig" 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="tokenSession" class="org.apache.struts2.interceptor.TokenSessionStoreInterceptor"/> <interceptor name="validation" class="org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor"/> <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" /> <interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" /> <interceptor name="jsonValidation" class="org.apache.struts2.interceptor.validation.JSONValidationInterceptor" /> <interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" />
<interceptor-stack name="defaultStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="debugging"/> <interceptor-ref name="profiling"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="fileUpload"/> <interceptor-ref name="checkbox"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"> <param name="excludeParams">dojo..*,^struts..*</param> </interceptor-ref> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> </interceptor-stack>
Struts2自定义拦截器
1、自定义拦截器的设计
package demo.interceptor; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class MyInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { //继承AbstractInterceptor,覆盖其interceptor()方法 // TODO Auto-generated method stub System.out.println("我开始拦截了。。。"); String result = invocation.invoke();////请求放行,交给下一个拦截器或者组件(如果不调用invocation.invoke(),则请求就不会到达Action组件) System.out.println("返回结果:"+result); return result;//返回视图结果代码 } }
2、自定义拦截器的配置
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <package name="helloworld" extends="struts-default"> <!-- name:自定义包名 extends:继承自struts2的缺省包struts-default --> <interceptors> <!-- 定义自定义拦截器 name:自定义拦截器名 class:自定义拦截器全名 --> <interceptor name="interceptor" class="demo.interceptor.MyInterceptor"></interceptor> </interceptors> <action name="hello" class="demo.web.HelloWorldAction"> <!-- name:action请求的映射名 class:action组件的全名 --> <!-- 引用上面定义的拦截器 --> <interceptor-ref name="interceptor"></interceptor-ref> <result name="success">/helloworld.jsp</result><!-- name:action中execute对应的结果别名 /helloworld.jsp:视图页面中的URL地址 --> </action> </package> </struts>
3、调试运行
可以看到,已经在控制台打印出来了,但是显示的结果却是null,因为自定义拦截器之后,struts2就不会执行默认的struts2的拦截器了,所以还需要我们手动加上struts2的默认拦截器。
<action name="hello" class="demo.web.HelloWorldAction"> <!-- name:action请求的映射名 class:action组件的全名 --> <!-- 引用上面定义的拦截器 --> <interceptor-ref name="interceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref><!-- struts2默认的拦截器 --> <result name="success">/helloworld.jsp</result><!-- name:action中execute对应的结果别名 /helloworld.jsp:视图页面中的URL地址 --> </action>
现在就出来结果了:
程序源码请到Struts2拦截器下载(jar需要自己手动加上)