Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action redirect in struts.xml

Tags:

struts2

Can I redirect to another action from within a struts actions ? So the result of an action is another action i.e - here is a snippet of struts.xml

    <action name="newRedirect" >
        <result>formsearch</result>
    </action>

    <action name="formsearch" class="com.event.action.SearchForm"
    method="execute">
        <result name="success">/form.jsp</result>
    </action>

Thanks

like image 368
blue-sky Avatar asked Feb 10 '11 11:02

blue-sky


1 Answers

Yes. You can redirect and you can chain. Redirect starts from scratch, it is like you called the other action for the first time while chain keeps the values on the value stack and adds the variables of the new action.

To forward:

<action name="newRedirect" >
    <result type="redirect">/formsearch.action</result>
</action>

To chain:

<action name="newRedirect" >
    <result type="chain">formsearch</result>
</action>

As a convenience the redirect result type can be changed to a "redirectAction" result type... which lets us write:

 <action name="newRedirect" >
    <result type="redirectAction">formsearch</result>
</action>

This last one is probably what you want.

Now a warning, chaining/action redirection is up there with the "goto" statement. Not evil but easy to abuse, you should probably look to moving the deciding logic (the logic that determines what action to call of several to an interceptor) or if the logic is mostly setup related then some type of utility class that is invoked by the actions prepare method (or into the prepare method outright)... If the action needs parameters before prepare is called then use the paramsPrepareParamsStack.

like image 190
Quaternion Avatar answered Oct 01 '22 09:10

Quaternion