JSTL - JSP Standard Tag Libraries


JSTL Core Actions

 

The JSTL – Java Server Pages Standard Tag Library is a collection of useful JSP tags which encapsulates the core functionality common to many JSP applications. The JSTL is divided into several separate functional areas, each with its own namespace as shown in Table 1.

Table 1: JSTL Functional Areas

Functional area

Namespace Name prefix

Capabilities

Core

c

basic operations such as variables declaration, printing/displaying values, performing iterations, declaring conditional statements, etc

XML Processing

xml

allows a range of XML-related functions to be performed by a JSP document, including XSLT processing.

Functions

fn

Provides

1.    a length() function that can be used to obtain the number of elements in a collection

2.    a variety of standard string processing functions that can be included in EL expressions.

Database

sql

allows a JSP document to directly access a database rather than accessing it indirectly through JavaBeans objects

Internationalization

fmt

allows strings representing data such as currency or dates to be formatted appropriately for a given locale

 

 

The basic features of the JSTL core actions are

Action  

Purpose

set

Assign a value to a scoped variable, creating the variable if necessary

remove

Destroy a scoped variable

out

Write data to out implicit object, escaping XML special characters

url

Create a URL with query string

if

Conditional (if-then) processing

Choose

Conditional (if-then-elseif) processing

forEach

Iterate over a collection of items

 

 

Naming conventions

When the attribute var is specified in a JSTL element, it represents the name of a scoped variable that is being assigned a value by the element. The scope attribute is set to one of four values – page (default), request, session, or application. The values of var and scope attributes must be string literals.

<c:set var="visits" scope="application" value="${visits+1}" />

 

Example program to demonstrate JSTL Core actions

 

<!-- JSTL Core actions -->

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

<html>

<body>

<h2>JSTL Core Actions </h2>

<h3>

var, scope, set and out ...

     <c:set var = "sal" scope="session" value="${2000}"/>

              <c:out value = "${sal}"/>

     <c:set var = "msg" scope="page" value="${'hello'}"/>

               <c:out value = "${msg}"/>

remove ...

     <c:remove var = "sal"/>

               value after removing an attribute = <c:out value = "${sal}"/>

 

if ...

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

               <c:set var="visits" scope="application" value="0" />

     </c:if>

     Hit count =

     <c:set var="visits" scope="application" value="${visits+1}" />

               <c:out value = "${visits}"/>

 

choose ...

     <c:set var = "Num" scope = "session" value = "${0}"/>    

     <c:choose>

               <c:when test="${Num < 0}">

                        ${Num} is negetive.

               </c:when>

               <c:when test="${Num > 0}">

                        ${Num} is positive.

               </c:when>

               <c:otherwise>

                        Zero.

               </c:otherwise>

     </c:choose>

 

forEach...

     <c:forEach var="i" begin="2" end="20" step="2">

               ${i}

     </c:forEach>

 

url ...

     <a href = "<c:url value = "/hello.html"/>">TEST</a>

 

</h3>

</body>

</html>

 

Output:

JSTL Core Actions

 

var, scope, set and out ...

2000

hello

 

remove ...

value after removing an attribute =

 

if ...

Hit count = 3

 

choose ...

Zero.

 

forEach...

2   4   6   8   10   12   14   16   18   20

url ...

TEST

 


 

JSTL Functions

Function

Purpose

fn:contains()

checks whether the given string is present in the input as sub-string. It does a case sensitive check

fn:containsIgnoreCase()

case insensitive check to see whether the provided string is a sub-string of input.

fn:indexOf()

To find the start position of a string in the provided string. Function returns -1 if string is not found

fn:join()

concatenates the strings with a given separator and returns the output string.

fn:split()

splits a given string into an array of substrings.

fn:length()

for computing the length of a string or to find out the number of elements in a collection. It returns the length of the object

fn:startsWith()

checks the specified string is a prefix of given string

fn:endsWith()

for checking the suffix of a strin

fn:substring()

for getting a substring from the provided string

fn:trim()

removes spaces from beginning and end of a string and function.

fn:toUpperCase()

converts input string to a uppercase string.

fn:toLowerCase()

converts input string to a lower case string

fn:replace()

to search for a string in the input and replace it with the provided string. – case sensitive processing

 

<!-- JSTL Functions -->

 

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

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

<html>  

<body>

<h2>JSTL Functions </h2>

<h3>

<c:set var = "msg" scope="page" value="${'Hello World'}"/>

Original String = <c:out value = "${msg}"/>

 

Uppercase : ${fn:toUpperCase(msg)}

 

Lowercase : ${fn:toLowerCase(msg)}

 

Length : ${fn:length(msg)}

 

Substring : ${fn:substring(msg, 0, 5)}

 

Replace : ${fn:replace(msg, "Hello", "Hai")}

</h3>

</body>

</html>

 

Output:

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu