• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

JSTL XML x:choose ,x:when and x:otherwise Tags

March 4, 2014 //  by Krishna Srinivasan//  Leave a Comment

The <x:choose> tag is XML tag used for checking conditional statements. This is similar to java switch statement. In java program we use the case statement but in JSTL we use <c:when< and <x:otherwise< instead of case statement.

Syntax of <x:choose> Tag

<x:choose> body content </x:choose>

JSTL XML <x:when> Tag

This is the subtag of <x:choose> tag. The <x:when> tag is checks to see whether the expression evaluates to true, if it is true then it executes the body of <x:when> tag. This tag encloses a single case within the <x:choose> tag.

Syntax of <x:when> tag

<x:when attribute> body content </x:when>

Attribute of <x:when> tag

  • select: This tag is used to specifies whether the body of the tag will be executed or no.

JSTL XML <x:otherwise> Tag

This is the subtag of <x:choose> which is used with <x:when> tag. This is similar as <x:when> tag but it is unconditional. This tag includes its body when all of the conditions specified using <x:when> tags evaluated to false.

Syntax of <x:otherwise> tag

<x:otherwise> body content </x:otherwise>

Example of <x:choose&gt ,<x:when>, and <x:otherwise> tag

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>

<html>
<head>

  <title>JSTL x:choose Tags</title>

</head>
<body>

<h3>Fruits Info:</h3>

<c:set var="fruitss">

<fruits>

<fruit>
      <name>Apple</name>
      <price>90</price>
</fruit>

<fruit>
      <name>Orange</name>
      <price>30</price>
</fruit>

</fruits>

</c:set>

<x:parse xml="${fruitss}" var="output"/>

<x:choose>

 <x:when select="$output//fruit/price = '90'">
      The apple price is rs 90
  </x:when>

<x:otherwise>
      Unknown Fruit.
</x:otherwise>

</x:choose>

</body>
</html>

Details of the Code

  • <<c:set var=”fruitss”> tag is used to set the variable name which we want to display in the output.
  • <x:parse xml=”${fruitss}” var=”output”/> tag is used to parse the xml content and the result is been stored in specified variable.
  • <x:choose> tag is used to check the conditional statement.
  • <x:when select=”$output//fruit/name = ‘apple'”> tag is used to execute the particular statement when it is true.
  • <x:otherwise> tag is used to define unconditional statement.

Steps for Execution

Before executing the xml programs we should add jar files in eclipse namely:
xalan-2.7.0.jar

  • Save this file as example.jsp in your eclipse IDE.
  • Now select this jsp file, right mouse click and select Run as ->Run on server

Output

When the execution process is completed successfully we will get the following output :
jstl_xchoose_demo

 

Previous Tutorial :  JSTL XML x:transform Tag :: Next Tutorial : JSTL XML x:param Tag

Category: Java EETag: JSTL Tutorials

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « JSTL XML x:transform Tag
Next Post: JSTL XML x:param Tag »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

EJB 3.0 Timer Services

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact