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

3

15

We've been trying to redirect from one action to another, hoping that data would be passed between corresponding ActionForm beans. The first action receives a request from the browser, prints a data field, and forwards it to another action, which prints the same field and redirects to a JSP.

The problem is that ActionTo is printing an incorrect value - its commonInt has a default value of 0, while we expect 35.

Here is a representing example:

public class ActionFrom extends DispatchableAction{

    public ActionForward send(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response){
        FormA formA = (FormA)form;

        formA.commonInt = 35;
        System.out.println("sent: "+formA.commonInt);
        return mapping.findForward("send");
    }
}

public class ActionTo extends DispatchableAction{

    public ActionForward recv(ActionMapping mapping, ActionForm form, HttpServletRequest request,HttpServletResponse response){
        FormB formB = (FormB)form;

        System.out.println("recv= "+formB.commonInt);

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

And actionForms are:

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

public class FormB extends ActionForm{
    public int intB;
    public int commonInt;
}

Mappings:

<action path="/from" type="EXPERIMENT.ActionFrom" name="formA" scope="request"
      input="something.jsp" parameter="dispatch" unknown="false" validate="false">
 <forward  name="send" path="/to.do?dispatch=recv" redirect="false"/>
</action>

 <action path="/to" type="EXPERIMENT.ActionTo" name="formB"  scope="request"
      input="something.jsp" parameter="dispatch" unknown="false" validate="false">
      <forward name="send" path="/login.do"  redirect="false"/>
 </action>

Is there a way to accomplish this? Or both forms should be the same?

The workaround we tried was to pass things through request, but it can get large and messy.

Excoriation answered 11/5, 2009 at 17:5 Comment(0)
R
6

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.

Reconstruct answered 11/5, 2009 at 17:12 Comment(2)
The 'thing' here is that we are trying to add some modules to existing functionality. Both forms are quite large, and dont have many fields in common. We wanted to keep things tidy and not create a very large form.Excoriation
In that case you probably want to look at Actionredirect class then. You can add parameters to be sent to the next action. Since struts 1.2.7.Reconstruct
G
13

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.

Gayla answered 18/2, 2010 at 21:24 Comment(0)
R
6

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.

Reconstruct answered 11/5, 2009 at 17:12 Comment(2)
The 'thing' here is that we are trying to add some modules to existing functionality. Both forms are quite large, and dont have many fields in common. We wanted to keep things tidy and not create a very large form.Excoriation
In that case you probably want to look at Actionredirect class then. You can add parameters to be sent to the next action. Since struts 1.2.7.Reconstruct
D
0

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.

Donitadonjon answered 27/5, 2009 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.