| This Chapter | |
| - | Chapter 7: JSTL |
| - | Introducing JSTL |
| - | General-Purpose Actions |
| - | Conditional Actions |
| - | Iterator Actions |
| - | Formatting Actions |
| - | Functions |
| - | Summary |
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 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 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 tests whether a string ends with the specified suffix. The return value is a Boolean. Its syntax is as follows:
For example, the following EL expression returns true:
${fn:endsWith(“Hello World”, “World”)}
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 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 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 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 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 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 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 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 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 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 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 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 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 ")}