Wednesday, April 13, 2011

Struts Tips

Hi Friends ,
I was working on one of my j2ee project . From there I want to share few small but usefull tips for struts.In struts many time we caught in wrong side for duplicate page submission. I am going to share how to tackle that
and how to pass parameter once redirect is done, one way is there to put parameters in session but it is messy.
Manage Redirect:
It is quite simple we need to add one attribute redirect="true" in struts-config.xml
e.g.
<action path="{Action-Path}" type="{Action}" name="{Form-Bean}">

<forward name="{Forward-Name}" path="{Forward-Path}" redirect="true"/>
</action>
Pass Parameter After Redirect:
In action file in place of using mapping.findForward("{Action-Path}") use ActionRedirect (subclass of ActionForward).This class includes an addParameter() method.
e.g.
ActionRedirect ar = new ActionRedirect(mapping.findForward("{Action-Path}"));
ar.addParameter("{Parameter-Name}", {Parameter-Value});
return ar;
Or
return (new ActionRedirect(mapping.findForward("{Action-Path}"))).addParameter("{Parameter-Name}", "{Parameter-Value}");
Here all words with in {} are place holders.