1

I have defined a list collection in my servlet as following:

List<BookingRecord> list = getLast24();
req.setAttribute("records", list);

Then in JSP, I want to do following algorithm using jstl. If list is not empty, do XXXX, otherwise do YYYY.

I did something like this:

<c:when test="${not empty records}">
<table border="1">
    <tbody>
    <tr>
        <th>id</th>
        <th>hotel</th>
        <th>room</th>
        <th>guest id</th>
        <th>start date</th>
        <th>end date</th>
        <th></th>
    </tr>
    <c:forEach items="${records}" var="record">
        <tr<c:if test="${record.endDate != null }"> style="background-color: gainsboro"</c:if>>
            <td>${record.id}</td>
            <td>${record.hotelName}</td>
            <td>${record.roomNumber}</td>
            <td>${record.personId}</td>
            <td><fmt:formatDate value="${record.startDate}" pattern="yyyy-MM-dd HH:mm"/></td>
            <td><fmt:formatDate value="${record.endDate}" pattern="yyyy-MM-dd HH:mm"/></td>
            <td>
                <c:if test="${record.endDate == null }">
                    <form action="checkout" method="post">
                        <input type="hidden" name="recordId" value="${record.id}"/>
                        <input type="submit" value="退房"/>
                    </form>
                </c:if>
            </td>
        </tr>
    </c:forEach>
    </tbody>
</table>
</c:when>
<c:otherwise>
<h1>目前24小时内,没有订房</h1>
</c:otherwise>

But the tomcat gave me exception at

<c:when test="${not empty records}">

I appreciate if someone could help me out.

user3014926
  • 923
  • 7
  • 15
  • 25

4 Answers4

2
<c:choose>
    <c:when test="${not empty propertyValue}">
        <option value="${fonts}" selected="selected">${fonts}</option>          
    </c:when>
    <c:otherwise>
        <option value="${fonts}">${fonts}</option>
    </c:otherwise>
</c:choose> 
Ashish Vaghasiya
  • 580
  • 5
  • 19
1

Replace the when in your jstl tag to if.

<c:if test="${not empty records}">

Anthony Lo
  • 11
  • 1
1

Please see the syntax for choose which is like a switch case or use if you want if statement

<c:choose>
    <c:when test="${salary <= 0}">
       Salary is very low to survive.
    </c:when>
    <c:when test="${salary > 1000}">
        Salary is very good.
    </c:when>
    <c:otherwise>
        No comment sir...
    </c:otherwise>
</c:choose>

Also please refer this for jstl tags:

M4ver1k
  • 1,395
  • 1
  • 13
  • 26
-1

wrap your code with c:choose tag. It should do