Reputation: 2467
I have a form class where I have a String array. I am setting the value for the string array. I need to print this array values one by one in a JSP page. The code I have written is:
paxList = getPaxList(id);
List<String> passengerName = new ArrayList<String>();
List<Double> passengerAge = new ArrayList<Double>();
for(int i=0; i < paxList.size(); i++){
passengerName.add(paxList.get(i).getPassengerName());
passengerAge.add(paxList.get(i).getPassengerAge());
}
bookingsForm.setPassengerName(passengerName.toArray(new String[paxList.size()]));
bookingsForm.setPassengerAge(passengerAge.toArray(new Double[paxList.size()]));
Now I need to print the values from the PassengerName
.
I tried like this in my JSP page
<logic:iterate id="currentPassName" property="passengerName" >
<bean:write name="currentPassName" />
</logic:iterate>
but this is not working for me. can someone guide me.
Upvotes: 0
Views: 2254
Reputation: 691655
You shouldn't split your list of passengers into two lists of name and age. That's what makes things difficult. Just store paxList
into your form directly.
Once this is done, do yourself a favor and use the JSTL rather than the deprecated struts logic and bean tags:
<c:forEach items="${bookingsForm.paxList}" var="passenger">
Passenger <c:out value="${passenger.name}" is aged ${passenger.age}<br/>
</c:forEach>
To explain why it doesn't work as is:
<logic:iterate id="passRecord" property="passengerName" >
<bean:write name="passengerName" property="passRecord" />
</logic:iterate>
The above iterates through the elements of the form's passengerName
array (which you should name passengerNames
, since there are several of them), and defines a page attribute named passRecord
that you must use inside the loop.
Since passengerName
is a String array, passRecord
is a String. And inside the loop you're trying to access the property passRecord
of passengerName
. There is no method getPassRecord()
in String[]
. The passenger name is stored in passRecord
. You just need to write it. With properly named variables, it would be much clearer:
<logic:iterate id="currentPassengerName" property="passengerNames" >
<bean:write name="currentPassengerName" />
</logic:iterate>
Upvotes: 2
Reputation: 47280
With jstl you could do it somethign like this :
<table>
<c:forEach var = "oneName" items = "${passengerName}" >
<tr><td> ${oneName}</td</tr>
</c:forEach>
</table>
Upvotes: 0