1.

Explain Jstl Choose, When, Otherwise Tag With An Example?

Answer»

JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the when tag evaluates to true, then the content within when tag is evaluated, otherwise the content within the otherwise tag is evaluated.

We can also implement if-else-if construct by USING multiple when tag. The when tags are mutually EXCLUSIVE, that means the first when tag which evaluates to true is evaluated and then, the control exits the choose BLOCK. If none of the when condition evaluates to true, then otherwise condition is evaluated. For EXAMPLE

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
<c:choose>
<c:when test="${status.count %2 == 0 }">
<p> Divisible by 2 : ${tutorial.key} </p>
<br/>
</c:when>
<c:when test="${status.count %5 == 0 }">
<p > Divisible by 5 : ${tutorial.key} </p>
<br/>
</c:when>
<c:otherwise>
<p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>

JSTL choose, when, otherwise tag: These are conditional tags used to implement conditional operations. If the test condition of the when tag evaluates to true, then the content within when tag is evaluated, otherwise the content within the otherwise tag is evaluated.

We can also implement if-else-if construct by using multiple when tag. The when tags are mutually exclusive, that means the first when tag which evaluates to true is evaluated and then, the control exits the choose block. If none of the when condition evaluates to true, then otherwise condition is evaluated. For Example

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Tag Example</title>
</head>
<body>
<c:forEach var="tutorial" items="${MyTutorialMap}" begin="0" end="5" varStatus="status">
<c:choose>
<c:when test="${status.count %2 == 0 }">
<p> Divisible by 2 : ${tutorial.key} </p>
<br/>
</c:when>
<c:when test="${status.count %5 == 0 }">
<p > Divisible by 5 : ${tutorial.key} </p>
<br/>
</c:when>
<c:otherwise>
<p> Neither divisible by 2 nor 5 : ${tutorial.key} </p><br/>
</c:otherwise>
</c:choose>
</c:forEach>
</body>
</html>



Discussion

No Comment Found