0

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.

2 Answers 2

2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

@ Nizet, i can't use JSTL here. My scenerio is bit complicated now. I should provide a input field for passenger name if there is no record and if there is a record then it should be written into the same field. I'm tried this way
<logic:notEmpty name="passRecord" property="passengerList"> <logic:iterate id="passRecord" property="passengerList" type="com.abhibus.oprs.pojo.booking.PassengerInformation" > <bean:write name="passRecord" property="passengerName" > </logic:iterate> </logic:notEmpty> <logic:empty name="passRecord" property="passengerList"> <input type="text" name="passengerName" id="passengerName<%= (RequestType + i) %>" maxlength="150" size="15" class="requiredfield"/> </logic:empty> but still not able to find the right solution?
0

With jstl you could do it somethign like this :

<table>
    <c:forEach var = "oneName" items = "${passengerName}" >
    <tr><td> ${oneName}</td</tr>
    </c:forEach>
</table>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.