Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use redirect and chain result types in struts2

Tags:

struts2

In my struts 2 project when using redirect action i m loosing all my values such as action error and field errors.

I looked it up on net and found 2 options

  • Chain - This isn't used much i donno why ..
  • MessageStoreInterceptor - This needs to be placed in every action

So can any one please let me know when is redirect(or RedirectAction) preferred and when is chain preferred.

like image 333
Vipin Avatar asked Jan 21 '11 06:01

Vipin


People also ask

What is type chain in struts2?

This is called Action chaining in Struts 2. One action leads to another one and so on. Request > Action 1 > Action 2 > Response In Struts 2, this can be achieved by Chain Result. The Chain Result is a result type that invokes an Action with its own Interceptor Stack and Result.

Which is default result type in struts2?

The dispatcher result type is the default type, and is used if no other result type is specified. It's used to forward to a servlet, JSP, HTML page, and so on, on the server.


2 Answers

Redirecting an action looses the current value stack (anything in request scope) you can of course set up your action to preserve these values by passing them as parameters to the next action, but it is a bit of a pain.

Chain preserves the value stack, so the next action can work on parameters created from the previous action without needing to explicitly pass them, also since there is this snow ball effect you can use all the parameters in the view.

But it is generally recognized that a top down solution (maybe top down isn't the best word... 'structured') is better than building a maze of spaghetti actions.

So when you're under pressure to get something working and not overly familiar with struts2 then use chain or redirection, and then definitely come back and fix it! In general you should use an interceptor.

In the event of an action that routes to other actions based on some condition it would be better to make that an interceptor apply that to a package and put all actions which require this interesting behavior in that package. Then it is very clear which actions this applies to.

like image 152
Quaternion Avatar answered Sep 20 '22 15:09

Quaternion


First option

<action name="remove" class="com.action.firstAction" method="remove">
         <result name="success" type="redirectAction">
            secondaction
            <param name="actionName">secondaction</param>
            <param name="namespace">/</param>
            <param name="param name">${param value}</param>
        </result>           
    </action>
<action name="secondaction" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>

Another option

<action name="remove" class="com.action.firstAction" method="remove">
     <result name="success" type="chain">secondaction</result>
</action>
<action name="second action" class="com.action.secondAction" method="result">
    <result name="success">result.jsp</result>
</action>
like image 25
user2114253 Avatar answered Sep 21 '22 15:09

user2114253