JSTL Core <c:choose>,<c:when>,<c:otherwise> Tag

Introduction:

The JSTL <c:choose> Core Tag is used when a number of alternatives are available for a particular condition. It works same as of switch statement in java. The <c:choose> is like switch, <c:when> is like case and <c:otherwise> is like default statement.


Attribute:

  1. The <c:choose> tag does not have any attribute.
  2. The <c:otherwise> tag does not have any attribute.
  3. The <c:when> tag has one attributes which is listed below.


c:when tag attributes:

AttributeDescriptionRequired
testIt specify the condition to evaluate.No

Introduction:

The JSTL <c:choose> Core Tag is used when a number of alternatives are available for a particular condition. It works same as of switch statement in java. The <c:choose> is like switch, <c:when> is like case and <c:otherwise> is like default statement.


Attribute:

  1. The <c:choose> tag does not have any attribute.
  2. The <c:otherwise> tag does not have any attribute.
  3. The <c:when> tag has one attributes which is listed below.


c:when tag attributes:

AttributeDescriptionRequired
testIt specify the condition to evaluate.No

Syntax:

<c:choose>   
<c:when test="${testCondition1}"> 
//block of statements 
</c:when>   
<c:when test="${testCondition2}"> 
//block of statements 
</c:when>   
//Other 
<c:when>   
<c:otherwise> 
//block of statements 
</c:otherwise>   
</c:choose>

Example:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title>JSTL c:choose,c:when,c:otherwise</title>
</head>
<body>
	<c:set scope="request" value="18" var="age" />
	Your age is :
	<c:out value="${age}" />
	<c:choose>
		<c:when test="${age < 18}"> You are a minor </c:when>
		<c:when test="${age >= 18}">
			<h1>You are an adult</h1>
		</c:when>
		<c:otherwise> Not applicable </c:otherwise>
	</c:choose>
</body>
</html>

Output:

C:Choose





comments powered by Disqus