Sunday, 2 November 2014

JAVA : JSTL Core Tags c:choose, c:when, c:otherwise

Standard

These tags are used as conditional statements in Jsp's. These.These tags are works like Switch case statements in Java. These tags are helpful to choose multiple options based on the conditions. <c:choose> tag works like switch and<c:when> tag works like case statement, <c:otherwise> tag works like default , it executes if none of the conditions are  true. Let's  go for example for better understanding purpose.








Example : 




  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3.      <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11. <c:set var="money" scope="session" value="${550}"/>   
  12.  <p>The amount is : <c:out value="${money}"/></p>   
  13. <c:choose>   
  14.     <c:when test="${money <= 0}">   
  15.       We cannot By things  
  16.     </c:when>   
  17.     <c:when test="${money > 500}">   
  18.         Enough to Buy  
  19.     </c:when>   
  20.     <c:otherwise>   
  21.         Nothing is happen  
  22.     </c:otherwise>   
  23. </c:choose>   
  24. </body>  
  25. </html>  



Write the above code in Notepad++ or Notepad and save file with extension dot JSP(.jsp).


OUTPUT :





0 comments:

Post a Comment