In JSTL, the functionality if-else is provided by choose-when-otherwise statement .
<c:choose> tag is an enclosing tag for <c:when> and <c:otherwise>. And it is also known as mutual exclusion as only a block of code within the condition of mutual exclusion be executed. <c:when> and <c:otherwise> both come under this <c:choose> tag.
<c:when> tag is equivalent to the case statements of a switch statement. <c:when> statements are considered as more advanced than case statements as switch case statements only compares the variable mentioned in the switch statement w.r.t the constant in case statement while the <c:when> tag evaluates the expressions.
<c:otherwise> tag is equivalent to the else statements.
Example:
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title> Choose,Otherwise and When (if-else in JSTL)</title>
</head>
<body>
<c:if test="${pageContext.request.method=='POST'}">I have
<c:out value="${param.enter}" />
<c:choose>
<c:when test="${param.enter=='0'}">(means no ) notebook.
<br />
</c:when>
<c:when test="${param.enter=='1'}">notebook.
<br />
</c:when>
<c:otherwise>notebooks.
<br />
</c:otherwise>
</c:choose>
</c:if>
<form method="post">Enter a number:
<input type="text" name="enter" />
<input type="submit" value="accept" />
<br />
</form>
</body>
</html>
0 Comment(s)