rajput sufiyaan
rajput sufiyaan

Reputation: 71

"IF" JSTL not returning the output as per the condition

I have two if jstl tags and they both are getting executed event if one of the case is returning true.

Please refer to the code below where HasBooked is 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>

I have tested by printing the value of the hasBooked and it is true still the first case <c:if test = "${!hasBooked}"> is getting executed. The IF JSTL is not evaluating the conditions I guess.

Any guidance will be really appreciated!

Thanks in advance!

Upvotes: 0

Views: 134

Answers (1)

ErShakirAnsari
ErShakirAnsari

Reputation: 652

Use <c:choose> instead multiple <c:if>

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

Upvotes: 1

Related Questions