简体   繁体   English

如何将对象列表从jsp发送到struts2动作类?

[英]How to send a list of object from jsp to struts2 action class?

I am new to Struts2. 我是Struts2的新手。 I have a JSP page which shows a list of Rooms (where Room is a user-defined class). 我有一个JSP页面,其中显示了Rooms列表(其中Room是用户定义的类)。 I need to send this entire list to the action class as a hidden field. 我需要将整个列表作为隐藏字段发送到动作类。 The JSP code is as follows: JSP代码如下:

<form method="GET" action="reporting.action" >  
  <s:hidden name="roomsReport" value="%{allRooms}"/>
  <s:textfield name="roomsR" value="%{allRooms}"/>                       
  <s:submit name="action" style="width:220px;" value="Generate Report for Rooms" /> 
</form>

The textfield (used for testing) shows the address for the list (implying that its not null in the jsp page) I am still unable to access it in my ReportingAction class using the following code: 文本字段(用于测试)显示列表的地址(暗示其在jsp页面中不为null),我仍然无法使用以下代码在ReportingAction类中访问它:

System.out.println("xxx"+this.roomsReport.size());
System.out.println(this.roomsReport);

Both above print statements give 0 and [] respectively. 以上两个打印语句分别给出0和[]。 I have the getters and setters for roomsReport as follows: 我有roomReport的getter和setter方法,如下所示:

private List<Room> roomsReport;
public List<Room> getRoomsReport() {
  return roomsReport;
}

public void setRoomsReport(List<Room> roomsReport) {
  this.roomsReport = roomsReport;
}

Can anyone help ? 有人可以帮忙吗?

Finally I could pass the list to the jsp using session 最后我可以使用会话将列表传递给jsp

The list is set in a session variable in jsp as follows: 该列表在jsp的会话变量中设置,如下所示:

 <s:set name="roomsReport" value="%{allRooms}" scope="session" /> 

In the action class I can access it using the following code: 在动作类中,我可以使用以下代码进行访问:

List<Room> roomsReport = (List<Room>)ActionContext.getContext().getSession().get("roomsReport");

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

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