org.apache.struts2.interceptor

## Class PrepareInterceptor

- java.lang.Object
- - org.apache.struts2.interceptor.AbstractInterceptor
  - - org.apache.struts2.interceptor.MethodFilterInterceptor
    - - org.apache.struts2.interceptor.PrepareInterceptor

- All Implemented Interfaces:
    Serializable, ConditionalInterceptor, Interceptor

      public class PrepareInterceptor
      extends MethodFilterInterceptor

  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.

  Note: Since XWork 2.0.2, this interceptor extends `MethodFilterInterceptor`, therefore being able to deal with excludeMethods / includeMethods parameters. See [Workflow Interceptor] (class `DefaultWorkflowInterceptor`) for documentation and examples on how to use this feature.

  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 `ActionProxy`. For example, you can specify a prepareInput() method that will be run before the invocation of the input method.

  Interceptor 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.

  Example code:

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

  Author:
    Jason Carreira, Philip Luppens, tm_jee
  See Also:
    `Preparable`, Serialized Form

- - ### Field Summary

    - ### Fields inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor

      `excludeMethods, includeMethods`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `PrepareInterceptor()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `String` | `doIntercept(ActionInvocation invocation)` Subclasses must override to implement the interceptor logic.
    `void` | `setAlwaysInvokePrepare(String alwaysInvokePrepare)` Sets if the `prepare` method should always be executed.
    `void` | `setFirstCallPrepareDo(String firstCallPrepareDo)` Sets if the `prepareDoXXX` method should be called first

    - ### Methods inherited from class org.apache.struts2.interceptor.MethodFilterInterceptor

      `applyInterceptor, getExcludeMethodsSet, getIncludeMethodsSet, intercept, setExcludeMethods, setIncludeMethods`

    - ### Methods inherited from class org.apache.struts2.interceptor.AbstractInterceptor

      `destroy, init, setDisabled, shouldIntercept`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### PrepareInterceptor

          public PrepareInterceptor()

  - ### Method Detail

    - #### setAlwaysInvokePrepare

          public void setAlwaysInvokePrepare(String alwaysInvokePrepare)

      Sets if the `prepare` method should always be executed.

      Default is `true`.

      Parameters:
        `alwaysInvokePrepare` - if `prepare` should always be executed or not.

    - #### setFirstCallPrepareDo

          public void setFirstCallPrepareDo(String firstCallPrepareDo)

      Sets if the `prepareDoXXX` method should be called first

      Default is `false` for backward compatibility

      Parameters:
        `firstCallPrepareDo` - if `prepareDoXXX` should be called first

    - #### doIntercept

          public String doIntercept(ActionInvocation invocation)
                             throws Exception

      Description copied from class: `MethodFilterInterceptor`
      Subclasses must override to implement the interceptor logic.
      Specified by:
        `doIntercept` in class `MethodFilterInterceptor`
      Parameters:
        `invocation` - the action invocation
      Returns:
        the result of invocation
      Throws:
        `Exception` - in case of any errors

Copyright © 2000–2026 Apache Software Foundation. All rights reserved.