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.
You shouldn’t split your list of passengers into two lists of name and age. That’s what makes things difficult. Just store
paxListinto your form directly.Once this is done, do yourself a favor and use the JSTL rather than the deprecated struts logic and bean tags:
To explain why it doesn’t work as is:
The above iterates through the elements of the form’s
passengerNamearray (which you should namepassengerNames, since there are several of them), and defines a page attribute namedpassRecordthat you must use inside the loop.Since
passengerNameis a String array,passRecordis a String. And inside the loop you’re trying to access the propertypassRecordofpassengerName. There is no methodgetPassRecord()inString[]. The passenger name is stored inpassRecord. You just need to write it. With properly named variables, it would be much clearer: