【问题标题】:"IF" JSTL not returning the output as per the condition“IF” JSTL 未根据条件返回输出
【发布时间】:2019-03-11 20:10:14
【问题描述】:

我有两个 if jstl 标签,如果其中一个案例返回 true,它们都会执行事件。

请参考下面 HasBooked 为 true 的代码:

<form class="form-group" action="book_hotel.jsp" method="POST">
            <select class="form-control dest" name="destination" onchange="getSelDest(this)">
            <% 
            boolean hasBooked = (Boolean) session.getAttribute("hasBooked");
           %>
           <c:if test = "${!hasBooked}"> //returns true still it is not skipped
               <option value="null">Choose a destination</option> 
           </c:if>
          <c:if test = "${hasBooked}">
              <option selected value="${destination}">${destination}</option>
           </c:if>
            <option value="Chile">Chile</option>
            <option value="Cuba">Cuba</option>
            <option value="Dubai">Dubai</option>
            <option value="Egypt">Egypt</option>
          </select>
          <div class="text-center" style="margin:3% 0;">
            <button type="submit" class="btn btn-primary">Submit</button>
          </div>
        </form>

我已经通过打印 hasBooked 的值进行了测试,并且第一个案例 &lt;c:if test = "${!hasBooked}"&gt; 仍然被执行。 IF JSTL 没有评估我猜想的条件。

任何指导将不胜感激!

提前致谢!

【问题讨论】:

  • 使用${not hasBooked}而不是${!hasBooked}并检查hasBooked的值
  • @lucumt 实际上,如果 hasBooked 为真,我不想显示第一个选项,反之亦然。

标签: jsp servlets jstl jsp-tags


【解决方案1】:

使用&lt;c:choose&gt; 代替多个&lt;c:if&gt;

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>
<c:choose>
     <c:when test = "${not hasBooked}"> //returns true still it is not skipped
        <option value="null">Choose a destination</option> 
     </c:when>

     <c:otherwise>
        <option selected value="${destination}">${destination}</option>
     </c:otherwise>
  </c:choose>

【讨论】:

    猜你喜欢
    • 2023-01-24
    • 2014-11-28
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-30
    相关资源
    最近更新 更多