Java Struts 1:从动作到动作。通过 ActionForms 传递数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/849051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Struts 1: forward from action to action. Passing data through ActionForms
提问by Tom
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.
我们一直在尝试从一个动作重定向到另一个动作,希望数据能够在相应的ActionForm
bean之间传递。第一个动作接收来自浏览器的请求,打印一个数据字段,并将其转发给另一个动作,后者打印相同的字段并重定向到 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.
我们尝试的解决方法是通过请求传递内容,但它可能会变得庞大而混乱。
采纳答案by Vincent Ramdhanie
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.
实现这一点的方法是对两个动作使用相同的动作形式。您需要两种不同的动作形式是否有特定的原因?如果不是,请尝试将第二个动作映射修改为 name="formA" 并将动作本身修改为使用 FormA 而不是 FormB。
回答by Jon
Sounds like this could get messy, I'd keep this simple and use the ActionForm
only to store Request
data.
听起来这可能会变得混乱,我会保持简单并使用ActionForm
only 来存储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。
回答by lucasarruda
Tom, using you solution and combining with ActionRedirect, suggested by Vincent Ramdhanie, I got what you wanted too.
Tom,使用您的解决方案并结合Vincent Ramdhanie 建议的ActionRedirect,我也得到了您想要的。
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.
这最终节省了我的时间并帮助我不必努力直接在 JSP 中更改它,这会很糟糕。