Interceptors allow you to define code to be executed before and/or
after the execution of an Action method. (The "Filter" pattern.)
Interceptors can be a powerful tool when developing applications. There
are many, many use cases for Interceptors, including validation,
property population, security, logging, and profiling.
Interceptors 允许你定义代码在Action方法前或后来执行。当发布应用的时候,Interceptor是个强大的工具,它们能被用在很多很多的案例下,包括validation,peroperty population,security,logging,和 profiling。
Validation |
Examine input for correctness |
Property Population |
Transfer and convert input to object properties |
Logging |
Journal details regarding each action |
Profiling |
Time action throughput, looking for performance bottlenecks |
Interceptors can be chained together to create an Interceptor
"Stack". If an action neeeds to check the client's credentials, log the
action, and time the action, all of these routines, and more, could be
made part of the same Interceptor Stack.
Interceptors are implemented as Java classes, and so each
Interceptor has a unique class name. To make it easier to reference
Interceptors, each class can be registered with the framework and given
a simpler name.
<interceptors>
<interceptor name="security" class="com.company.security.SecurityInterceptor"/>
<interceptor-stack name="secureStack">
<interceptor-ref name="security"/>
<interceptor-ref name="defaultStack"/>
</interceptor-stack>
</interceptors>
Individual Interceptors and Interceptors Stacks can be "mixed and
matched" in any order when defining an Interceptor Stack. The framework
will invoke each Interceptor on the stack in the order it is defined.
Most applications will define a default Interceptor Stack,
<default-interceptor-ref name="secureStack"/>
but each action can also define it's own local stack.
<action name="VelocityCounter" class="org.apache.struts2.example.counter.SimpleCounter">
<result name="success">...</result>
<interceptor-ref name="defaultComponentStack"/>
</action>
The default configuration (struts-default.xml) sets up a default Interceptor Stack that will work well for most applications.
For more, see Interceptors.