Choose in JSP Core Tag Library
How is "Choose" tag from JSP Core Tag Library used?
Explanation
The
choose tag of the JSP Core Tag Library is used for conditional execution of statements.
Syntax:
<c:choose>
<c:when.....>
...
</c:when>
<c:when.....>
...
</c:when>
<c:otherwise>
...
</c:otherwise>
</c:choose>
In the above syntax if the condition is "true" the statement or code under the "when" tag will be in execution, if nothing is matched the statement under the tag "otherwise" will be in execution. Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core"
prefix="c" %>
<c:forEach var="index" items="Monday,Tuesday,Wenesday,
Thursday,Friday">
# ${index}:
<c:choose>
<c:when test="${index == 'Monday'}">
First day of week!</br>
</c:when>
<c:when test="${index == 'Wenesday'}">
Third day of week!</br>
</c:when>
<c:when test="${index =='Friday'}">
Fifth day of week!</br>
</c:when>
<c:otherwise>
Not a chosen day?</br>
</c:otherwise>
</c:choose>
</c:forEach>
Result :
# Monday: First day of week!
# Tuesday: Not a chosen day?
# Wenesday: Third day of week!
# Thursday: Not a chosen day?
# Friday: Fifth day of week!
In the above example a forEach loop is used to iterate five days of a week and only the chosen days of the week "Monday, Wenesday, Friday" have a message to display other days which are not listed display the message given under the "otherwise" tag "Not a chosen day?" is displayed.