JSTL c:choose 和多个 c:forEach、c:if 问题
JSTL c:choose and mutiple c:forEach, c:if issue
我正在尝试在 <c:choose>
中使用 <c:forEach>
和 <c:if>
。我最初的计划是当列表大小为 0 时显示 "there is no result",如果不是,则显示列表中的内容。但是,似乎每当我使用 <c:choose>
时,页面都无法正确加载,甚至 ${param}
也无法捕获参数。
这是我的代码。第一部分是我使用 ${param} 的地方,它在没有 <c:choose>
.
的情况下完美运行
<select class="myselect" id="type_size" name="type_size">
<c:if test="${param.type_size eq null}">
<c:forEach var="i" begin="2" end="6" step="2">
<option value="${i}">${i}</option>
</c:forEach>
</c:if>
<c:if test="${param.type_size ne null}">
<option value=" ${param.type_size}"> ${param.type_size}</option>
</c:if>
</select>
这里是代码 <c:choose>
<c:forEach>
等等
<div class="container>
<c:if test="${item[0].getRoom_no eq null}">
<div class="row_room">
<div class="item">
There is no result available at this moment.
</div>
</div>
</c:if>
<c:if test="${item[0].getRoom_no ne null}">
<c:forEach var="tmp" items="${item}" varStatus="idx">
<c:if test="${idx.index%3==0}">
<div class="row">
</c:if>
<div class="item">
<div class="media">
<img src="resources/room_img/${tmp.getRoom_img01()}"/>
</div><!-- media end -->
<div class="row">
</c:if>
<h4>${tmp.getRoom_no()} | ${tmp.getType_name()} </h4>
<button type="button" onclick="location.href='roomView?checkin=${param.checkin}&checkout=${param.checkout}&type_size=${param.type_size}&room_no=${tmp.getRoom_no()}'">reserve</button>
</div><!-- row end -->
<c:if test="${idx.index%3 == 2 || idx.index == end}">
</div>
</div><!-- row end -->
</c:if>
</c:forEach>
</c:otherwise>
</c:if>
</c:choose>
</div><!-- container end -->
我发现了一些语法错误:
- 缺少结束引号:
<div class="container>
应该是 <div class="container">
。
- 我没有看到
<c:choose>
和 <c:otherwise>
的任何开始标签。 <c:when>
也不见了。
choose-when-otherwise 的基本构造是:
<c:choose>
<c:when test="${something eq 0}">
something is zero
</c:when>
<c:when test="${something gt 0}">
something is positive
</c:when>
<c:otherwise>
something is negative
</c:otherwise>
</c:choose>
可能还有更多错误,但由于标签错误我看不到 indents/format。
这也意味着这种形式的代码不可维护。
考虑将 if-else 逻辑移动到 servlet/class 并让 jsp 只显示数据。
我正在尝试在 <c:choose>
中使用 <c:forEach>
和 <c:if>
。我最初的计划是当列表大小为 0 时显示 "there is no result",如果不是,则显示列表中的内容。但是,似乎每当我使用 <c:choose>
时,页面都无法正确加载,甚至 ${param}
也无法捕获参数。
这是我的代码。第一部分是我使用 ${param} 的地方,它在没有 <c:choose>
.
<select class="myselect" id="type_size" name="type_size">
<c:if test="${param.type_size eq null}">
<c:forEach var="i" begin="2" end="6" step="2">
<option value="${i}">${i}</option>
</c:forEach>
</c:if>
<c:if test="${param.type_size ne null}">
<option value=" ${param.type_size}"> ${param.type_size}</option>
</c:if>
</select>
这里是代码 <c:choose>
<c:forEach>
等等
<div class="container>
<c:if test="${item[0].getRoom_no eq null}">
<div class="row_room">
<div class="item">
There is no result available at this moment.
</div>
</div>
</c:if>
<c:if test="${item[0].getRoom_no ne null}">
<c:forEach var="tmp" items="${item}" varStatus="idx">
<c:if test="${idx.index%3==0}">
<div class="row">
</c:if>
<div class="item">
<div class="media">
<img src="resources/room_img/${tmp.getRoom_img01()}"/>
</div><!-- media end -->
<div class="row">
</c:if>
<h4>${tmp.getRoom_no()} | ${tmp.getType_name()} </h4>
<button type="button" onclick="location.href='roomView?checkin=${param.checkin}&checkout=${param.checkout}&type_size=${param.type_size}&room_no=${tmp.getRoom_no()}'">reserve</button>
</div><!-- row end -->
<c:if test="${idx.index%3 == 2 || idx.index == end}">
</div>
</div><!-- row end -->
</c:if>
</c:forEach>
</c:otherwise>
</c:if>
</c:choose>
</div><!-- container end -->
我发现了一些语法错误:
- 缺少结束引号:
<div class="container>
应该是<div class="container">
。 - 我没有看到
<c:choose>
和<c:otherwise>
的任何开始标签。<c:when>
也不见了。
choose-when-otherwise 的基本构造是:
<c:choose>
<c:when test="${something eq 0}">
something is zero
</c:when>
<c:when test="${something gt 0}">
something is positive
</c:when>
<c:otherwise>
something is negative
</c:otherwise>
</c:choose>
可能还有更多错误,但由于标签错误我看不到 indents/format。
这也意味着这种形式的代码不可维护。
考虑将 if-else 逻辑移动到 servlet/class 并让 jsp 只显示数据。