简体   繁体   English

Java Struts 1:从动作转向动作。通过ActionForms传递数据

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

We've been trying to redirect from one action to another, hoping that data would be passed between corresponding ActionForm beans. 我们一直在尝试从一个动作重定向到另一个动作,希望数据将在相应的ActionForm bean之间传递。 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. 第一个操作接收来自浏览器的请求,打印数据字段,并将其转发到另一个操作,该操作打印相同的字段并重定向到JSP。

The problem is that ActionTo is printing an incorrect value - its commonInt has a default value of 0 , while we expect 35 . 问题是ActionTo打印的值不正确 - 它的commonInt默认值为0 ,而我们预计值为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: 而actionForms是:

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. 我们尝试的解决方法是通过请求传递内容,但它可能变得庞大而混乱。

Tom, using you solution and combining with ActionRedirect , suggested by Vincent Ramdhanie, I got what you wanted too. 汤姆,使用你的解决方案并结合ActionRedirect ,由Vincent Ramdhanie建议,我也得到了你想要的东西。

The code is simple as that and it allow you to have separated Forms for each Action. 代码很简单,它允许您为每个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. 这样可以节省我的时间并帮助我不要直接在JSP中进行更改,这将是非常糟糕的。

The way to accomplish this is to use the same actionform for both actions. 实现此目的的方法是对两个操作使用相同的actionform。 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. 如果没有尝试修改第二个动作映射到name =“formA”,并且动作本身使用FormA而不是FormB。

Sounds like this could get messy, I'd keep this simple and use the ActionForm only to store Request data. 听起来这可能会变得混乱,我会保持这个简单,只使用ActionForm来存储Request数据。

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. 提交Request将数据从ActionForm取出,然后直接将其填入会话中,或者将其填充到会话中的数据持有者中以维护此类信息。

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. 理想情况下,如果你没有大量投资Struts 1,我会看一下Struts 2。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM