Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Interceptors > Prepare Interceptor
Added by digi9ten, last edited by Philip Luppens on Feb 10, 2007  (view change) show comment

This interceptor calls prepare() on actions which implement Preparable. This interceptor is very useful for any situation where you need to ensure some logic runs before the actual execute method runs.

A typical use of this is to run some logic to load an object from the database so that when parameters are set they can be set on this object. For example, suppose you have a User object with two properties: id and name. Provided that the params interceptor is called twice (once before and once after this interceptor), you can load the User object using the id property, and then when the second params interceptor is called the parameter user.name will be set, as desired, on the actual object loaded from the database. See the example for more info.

Update: Added logic to execute a prepare{MethodName} and conditionally the a general prepare() Method, depending on the 'alwaysInvokePrepare' parameter/property which is by default true. This allows us to run some logic based on the method name we specify in the com.opensymphony.xwork2.ActionProxy. For example, you can specify a prepareInput() method that will be run before the invocation of the input method.

In PrepareInterceptor

Applies only when action implements Preparable

  1. if the action class have prepare{MethodName}(), it will be invoked
  2. else if the action class have prepareDo(MethodName()}(), it will be invoked
  3. no matter if 1] or 2] is performed, if alwaysinvokePrepare property of the interceptor is "true" (which is by default "true"), prepare() will be invoked.

Parameters

  • alwaysInvokePrepare - Default to true. If true, prepare will always be invoked, otherwise it will not.

Extending the Interceptor

There are no known extension points to this interceptor.

Examples

<!-- Calls the params interceptor twice, allowing you to
      pre-load data for the second time parameters are set -->
 <action name=&quot;someAction&quot; class=&quot;com.examples.SomeAction&quot;>
     <interceptor-ref name=&quot;params&quot;/>
     <interceptor-ref name=&quot;prepare&quot;/>
     <interceptor-ref name=&quot;basicStack&quot;/>
     <result name=&quot;success&quot;>good_result.ftl</result>
 </action>