2

I have a scriptlet for-loop that displays movie schedules from a List<Schedule>.

<%
    @SuppressWarnings("unchecked")
    List<Schedule> scheduleList = 
        (ArrayList<Schedule>) session.getAttribute("scheduleList");

    for (int ctr = 0; ctr < scheduleList.size(); ctr++) {
%>

<tr>
    <td class="center">
        <input type="radio" name="selection" value="<%=ctr%>" required title="Please select a schedule." />
    </td>
    <td><%=scheduleList.get(ctr).getMallId()%></td>
    <td class="center"><%=scheduleList.get(ctr).getScheduleCinema()%></td>
    <td>PHP <%=scheduleList.get(ctr).getSchedulePrice()%></td>
    <td><%=scheduleList.get(ctr).getScheduleDate()%></td>
    <td><%=scheduleList.get(ctr).getScheduleTime()%></td>
    <td class="center"><%=scheduleList.get(ctr).getScheduleSeats()%></td>
</tr>

<%
    }
%>

Here is the Schedule bean object for reference:

public class Schedule {

    private int scheduleId;
    private int movieId;
    private String mallId;
    private int scheduleCinema;
    private BigDecimal schedulePrice;
    private Date scheduleDate;
    private Time scheduleTime;
    private int scheduleSeats;

    // getters and setters
}

I have managed to convert it to JSTL with my desired number of iterations (related thread).

<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
<tr>
    <td class="center">
        <input type="radio" name="selection" value="${ctr}" required title="Please select a schedule." />
    </td>
    <td>${schedule.mallId}</td>
    <td>${schedule.scheduleCinema}</td>
    <td>${schedule.schedulePrice}</td>
    <td>${schedule.scheduleDate}</td>
    <td>${schedule.scheduleTime}</td>
    <td>${schedule.scheduleSeats}</td>
</tr>
</c:forEach>

Output:

enter image description here

Firefox Inspector:

<tr>

    <td class="center">
        <input type="radio" title="Please select a schedule." required="" value="0" name="selection"></input>
    </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>

</tr>
<tr>

    <td class="center">
        <input type="radio" title="Please select a schedule." required="" value="1" name="selection"></input>
    </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>

</tr>

Unfortunately, the schedule details are not displaying.
How do I display the data of each Schedule element from the List<Schedule> in JSTL?

(The ctr is important by the way, its value will be needed in the next action.)

Community
  • 1
  • 1
k_rollo
  • 5,304
  • 16
  • 63
  • 95

2 Answers2

3

Instead of

<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">

Use:

<c:forEach var="schedule" items="${scheduleList}" varStatus="ctr">
<tr>
<td class="center">
    <input type="radio" name="selection" value="${ctr.index}" required title="Please select a schedule." />
</td>

For ctr you could maintain separate counter variable and increment within your forEach.

SMA
  • 36,381
  • 8
  • 49
  • 73
  • Hi, I've been using this approach for my other JSTL iterations, but this one particularly needs a `ctr`. Could you kindly update your answer to show what you meant with the `ctr` variable? I'm particularly new to JSTL. – k_rollo Nov 27 '14 at 08:11
  • Minor typo by the way, `var="schedule"items="${scheduleList}"` needs whitespace. :) – k_rollo Nov 27 '14 at 08:42
  • 1
    Thanks for pointing it out. You are right. I have just modified and corrected the typo. – SMA Nov 27 '14 at 09:03
1

You can use varStatus as well as var variable in c:forEach

varStatus="loop"

and use it as ${loop.index}

e.g. see the example

Community
  • 1
  • 1
StanislavL
  • 56,971
  • 9
  • 68
  • 98
  • Thanks, I believe [this](http://stackoverflow.com/questions/6600738/use-jstl-foreach-loops-varstatus-as-an-id) post is also helpful for anyone reading this thread. – k_rollo Nov 27 '14 at 13:33