Java的Struts的1:从行动对行动转发。通过ActionForm中传递数据
题
我们一直试图从重定向一个动作到另一个,希望数据将相应ActionForm
豆之间传递。第一动作收到来自浏览器的请求时,打印数据字段,并且将其转发到另一个动作,该打印相同字段和重定向到JSP。
的问题是,ActionTo
是印刷不正确的值 - 其commonInt
具有0
的默认值,而我们预计35
下面是一个表示示例:
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");
}
}
和ActionForms被:
public class FormA extends ActionForm {
public int intA;
public int commonInt;
}
public class FormB extends ActionForm{
public int intB;
public int commonInt;
}
映射:
<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>
有没有办法做到这一点?或这两种形式应该是相同的?
我们尝试的解决方法是通过请求中传递的事情,但它可以得到大而杂乱。
解决方案
要实现这一点的方式是使用相同的ActionForm为两个操作。是否有你为什么需要两个不同的ActionForm具体原因是什么?如果没有尝试修改所述第二动作映射到名称=“备考”和动作本身使用FORMA而非FormB。
其他提示
汤姆,使用你溶液中并用组合ActionRedirect ,文森特Ramdhanie的建议,我得到了你想要的东西了。
的代码是这么简单,它让你有用于每个操作分离形式。
ActionRedirect redirect = new ActionRedirect(mapping.findForward("send"));
redirect.addParameter("commonInt", formA.getCommonInt());
return redirect;
formB.setCommonInt(request.getParameter("commonInt"));
这endend挽救了我的一天,并帮助我不要有直接在JSP改变的努力,这将是可怕的。
听起来这可能会导致混乱,我会保持这个简单而使用ActionForm
只存储Request
数据。
public class FormA extends ActionForm {
public int intA;
public int commonInt;
}
一旦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");
}
}
在理想情况下,如果你不投入巨资的Struts 1我想看看的Struts 2。
不隶属于 StackOverflow