拦截器原理
Understanding Interceptors
Interceptors can execute code before and after an Action is invoked. Most of the framework's core functionality is implemented as Interceptors. Features like double-submit guards, type conversion, object population, validation, file upload, page preparation, and more, are all implemented with the help of Interceptors. Each and every Interceptor is pluggable, so you can decide exactly which features an Action needs to support.
Interceptors can be configured on a per-action basis. Your own custom Interceptors can be mixed-and-matched with the Interceptors bundled with the framework. Interceptors "set the stage" for the Action classes, doing much of the "heavy lifting" before the Action executes.
拦截器配置struts.xml
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".." />
<interceptor name="logger" class=".." />
</interceptors>
<action name="login" class="tutorial.Login">
<interceptor-ref name="timer" />
<interceptor-ref name="logger" />
<result name="input">login.jsp</result>
<result name="success" type="redirectAction">/secure/home</result>
</action>
</package>
拦截器栈
<package name="default" extends="struts-default">
<interceptors>
<interceptor name="timer" class=".." />
<interceptor name="logger" class=".." />
<interceptor-stack name="myStack">
<interceptor-ref name="timer" />
<interceptor-ref name="logger" />
</interceptor-stack>
</interceptors>
<action name="login" class="tutuorial.Login">
<interceptor-ref name="myStack" />
<result name="input">login.jsp</result>
<result name="success" type="redirectAction">/secure/home</result>
</action>
</package>
方法过滤
MethodFilterInterceptor is an abstract Interceptor
used as a base class for interceptors that will filter execution based on method names according to specified included/excluded method lists.
Settable parameters are as follows:
- excludeMethods - method names to be excluded from interceptor processing
- includeMethods - method names to be included in interceptor processing
NOTE: If method name are available in both includeMethods and excludeMethods, it will be considered as an included method: includeMethods takes precedence over excludeMethods.
Interceptors that extends this capability include:
- TokenInterceptor
- TokenSessionStoreInterceptor
- DefaultWorkflowInterceptor
- ValidationInterceptor