Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment:

Update nomenclature and formatting

Description

Interceptors allow you to define code to be executed before and/or after the execution of an action. Action method. (The "Filter" pattern.) Interceptors can be a powerful tool when writing web applications. Some of the most common implementations of an Interceptor might be:

  • Security Checking (ensuring the user is logged in)
  • Trace Logging (logging every action)
  • Bottleneck Checking (start a timer before and after every action, to check bottlenecks in your application)
    You can also chain Interceptors together to create an interceptor stack. If you wanted to do a login check, security check, and logging all before an Action call, this could easily be done with an interceptor package.

developing applications. There are many, many use cases for Interceptors, including validation, property population, security, logging, and 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 needed 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. Intercepters must first be defined (to give name them) and can be chained together as a stack:

Code Block
xml
xml
titleRegistering Interceptors
<interceptors>
  <interceptor name="security" class="com.mycompany.security.SecurityInterceptor"/>
  <interceptor-stack name="defaultComponentStacksecureStack">
    <interceptor-ref name="componentsecurity"/>
    <interceptor-ref name="defaultStack"/>
  </interceptor-stack>
</interceptors>

(tick) 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. To use them in your actions:

Code Block
xml
xml
titleA local Interceptor Stack
<action name="VelocityCounter" class="com.opensymphony.webwork.example.counter.SimpleCounter">
   <result name="success">...</result>
   <interceptor-ref name="defaultComponentStack"/>
</action>

NOTE: Reference name can be either the name of the interceptor or the name of a stack

For more details, see Interceptors reference.

The default configuration (action-default.xml) sets up a default Interceptor Stack that will work well for most applications.

(lightbulb) For more, see Interceptors.Most of the content here provided by Matt Dowell <matt.dowell@notiva.com>