开发者

Struts redirect with params

I'm using struts 1.2.4 but I have a need to redirect with parameters from an Action Servlet to a target page. I'm not able to upgrade to 1.2.7+ struts, so I'm not able to take advantage of ActionRedirect.

Right now the save() method in my action class returns an ActionForward

    return (mapping.findForward("success"));
}

which is mapped to this entry in struts-config.xml

        <forward 
 开发者_C百科           name="success" 
            path="/enterprise/company/searchCompany.do"/>

I'd actually like it the save() method to forward to something like '/enterprise/company/saveSurvey.do?companyID=1' -- the value of companyID is available to my action class's save method.

What is the best way to accomplish redirect with param if I'm not able to upgrade Struts to take advantage of ActionRedirect?


I see 2 variants, but I am definitely have low knowledge in that area.

First: You can save you params in Map and then in session, and after that you can load them

public ActionForward execute (HttpServletRequest request, ....){
  Map<String,String> params = new HashMap<>();
  params.put("companyId","1");
request.getSession().setAttribute("PARAM_MAP",params);
   return (mapping.findForward("success"));
}

In your other method you can get params from that map like

Map<String,String> map = (Map<String,String>)request.getSession().getAttribute("PARAM_MAP");

The second variant is easier. You can use 1 form for both Actions and in struts.xml you have to point that

<form-beans>
   <form-bean name="MyBeanForm" type="your.full.packet.path.MyBeanForm"/>
</form-beans>

<action-mappings>

  <action path="searchCompany"
          name ="MyBeanForm"
          scope="session"
          type="enterprise.company.SearchCompanyAction" />


  <forward 
            name="success" 
            path="/enterprise/company/searchCompany.do"/>

</action-mappings>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜