Jay
Jay

Reputation: 2527

multiple <c:when> inside <c:choose>

I am just wondering whether the below code is valid?

<c:choose>
    <c:when test="${empty example1}">
    </c:when>
    <c:when test="${empty example2}">
    </c:when>
    <c:otherwise>
    </c:otherwise>              
</c:choose>

Upvotes: 9

Views: 40803

Answers (3)

DwB
DwB

Reputation: 38328

In a c:choose, the first when for which the test is true is the winner. In the c:choose below, if "first test" and "second test" are both true, then the "Kpow" h2 will be added to the html page and the "Blammy" will not.

<c:choose>
  <c:when test="first test">
    <h2>Kpow</h2>
  </c:when>
  <c:when test="second test">
    <h2>Blammy</h2>
  </c:when>
</c:choose>

Upvotes: 39

Satya
Satya

Reputation: 8346

<c:choose>
    <c:when test="${empty example1}">
    </c:when>
  <c:when test="${empty example2}">
  </c:when>
  <c:otherwise>
  </c:otherwise>              
</c:choose>

This code is nothing but

 switch(int i){
   case 1:
   ...
   break;
   case 2:
   ...
   break;
   default:
   ...
   break;
}

Upvotes: 8

Mike K.
Mike K.

Reputation: 3789

Yes its valid. Why not just try it though? Look up JSTL for more info.

Upvotes: 1

Related Questions