struts攔截器
圖:
?
?
1、攔截器是什么?
分離關注:
完成一個功能,可以寫在一個類中,然后一個類中4個步驟,實現該類完成。
我們可以將4個步驟寫在4個類中,然后每一個類完成一部分功能,然后將其按順序執行,就可以完成我們想要的功能。
?
Struts2中的處理是通過過濾器完成的,struts2就使用了分離關注這個思想,它定義了多個過濾器,讓每一個過濾器只完成一個功能或者一個需求,然后核心過濾器只需要調用所有的自定義過濾器即可。然后這些自定義的過濾器就是struts2的攔截器。
?
2、攔截器和過濾器的區別
相同點:
???????? 都是起攔截作用
不同點:
?????????? 使用范圍:
??????????????????? 過濾器是屬于J2EE的范圍,過濾器所有的web工程都可以使用
??????????????????? 攔截器是屬于struts2框架的,使用攔截器必須在使用struts2框架,攔截器是離不開struts2框架的
?????????? 完成功能:
??????????????????? 以前的過濾器:攔截處理所有的功能
在擁有攔截器的情況下:只攔截頁面的請求資源
攔截器:其他的所有功能都交給攔截器來處理
??????????????????????????? 執行順序:
???????????????????????????????????? 先執行過濾器,然后再執行攔截器。因為攔截器由struts特定過濾器調用
?
3、理解struts-default.xml:
<?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:是struts2框架底層提供出來的name:用于讓其他包來繼承的abstract:設置為抽象包,內部不能有action--><package name="struts-default" abstract="true"><result-types><result-type name="chain" class="com.opensymphony.xwork2.ActionChainResult"/><result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/><result-type name="freemarker" class="org.apache.struts2.views.freemarker.FreemarkerResult"/><result-type name="httpheader" class="org.apache.struts2.dispatcher.HttpHeaderResult"/><result-type name="redirect" class="org.apache.struts2.dispatcher.ServletRedirectResult"/><result-type name="redirectAction" class="org.apache.struts2.dispatcher.ServletActionRedirectResult"/><result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/><result-type name="velocity" class="org.apache.struts2.dispatcher.VelocityResult"/><result-type name="xslt" class="org.apache.struts2.views.xslt.XSLTResult"/><result-type name="plainText" class="org.apache.struts2.dispatcher.PlainTextResult" /><result-type name="postback" class="org.apache.struts2.dispatcher.PostbackResult" /></result-types><!-- interceptorsinterceptor:聲明攔截器name:攔截器的名稱class:攔截器對應類的完成路徑--><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="cookie" class="org.apache.struts2.interceptor.CookieInterceptor"/><interceptor name="cookieProvider" class="org.apache.struts2.interceptor.CookieProviderInterceptor"/><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="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="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="datetime" class="org.apache.struts2.interceptor.DateTextFieldInterceptor" /><interceptor name="profiling" class="org.apache.struts2.interceptor.ProfilingActivationInterceptor" /><interceptor name="roles" class="org.apache.struts2.interceptor.RolesInterceptor" /><interceptor name="annotationWorkflow" class="com.opensymphony.xwork2.interceptor.annotations.AnnotationWorkflowInterceptor" /><interceptor name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" /><interceptor name="deprecation" class="org.apache.struts2.interceptor.DeprecationInterceptor" /><!-- Basic stack --><interceptor-stack name="basicStack"><interceptor-ref name="exception"/><interceptor-ref name="servletConfig"/><interceptor-ref name="prepare"/><interceptor-ref name="checkbox"/><interceptor-ref name="datetime"/><interceptor-ref name="multiselect"/><interceptor-ref name="actionMappingParams"/><interceptor-ref name="params"/><interceptor-ref name="conversionError"/><interceptor-ref name="deprecation"/></interceptor-stack><!-- Sample file upload stack --><interceptor-stack name="fileUploadStack"><interceptor-ref name="fileUpload"/><interceptor-ref name="basicStack"/></interceptor-stack><!-- interceptor-stack:聲明攔截器棧struts2通過使用攔截器棧來使用一些聲明好的攔截器在攔截器棧里面存放了一些上面聲明好的攔截器攔截器棧相當于一個list集合。執行的時候是按照存放的先后順序來執行的--><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="scopedModelDriven"/><interceptor-ref name="modelDriven"/><interceptor-ref name="fileUpload"/><interceptor-ref name="checkbox"/><interceptor-ref name="datetime"/><interceptor-ref name="multiselect"/><interceptor-ref name="staticParams"/><interceptor-ref name="actionMappingParams"/><interceptor-ref name="params"/><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-ref name="debugging"/><interceptor-ref name="deprecation"/></interceptor-stack></interceptors><!-- 配置在struts2框架運行時默認要執行的攔截器棧 --><default-interceptor-ref name="defaultStack"/><default-class-ref class="com.opensymphony.xwork2.ActionSupport" /></package></struts>
?
自問自答:
為什么我們在struts.xml中定義的package都顯式或者隱式繼承struts-default?
因為struts2的功能被分化了,是通過各個攔截器來實現的。而各個基礎攔截器的聲明與組成攔截器棧調用時在struts-default.xml里面配置的。如果不繼承這文件,就沒有辦法使用struts2提供的所有攔截器。
為什么我們的struts文件都必須命名為struts.xml并且在classpath下?
你可以查看源碼,可以發現取讀的配置文件的路徑是一個靜態final變量,依次是struts-default.xml,struts-plugin.xml,struts.xml
?
?