How to call functions in JSP via JSTL

In your jsp page your need to import a jstl taglib as below

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

Table below lists all the functions available under fn: prefix

Sr.No Return Type Function Contract Description
1 boolean contains(java.lang.String, java.lang.String) Tests if input string contians the specified substring
2 boolean containsIgnoreCase( java.lang.String, java.lang.String) Tests if an input string contains the specified substring in a case insensitive way.
3 boolean endsWith( java.lang.String, java.lang.String) Tests if an input string ends with the specified suffix.
4 java.lang.String escapeXml( java.lang.String) Escapes characters that could be interpreted as XML markup.
5 int indexOf( java.lang.String, java.lang.String) Returns the index withing a string of the first occurrence of a specified substring.
6 java.lang.String join( java.lang.String[], java.lang.String) Joins all elements of an array into a string.
7 int length( java.lang.Object) Returns the number of items in a collection, or the number of characters in a string.
8 java.lang.String replace( java.lang.String, java.lang.String, java.lang.String) Returns a string resulting from replacing in an input string all occurrences of a “before” string into an “after” substring.
9 java.lang.String[] split( java.lang.String, java.lang.String) Splits a string into an array of substrings.
10 boolean startsWith( java.lang.String, java.lang.String) Tests if an input string starts with the specified prefix.
11 java.lang.String substring( java.lang.String, int, int) Returns a subset of a string.
12 java.lang.String substringAfter( java.lang.String, java.lang.String) Returns a subset of a string following a specific substring.
13 java.lang.String substringBefore( java.lang.String, java.lang.String) Returns a subset of a string before a specific substring.
14 java.lang.String toLowerCase( java.lang.String) Converts all of the characters of a string to lower case.
15 java.lang.String toUpperCase( java.lang.String) Converts all of the characters of a string to upper case.
16 java.lang.String trim( java.lang.String) Removes white spaces from both ends of a string.

eg.

The syntax for calling functions is ${fn:length(lst)}

Now in your jsp code you should be using it as below, assume that “lst” is an arraylist in your jsp page.

<c:choose>
	<c:when test="${fn:length(lst) gt 0}">
            ....
            do some cool stuff here, maybe iterate over your list!
            ....
        </c:when>
</c:choose>

References:

http://docs.oracle.com/javaee/5/jstl/1.1/docs/tlddocs/

Advertisements

, , , , ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: