WEBSITE

Java EE 6 with GlassFish 3 Application Server : JSP Standard Tag Library - JSTL functions

9/24/2012 1:07:30 AM
JSTL contains a number of functions that take Unified Expression Language expressions as parameters. All JSTL functions except one are used exclusively for String manipulation. An exception is the fn:length() function, which can take a String, Collection, or array as a parameter. It returns the length of the String, the size of the Collection, or the length of the array, depending on what parameter is passed to it. The following JSP illustrates the use of JSTL functions:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Function Tag Demo</title>
</head>
<body>
<c:set var="nameArr" value="${fn:split('Kevin,Danielle,Alex,Beatrice',',')}" />
We have a list of ${fn:length(nameArr)} names, here they are:
<br />
<ol>
<c:forEach var="currentName" items="${nameArr}">
<li />
${fn:toUpperCase(currentName)}
<br />
</c:forEach>
</ol>
</body>
</html>


					  

After packaging this JSP in a WAR file, deploying it and pointing the browser to its URL, we should see a page like the following:

This JSP illustrates the use of some of the JSTL functions. The fn:split() function splits a String into an array of strings using the character specified by its second parameter as a delimiter.

Notice that the strings inside the fn:split() function are enclosed inside single quotes. JSTL allows this as using double quotes for the strings would have resulted in illegal syntax, as the fn:split() function is already inside double quotes.


In the example, the fn:length() function returns the number of elements in the array we created when the fn:split() function was executed. Like we mentioned earlier, the fn:length() function can also take a Collection or a String as a parameter. When applied to a Collection, the function returns the number of elements in it. When applied to a String, the function returns the number of characters in the String.

The next function illustrated in the example is fn:toUpperCase(), which simply makes every alphabetical character in the String it takes as a parameter uppercase. There are many other JSTL functions, all of them are very intuitive to use. The following table lists all the JSTL functions:

Function Description Example
fn:contains(String, String) Returns a boolean indicating if the second parameter is contained in the first one. ${fn:contains("envir onment", "iron")}
fn:containsIgnoreCase( String, String) Case-insensitive version of fn:contains(). ${fn:containsIgnoreC ase("environment", "Iron")}
fn:endsWith(String, String) Returns a boolean indicating if the first parameter ends with a string equal to the second parameter. ${fn:endsWith("Glass Fish", "Fish")}
fn:escapeXml(String) Returns a string with all XML characters in the parameter escaped into their respective XML character entity code. ${fn:escapeXml( "<html>")}
fn:indexOf(String, String) Returns an int indicating the index of the second parameter in the first parameter. Returns -1 if the second parameter is not a substring of the first parameter. ${fn:indexOf("GlassF ish", "Fish")}
fn:join(String[], String) Returns a string composed of the elements in the first parameter, using the second parameter as a delimiter. ${fn:join(arrayVar," , ")}
fn:length(Object) Returns the length of an array, the size of a collection, or the length of a string, depending on the type of the parameter. ${fn:length("String, Collection or Array")}
fn:replace(String, String, String) Returns a string replacing every instance of the second parameter on the first parameter with the third parameter. ${fn:replace("Crysta lFish", "Glass")}
fn:startsWith(String, String) Returns a boolean indicating if the first parameter starts with the first parameter. ${fn:startsWith("Gla ssFish", "Glass")}
fn:split(String, String) Returns an array of strings containing elements in the first parameter as delimited by the second parameter. ${fn:split("Eeny, meeny", ",")}
fn:substring(String, int, int) Returns a string containing the substring in the first parameter, starting at the index indicated by the second parameter and ending just before the index indicated by the second parameter. ${fn:substring( "0123456789", 3, 6)}
fn:substringAfter (String, String) Returns a string containing the substring in the first parameter, starting after the first occurrence of the second parameter until the end of the first parameter. ${fn:substringAfter( "GlassFish", "Glass")}
fn:substringBefore (String, String) Returns a string containing the substring in the first parameter, starting before the first occurrence of the second parameter until the end of the first parameter. ${fn:substringBefore ("GlassFish", "Fish")}
fn:toLowerCase (String) Returns a string containing a version of the parameters with all alphabetical characters as lowercase. ${fn:toLowerCase( "GlassFish")}
fn:toUpperCase (String) Returns a string containing a version of the parameters with all alphabetical characters as uppercase. ${fn:toUpperCase (" GlassFish ")}
fn:trim(String) Returns a string containing a modified version of the parameter with all whitespace at the beginning and end of the parameter removed. ${fn:trim(" Gla ssFish ")}
Other  
 
Top 10
3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
OPEL MERIVA : Making a grand entrance
FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
BMW 650i COUPE : Sexy retooling of BMW's 6-series
BMW 120d; M135i - Finely tuned
PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
Java Tutorials : Nested For Loop (part 2) - Program to create a Two-Dimensional Array
Java Tutorials : Nested For Loop (part 1)
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
VIDEO TUTORIAL
- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

- How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
Popular Tags
Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS