Struts 1:我如何在Actions之间传递数据?

时间:2012-08-31 13:11:52

标签: java jsp struts

我正在尝试在2 Action个类之间传递数据。我现在正在做这个

在我的第一个Action类

doExecute(){
         request.setAttribute("Order_ID", 2);
         // code to find forward to next Action class
}

在以下Action类

doExecute(){
     Object id = request.getAttribute("Order_ID");
     // code to process id
}

但是,getAttribute()方法始终返回null。我如何在两个相互跟随的动作表单之间传递数据?

提前致谢

2 个答案:

答案 0 :(得分:2)

有很多方法,你可以这样做。

方法1:

在会话中设置属性,然后返回值,然后将其从会话中删除。

doExecute(){

 HttpSssion session=request.getSession();
 session.setAttribute("Order_ID", 2);
}

然后将其退回。

doExecute(){

 HttpSssion session=request.getSession();
 Object id =session.getAttribute("Order_ID");
 session.removeAttribute("Order_ID");
}

方法2:

传递url中的值,然后从其他操作中恢复,但这里需要记住,您的请求不会丢失。

有关详情,请参阅以下链接。

http://www.apl.jhu.edu/~hall/java/Servlet-Tutorial/Servlet-Tutorial-Session-Tracking.html

答案 1 :(得分:0)

在你处于会话模式,而不是保存你想要在actionForm中传输的数据,那么你不需要setAttribute / getAttribute。