Monday, September 13, 2010

Pass parameters to Struts forward

You can add  parameters in the code execute(..) method in Action class, before the request is forwarded desired location as declared in struts-config.xml.
The mapping in struts-config.xml file is like this...

<action path="/TheAction" type="com.Actions.TestAction">
    <forward name="success" path="/View.jsp"/>
    </action>

And then in the execute(..) if we want to add parameter called "id" to the request it can do like this...

 public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception {

StringBuffer path=new StringBuffer((mapping.findForward("success")).getPath());
        String id="123"
        path.append("?ID="+id);
        return new ActionForward(path.toString());


}
 

No comments:

Post a Comment