日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

自定义拦截器和提供的拦截器

發布時間:2025/3/20 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 自定义拦截器和提供的拦截器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在訪問struts2中某個action之后或者之前,會自動調用的類,就是struts2中的攔截器

一,怎樣自定義攔截器

自定義一個攔截器需要三步:
1 ,自定義一個實現Interceptor接口(或者繼承自AbstractInterceptor)的類。
2 ,在struts.xml中注冊上一步中定義的攔截器。
3 ,在需要使用的Action中引用上述定義的攔截器,為了方便也可將攔截器定義為默認的攔截器,這樣在不加特殊聲明的情況下所有的Action都被這個攔截器攔截。

Interceptor接口聲明了三個方法:

public interface Interceptor extends Serializable {void destroy();void init();String intercept(ActionInvocation invocation) throws Exception; }
Init方法在攔截器類被創建之后,使用這個方法可以給攔截器類做必要的初始話操作,主要用于初始化系統資源,如打開數據庫資源

Destroy方法在攔截器被垃圾回收之前調用,用來回收init方法初始化的資源。

Intercept是攔截器的主要攔截方法,如果需要調用后續的Action或者攔截器,只需要在該方法中調用invocation.invoke()方法即可,在該方法調用的前后可以插入Action調用前后攔截器需要做的方法。
如果不需要調用后續的方法,則返回一個String類型的對象即可,例Action.SUCCESS,即可返回Action中SUCCESS邏輯視圖的字符串對應的jsp


另外AbstractInterceptor提供了一個簡單的Interceptor的實現,這個實現為:

public abstract class AbstractInterceptor implements Interceptor {public void init() {}public void destroy() {}public abstract String intercept(ActionInvocation invocation) throws Exception; }
所以 編寫攔截器時只需要繼承AbstractInterceptor抽象類即可,重寫使用的方法。

二,struts2提供的攔截器功能說明



struts2提供的攔截器的作用:

sturts2本身只是一個空的容器,而是大量的攔截器讓struts2擁有非常強大的功能,比如防止表單重復提交,進行輸入校驗…打開struts2-core.jar包下的struts-default.xml中看到struts2都實現了哪些攔截器。struts-default.xml這個文件是寫struts.xml時需要繼承的xml文件,其中就聲明了大量的攔截器和攔截器棧。我們可以找到defaultStack這個攔截器棧,它就是struts2默認加載的攔截器棧,他提供了struts2的基本操作,比如得到參數并將參數賦值給對應的action中的屬性……

注:當我們手動為某個action添加一個攔截器的時候,會讓defaultStack自動無效,所以需要首先引用defaultStack然后再添加其他的攔截器

struts-default.xml

<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-types><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="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 name="multiselect" class="org.apache.struts2.interceptor.MultiselectInterceptor" /><!-- 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="multiselect"/><interceptor-ref name="actionMappingParams"/><interceptor-ref name="params"><param name="excludeParams">dojo\..*,^struts\..*</param></interceptor-ref><interceptor-ref name="conversionError"/></interceptor-stack><!-- Sample validation and workflow stack --><interceptor-stack name="validationWorkflowStack"><interceptor-ref name="basicStack"/><interceptor-ref name="validation"/><interceptor-ref name="workflow"/></interceptor-stack><!-- Sample JSON validation stack --><interceptor-stack name="jsonValidationWorkflowStack"><interceptor-ref name="basicStack"/><interceptor-ref name="validation"><param name="excludeMethods">input,back,cancel</param></interceptor-ref><interceptor-ref name="jsonValidation"/><interceptor-ref name="workflow"/></interceptor-stack><!-- Sample file upload stack --><interceptor-stack name="fileUploadStack"><interceptor-ref name="fileUpload"/><interceptor-ref name="basicStack"/></interceptor-stack><!-- Sample model-driven stack --><interceptor-stack name="modelDrivenStack"><interceptor-ref name="modelDriven"/><interceptor-ref name="basicStack"/></interceptor-stack><!-- Sample action chaining stack --><interceptor-stack name="chainStack"><interceptor-ref name="chain"/><interceptor-ref name="basicStack"/></interceptor-stack><!-- Sample i18n stack --><interceptor-stack name="i18nStack"><interceptor-ref name="i18n"/><interceptor-ref name="basicStack"/></interceptor-stack><!-- An example of the paramsPrepareParams trick. This stackis exactly the same as the defaultStack, except that itincludes one extra interceptor before the prepare interceptor:the params interceptor.This is useful for when you wish to apply parameters directlyto an object that you wish to load externally (such as a DAOor database or service layer), but can't load that objectuntil at least the ID parameter has been loaded. By loadingthe parameters twice, you can retrieve the object in theprepare() method, allowing the second params interceptor toapply the values on the object. --><interceptor-stack name="paramsPrepareParamsStack"><interceptor-ref name="exception"/><interceptor-ref name="alias"/><interceptor-ref name="i18n"/><interceptor-ref name="checkbox"/><interceptor-ref name="multiselect"/><interceptor-ref name="params"><param name="excludeParams">dojo\..*,^struts\..*</param></interceptor-ref><interceptor-ref name="servletConfig"/><interceptor-ref name="prepare"/><interceptor-ref name="chain"/><interceptor-ref name="modelDriven"/><interceptor-ref name="fileUpload"/><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><!-- A complete stack with all the common interceptors in place.Generally, this stack should be the one you use, though itmay do more than you need. Also, the ordering can beswitched around (ex: if you wish to have your servlet-relatedobjects applied before prepare() is called, you'd need to moveservletConfig interceptor up.This stack also excludes from the normal validation and workflowthe method names input, back, and cancel. These typically areassociated with requests that should not be validated.--><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="scopedModelDriven"/><interceptor-ref name="modelDriven"/><interceptor-ref name="fileUpload"/><interceptor-ref name="checkbox"/><interceptor-ref name="multiselect"/><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><!-- The completeStack is here for backwards compatibility forapplications that still refer to the defaultStack by theold name --><interceptor-stack name="completeStack"><interceptor-ref name="defaultStack"/></interceptor-stack><!-- Sample execute and wait stack.Note: execAndWait should always be the *last* interceptor. --><interceptor-stack name="executeAndWaitStack"><interceptor-ref name="execAndWait"><param name="excludeMethods">input,back,cancel</param></interceptor-ref><interceptor-ref name="defaultStack"/><interceptor-ref name="execAndWait"><param name="excludeMethods">input,back,cancel</param></interceptor-ref></interceptor-stack></interceptors><default-interceptor-ref name="defaultStack"/><default-class-ref class="com.opensymphony.xwork2.ActionSupport" /></package>

總結

以上是生活随笔為你收集整理的自定义拦截器和提供的拦截器的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。