JSTL Tags

The JavaServer Pages Standard Tag Library (JSTL) is a collection of useful JSP tags which encapsulates core functionality common to many JSP applications. Spring MVC support JSTL.

JSTL has support for control statements such as iteration and if condition. It supports tags for manipulating XML documents, internationalization tags, and SQL tags. It also provides a framework for integrating existing custom tags with JSTL tags.

JSTL tags can be categorized into following libraries:

  1. Core Tags

  2. Formatting tags

  3. SQL tags

  4. XML tags

  5. JSTL Functions

Core Tag Library

These tags are most frequently used and contains tags for control statements. Using these tags you can implement conditional and iterative presentation logic. Iteration is mostly used to render list data on Views.

You can import Core tag library using following JSP taglib directive:

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

Followings are JSTL tags and their description:

Most frequently tags are marked bold in table.

The <c:out > Tag

The <c:out> tag displays the result of an expression, similar to the way <%= %> works with a difference that <c:out> tag lets you use the simpler "." notation to access properties.

For example, to access customer.address.street just use tag is

<s:out value="${customer.address.street}" />

<s:out value="${marksheet.name}" />

The <c:if> Tag

The <c:if> tag evaluates an expression and displays its body content only if the expression evaluates to true.

Example # 1:

<c:set var="price" scope="session" value="${10*110}"/>

<c:if test="${price < 200}">

<p>You can buy pizza that's price is : <c:out value="${price}"/><p>

</c:if>

Example # 2:

List<CollegeDTO> list = list = service.search();

<c:if test="${empty list}">

<H2 style="color: red">Records not found</H2>

</c:if>

The <c:forEach> Tag

This tag iterates over a collection of objects. This is the alternate of Java loop (for, while, or do-while) statements.

This has following attributes:

Example:

List<CollegeDTO> list = list = service.search();

<c:forEach items="${list}" var="college" varStatus="loop">

<tr>

<td><input type="checkbox" name="ids" value="${college.id}"></td>

<td>${loop.index}</td>

<td>${college.name}</td>

<td>${college.address}</td>

</tr>

</c:forEach>

The <c:choose> Tag

This tag works like a Java switch statement, it allows you to choose between a number of alternatives. Where the switch statement has case statements, the <c:choose> tag has <c:when> tags. A a switch statement has default clause to specify a default action and similar way <c:choose> has <c:otherwise> as default clause.

Attributes:

The <c:choose> tag does not have any attribute.

The <c:when> tag has one attributes which is listed below.

The <c:otherwise> tag does not have any attribute.

<c:set var="money" scope="request" value="100"/>

<c:choose>

<c:when test="${money< 100}">

<B>You can buy regular Pizza</b>

</c:when>

<c:when test="${money> 100 }">

<B>You can buy regular Pizza with Cheese </b>

</c:when>

<c:otherwise>

<B>Would you like to have Bada Pav?</b>

</c:otherwise>

</c:choose>

The <c:url> Tag

This tag formats a URL into a string and stores it into a variable. This tag automatically performs URL rewriting when necessary. The var attribute specifies the variable that will contain the formatted URL.

The JSTL url tag is just an alternative method of writing the call to the response.encodeURL() method. The only real advantage the url tag provides is proper URL encoding, including any parameters specified by children param tag.

It has following attributes:

Example:

<a href="<c:url value="/index.jsp"/>" >Home</a>