Home

This Chapter
-Chapter 7: JSTL
-Introducing JSTL
-General-Purpose Actions
-Conditional Actions
-Iterator Actions
-Formatting Actions
-Functions
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

Functions

In addition to custom actions, JSTL 1.1 defines a set of standard functions you can use from EL expressions. Unfortunately they are not available in JSTL 1.0. These functions are grouped in the function tag library. To use the functions, you must use the following taglib directive on top of your JSP.

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

To invoke a function, you use an EL with the format

${fn:functionName}

where functionName is the name of the function.

Most of the functions are for string manipulation. The length function works for both strings and collections. In particular, the length function returns the number of items in a collection or array or the number of characters in a string. All these functions are described in the sections to follow.

The contains Function

The contains function tests whether a string contains the specified substring. The return value is a Boolean true if the string contains the substring, and false otherwise. Its syntax is as follows:

contains(string, substring).

For example, both of these EL expressions return true:

<c:set var="myString" value="Hello World"/>
${fn:contains(myString, "Hello")}

${fn:contains("Stella Cadente", "Cadente")}

The containsIgnoreCase Function

The containsIgnoreCase function is similar to the contains function, but testing is performed in a case-insensitive way. The syntax is as follows:

containsIgnoreCase(string, substring)

For instance, the following EL expression returns true:

${fn:containsIgnoreCase("Stella Cadente", "CADENTE")}

The endsWith Function

The endsWith function tests whether a string ends with the specified suffix. The return value is a Boolean. Its syntax is as follows:

endsWith(string, suffix)

For example, the following EL expression returns true:

${fn:endsWith(“Hello World”, “World”)}

The escapeXml Function

This function is useful for encoding a String. The conversion is the same as the out tag with its escapeXml attribute set to true. The syntax of escapeXml is as follows:

escapeXml(string)

For example, the EL expression

${fn:escapeXml("Use <br/> to change lines")}

is rendered as the following:

Use <br/> to change lines

The indexOf Function

The indexOf function returns the index within a string of the first occurrence of the specified substring. If the substring is not found, it returns -1. Its syntax is as follows:

indexOf(string, substring)

For instance, the following EL expression returns 7:

${fn:indexOf("Stella Cadente", "Cadente")}

The join Function

The join function joins all elements of a String array into a string, separated by the specified separator. The syntax is as follows:

join(array, separator)

If the array is null, an empty string is returned.

For example, if myArray is a String array having the two elements “my” and “world”, the EL expression

${fn:join(myArray,",")}

returns “my,world”.

The length Function

The length function returns the number of items in a collection, or the number of characters in a string. Its syntax is as follows:

length{input}

As an example, the following EL expression returns 14:

${fn:length("Stella Cadente", "Cadente")}

The replace Function

The replace function replaces all occurrences of beforeString with afterString in a string and returns the result. Its syntax is as follows:

replace(string, beforeSubstring, afterSubstring)

For example, the EL expression

${fn:replace("Stella Cadente", "e", "E")}

returns “StElla CadEntE”.

The split Function

The split function splits a string into an array of substrings. It does the opposite of the join function. For example, the following code splits the string “my,world” and stores the result in the scoped variable split. It then formats split into an HTML table using the forEach tag.

<c:set var="split" value='${fn:split("my,world",",")}'/>
<table>
<c:forEach var="substring" items="${split}">
<tr><td>${substring}</td></tr>
</c:forEach>
</table>

The result is this:

<table>
<tr><td>my</td></tr>
<tr><td>world</td></tr>
</table>

The startsWith Function

The startsWith function tests whether a string starts with the specified prefix. The syntax is as follows:

startsWith(string, prefix)

For instance, the following EL expression returns true:

${fn:startsWith("Stella Cadente", "St")}

The substring Function

The substring function returns a substring from the specified zero-based begin index (inclusive) to the specified zero-based end index. The syntax is as follows:

substring(string, beginIndex, endIndex)

For example, the following EL expression returns “Stel”.

${fn:substring("Stella Cadente", 0, 4)}

The substringAfter Function

The substringAfter function returns the portion of a string after the first occurrence of the specified substring. Its syntax is as follows:

substringAfter(string, substring)

For example, the EL expression

${fn:substringAfter("Stella Cadente", "e")}

returns “lla Cadente”.

The substringBefore Function

The substringBefore function returns the portion of a string before the first occurrence of the specified substring. Its syntax is as follows:

substringBefore(string, substring)

For instance, the following EL expression returns “St”.

${fn:substringBefore("Stella Cadente", "e")}

The toLowerCase Function

The toLowerCase function converts a string into its lowercase version. Its syntax is as follows:

toLowerCase(string)

For example, the following EL expression returns “stella cadente”.

${fn:toLowerCase("Stella Cadente")}

The toUpperCase Function

The toUpperCase function converts a string into its uppercase version. Its syntax is as follows:

toUpperCase(string)

For instance, the following EL expression returns “STELLA CADENTE”.

${fn:toUpperCase("Stella Cadente")}

The trim Function

The trim function removes the leading and trailing whitespaces of a string. Its syntax is as follows:

trim(string)

For example, the following EL expression returns “Stella Cadente”.

${fn:trim("                 Stella Cadente  ")}

Previous
Next