Passing a Java object from one Struts action to another

时光怂恿深爱的人放手 提交于 2019-12-24 05:32:24

问题


In one of my Struts action I've got the following code in a method:

  ...
  List<Object> retrievedListOfObjects = c.getListOfObjects();
  return mapping.findForward("view");
}

fw_view leads to a new Struts action with another Struts form. Let's say this form has got among others the following field

List<Object> listOfObjects;

I now want to pass the retrievedListOfObjects from within the first Struts action to the form of the following Struts action.

Is this possible without storing it in the session?


回答1:


you can store it as a request attribute.

request.setAttribute("listOfObjects", listOfObjects);

and then in the Action that is forwarded to

List<Object> listOfObjects = (List<Object>)request.getAttribute("listOfObjects");

Given that when setting request attributes you can give them meaningful names, you should consider setting many attributes rather than setting one big list of objects.




回答2:


Correction of krock code.

Setting object to request:

request.setAttribute("listOfObjects", listOfObjects);

Getting the object in an other action.

List<Object> listOfObjects = (List<Object>)request.getAttribute("listOfObjects");


来源:https://stackoverflow.com/questions/3052273/passing-a-java-object-from-one-struts-action-to-another

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!