【问题标题】:JSTL c:choose and mutiple c:forEach, c:if issueJSTL c:choose 和多个 c:forEach, c:if 问题
【发布时间】:2017-01-17 13:44:38
【问题描述】:

我正在尝试在<c:choose> 中使用<c:forEach><c:if>。我最初的计划是当列表大小为 0 时显示“没有结果”,如果不是,则显示列表中的内容。但是,似乎每当我使用<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>

这里是&lt;c:choose&gt; &lt;c:forEach&gt; 等的代码

<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 -->

【问题讨论】:

    标签: jsp foreach jstl


    【解决方案1】:

    我发现了一些语法错误:

    • 缺少结束引号:&lt;div class="container&gt; 应该是 &lt;div class="container"&gt;
    • 我没有看到&lt;c:choose&gt;&lt;c:otherwise&gt; 的任何开始标签。 &lt;c:when&gt; 也不见了。

    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>
    

    可能会有更多错误,但由于标签缩进/格式错误,我看不到。
    这也意味着这种形式的代码是不可维护的。
    考虑将 if-else 逻辑移动到 servlet/类,让 jsp 只显示数据。

    【讨论】:

      猜你喜欢
      • 2011-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2011-07-09
      • 2015-04-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多