javadata-bindingstruts2interceptorinterceptorstack

Changing parameters after bind in Struts 2


I have an action that receives some parameters from user (e.g. date). This action produces many different reports, so it has many different methods. I need to tune those parameters (set a time to midnight) before every method. The prepare method is executed before parameters are bound. Is there any other interceptor or any other convention that allows me to do that?


Solution

  • Use the <interceptor-ref name="paramsPrepareParamsStack"/>

    <!-- An example of the params-prepare-params trick. This stack
        is exactly the same as the defaultStack, except that it
        includes one extra interceptor before the prepare interceptor:
        the params interceptor.
    
        This is useful for when you wish to apply parameters directly
         to an object that you wish to load externally (such as a DAO
         or database or service layer), but can't load that object
         until at least the ID parameter has been loaded. By loading
         the parameters twice, you can retrieve the object in the
         prepare() method, allowing the second params interceptor to
         apply the values on the object. -->
    

    If you are using the Convention Plugin apply it on action

    @Action(value="action1", interceptorRefs=@InterceptorRef("paramsPrepareParamsStack"))