0

I am new to Struts2. I have a JSP page which shows a list of Rooms (where Room is a user-defined class). I need to send this entire list to the action class as a hidden field. The JSP code is as follows:

<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:

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

Both above print statements give 0 and [] respectively. I have the getters and setters for roomsReport as follows:

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

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

Can anyone help ?

Roman C
  • 49,761
  • 33
  • 66
  • 176
Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53
  • try to print the value using getters as struts action sets values directly in to setters – Divya Mar 25 '14 at 06:22
  • ya tried using getRoomsReport().size(); Still giving the same empty set. It seems that the getters are not even returning null (else it would give a null pointer exception). They are returning an empty set. – Crusaderpyro Mar 25 '14 at 06:41
  • Also I have kept the method as "GET" and hence I can see the parameter allRooms being sent in the URL. Just can't access it ! – Crusaderpyro Mar 25 '14 at 06:44
  • some times it happens in struts that values not get set.so you can instead use normal buttons.instead of struts ,also to pass hidden field you can use input type="hidden" . – Divya Mar 25 '14 at 06:44
  • 1
    @A5l-lE5: Values are not being set when someone does something wrong. – Aleksandr M Mar 25 '14 at 08:21
  • 1
    @Crusaderpyro: You cannot just pass whole object in hidden field. – Aleksandr M Mar 25 '14 at 08:23
  • 1
    The answer is: use session. – Andrea Ligios Mar 25 '14 at 09:25
  • List of object separated by a comma? What do you mean by list of objects? Struts2 support creating of objects during request but this technique is prohibited via security patches by the recent fixes. – Roman C Mar 25 '14 at 10:02
  • @RomanC: Do you have a link to release notes or jira issues about restrictions of object creation? – Aleksandr M Mar 25 '14 at 10:12
  • @AleksandrM What do you ask me could you explain? If you have a question you could post it to this tag, and I have already answers to that question. Upvote them as well. – Roman C Mar 25 '14 at 10:18
  • @RomanC - By List of object i meant List of User-defined objects i.e ArrayList in this case. So I guess the only option to send the object using session ! – Crusaderpyro Mar 25 '14 at 10:42
  • @Crusaderpyro I see that you have provided the field in the action class, but you didn't provide those objects in JSP. If you want to have an option to send *object* you should tell how do you want to send it in the request or in the response. The second case is meant if you talk about a session. – Roman C Mar 25 '14 at 10:52
  • Of course S2 still supports object creation during requests; that's how the entire framework works. – Dave Newton Mar 25 '14 at 13:41
  • So right now I was trying to set the list in jsp as a session variable as and trying to access it in the action class using roomsReport = (List)ActionContext.getContext().getSession().get(roomsReport); which is giving a null pointer exception ! – Crusaderpyro Mar 25 '14 at 15:36
  • ok sorry I missed the quotes in the action class for roomsReport. Its working fine now. Thanks all – Crusaderpyro Mar 25 '14 at 15:39
  • @Crusaderpyro, do we have any other way other than using session object ? – unknown Oct 08 '21 at 12:30

1 Answers1

0

Finally I could pass the list to the jsp using session

The list is set in a session variable in jsp as follows:

 <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");
Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53