Struts 使用参数重定向

发布于 2024-11-27 18:45:47 字数 612 浏览 0 评论 0原文

我正在使用 struts 1.2.4,但我需要使用参数从 Action Servlet 重定向到目标页面。我无法升级到 1.2.7+ struts,因此无法利用 ActionRedirect

现在,我的操作类中的 save() 方法返回一个 ActionForward

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

,它映射到 struts-config.xml 中的此条目,

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

我实际上希望 save() 方法转发到类似 ' /enterprise/company/saveSurvey.do?companyID=1'——companyID 的值可用于我的操作类的 save 方法。

如果我无法升级 Struts 以利用 ActionRedirect,那么使用参数完成重定向的最佳方法是什么?

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 
            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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2024-12-04 18:45:47

我看到了两种变体,但我在该领域的知识肯定很低。

第一的:
您可以将参数保存在 Map 中,然后保存在会话中,然后您可以

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"));
}

在其他方法中加载它们,您可以从该映射中获取参数,例如

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

第二个变体更容易。您可以对两个操作使用 1 个表单,并且在 struts.xml 中您必须指出

<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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文