Java Struts 1: forward from action to action. Passing data through ActionForms

不羁的心 提交于 2019-12-03 11:44:39

The way to accomplish this is to use the same actionform for both actions. Is there a specific reason why you need two different actionforms? If not try modifying the second action mapping to name="formA" and the action itself to use FormA rather than FormB.

lucasarruda

Tom, using you solution and combining with ActionRedirect, suggested by Vincent Ramdhanie, I got what you wanted too.

The code is simple as that and it allow you to have separated Forms for each Action.

ActionRedirect redirect = new ActionRedirect(mapping.findForward("send"));
redirect.addParameter("commonInt", formA.getCommonInt());
return redirect;
formB.setCommonInt(request.getParameter("commonInt"));

This endend up saving my day and helping me not to have the effort to change that directly in the JSP, what would be awful.

Jon

Sounds like this could get messy, I'd keep this simple and use the ActionForm only to store Request data.

public class FormA extends ActionForm {
    public int intA;
    public int commonInt;
}

Once the Request has been submitted take the data out of the ActionForm and stuff it into the session somewhere, either directly, or into a data holder within the session to maintain this kind of information.

public class ActionTo extends DispatchableAction {
  public ActionForward recv(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response) {
    FormA form = (FormA)form;

    DataHolder dataHolder = request.getSession().getAttribute("dataHolder");
    dataHolder.setCommonInt(commonInt);
    dataHolder.setIntA(intA);

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

Ideally, if you're not heavily invested in Struts 1 I'd take a look at Struts 2.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!