When to use redirect and chain result types in 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.
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.
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>
精彩评论