Common usage of jstl and El Combination

Source: Internet
Author: User

JSTL Functions Tag Library

Section 12.3 of this book, defining and Using El Functions, describes how to create and use El functions. A common set of El Functions is provided in the JSTL functions tag library, primarily for processing strings, which can be used directly in JSPs.

To use the functions tag library in a JSP file, first introduce the tag library through the taglib directive:

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

This chapter describes the usage of the 16 commonly used functions in the functions tag library, which are similar in name and function to the corresponding methods in the Java.lang.String class. For example: The Fn:indexof function is similar to the indexof () method of the class string class, and the Fn:substring function is similar to the substring () method of the String class.

1fn:contains function

The Fn:contains function is used to determine whether a target string is included in the source string, with the following syntax:

Fn:contains (String source,string target)-------Boolean;

The source string above specifies the target parameter specifying the destination string, and the return type is Boolean.

For example, the following El expression:

${fn:contains ("Tomcat", "Cat")}

${fn:contains ("Tomcat", "CAT")}

The value of the first El expression is true, and the value of the second El expression is false.

2fn:containsignorecase function

The Fn:containsignorecase function is used to determine whether the target string is included in the source string and ignores case when judged, with the syntax:

Fn:containsignorecase (String source,string target)-------Boolean;

The source string above specifies the target parameter specifying the destination string, and the return type is Boolean.

For example, the following El expression:

${fn:containsignorecase ("Tomcat", "CAT")}

${fn:containsignorecase ("Tomcat", "Mike")}

The value of the first El expression is true, and the value of the second El expression is false.

3 Fn:startswith function

The Fn:startswith function is used to determine whether the source string begins with the specified target string, and its syntax is:

Fn:startswith (String source,string target)----Boolean

The source string above specifies the target parameter specifying the destination string, and the return type is Boolean.

For example, the following El expression:

${fn:startswith ("Tomcat", "Tom")}

${fn:startswith ("Tomcat", "Cat")}

The value of the first El expression is true, and the value of the second El expression is false.

4 Fn:endswith function

The Fn:endswith function is used to determine whether the source string ends with the specified target string, with the following syntax:

Fn:endswith (String source,string target)----Boolean

The source string above specifies the target parameter specifying the destination string, and the return type is Boolean.

For example, the following El expression:

${fn:endswith ("Tomcat", "Cat")}

${fn:endswith ("Tomcat", "Tom")}

The value of the first El expression is true, and the value of the second El expression is false.

5 Fn:indexof function

The Fn:indexof function is used to find the target string in the source string and returns the index of the first character in the source string that matches the target string first, and returns 1 if the source string does not contain the target string, and the first character in the source string has an index of 0. The syntax for the Fn:indexof function is:

Fn:indexof (String source,string target)----int

The source string above specifies the target argument specifying the destination string, and the return type is int.

For example, the following El expression:

1 ${fn:indexof ("Tomcat", "Cat")}<br/>

2 ${fn:indexof ("2211221", "Up")} <br/>

3 ${fn:indexof ("Tomcat", "Mike")} <br/>

The output is:

1 3

2 1

3-1

6 Fn:replace function

The Fn:replace function is used to replace a portion of the source string with another string and return the replaced string. The syntax for the Fn:replace function is:

Fn:replace (String source,string before,string after)----string

The source string above specifies the before parameter that specifies the substituted substring in the source string, and the after parameter specifies the substring to use for the replacement, and the return type is string.

For example, the following El expression:

1 ${fn:replace ("TomcAt", "Cat", "cat")}<br/>

2 ${fn:replace ("2008/1/9", "/", "-")}<br/>

The output is:

1 Tomcat

2 2008-1-9

7 fn:substring function

The Fn:substring function is used to get a specific substring in the source string, whose syntax is:

Fn:substring (String source,int beginindex,int endIndex)------string

The source argument above specifies the origin string, the Beginindex parameter represents the index of the first character in the substring in the source string, the Endindex parameter represents the last character of the substring in the source string index plus 1, the return type is string, The index of the first character in the source string is 0.

For example, the following El expression:

1 ${fn:substring ("Tomcat", 0,3)}<br/>

2 ${fn:substring ("Tomcat", 3,6)}<br/>

The output is:

1 Tom

2 Cat

8 Fn:substringbefore function

The Fn:substringbefore function is used to get the substring before the specified substring in the source string, whose syntax is:

Fn:substringbefore (string source,string target)----string

The source string above specifies the target parameter that specifies the substring, and the return type is string. If the source string does not contain a specific substring, an empty string is returned.

For example, the following El expression:

1 ${fn:substringbefore ("Tomcat", "Cat")}<br/>

2 ${fn:substringbefore ("MyData.txt", ". txt")}<br/>

The output is:

1 Tom

2 MyData

9 Fn:substringafter function

The Fn:substringafter function is used to get the substring after the specified substring in the source string, with the following syntax:

Fn:substringafter (string source,string target)----string

The source string above specifies the target parameter that specifies the substring, and the return type is string. If the source string does not contain a specific substring, an empty string is returned.

For example, the following El expression:

1 ${fn:substringafter ("Tomcat", "Tom")}<br/>

2 ${fn:substringafter ("MyData.txt", "MyData.")} <br/>

The output is:

1 cat

2 txt

Ten Fn:split function

The Fn:split function is used to split a source string into a string array with the following syntax:

Fn:split (String source,string delimiter)----string[]

The source string above specifies the delimiter parameter that specifies the delimiter used to split the source string, and the return type is string[]. If the delimiter specified by the delimiter parameter is not included in the source string, or if the delimiter parameter is NULL, then there is only one element in the returned string array, which is the source string.

For example, the following El expression:

<c:set value= ' ${fn:split ("www.mywebsite.org", ".")} ' var= ' STRs '/>

<c:foreach var= "token" item= "${strs}" >

${token}<br/>

</c:forEach>

The output is:

Www

Mywebsite

Org

Again for example for the following code:

<c:set value= ' ${fn:split ("www.mywebsite.org", "-")} ' var= "STRs"/>

${strs[0]}

The output is:

www.mywebsite.org

One fn:join function

The Fn:join function is used to concatenate all strings in the source string array into a string with the following syntax:

Fn:join (String source[],string separator)----string

The above source parameter specifies the source string array, the separator parameter specifies the delimiter used to concatenate individual strings in the source string array, and the return type is string.

For example, the following code:

<%

String strs[] = {"www", "mywebsite", "org"};

%>

<c:set value= "<%=strs%>" var= "STRs"/>

${fn:join (STRs, ".")}

The output is:

Www. Mywebsite. Org

Fn:tolowercase function

The Fn:tolowercase function is used to change all characters in the source string to lowercase, with the following syntax:

Fn:tolowercase (String source)-----String

The source string above specifies the return type as String.

For example, the following El expression:

Fn:tolowercase ("TomCat")

The output is:

Tomcat

Fn:touppercase function

The Fn:touppercase function is used to change all characters in the source string to uppercase, with the following syntax:

Fn:touppercase (String source)-----String

The source string above specifies the return type as String.

For example, the following El expression:

Fn:touppercase ("TomCat")

The output is:

Tomcat

Fn:trim function

The Fn:trim function is used to delete the beginning and end spaces in the source string, with the following syntax:

Fn:trim (string source)----string

The source string above specifies the return type as String.

For example, the following El expression:

Fn:trim ("Tomcat")

The value of the above El expression is "Tomcat".

The Fn:escapexml function

The Fn:escapexml function is used to convert characters "<", ">", "" "and" & "in the source string to escape characters, and the concept of escape characters is described in section 1.2 (Introduction to HTML) in chapter 1th of this book. The behavior of the Fn:escapexml function is the same as the conversion behavior when the EscapeXML property of the <c:out> tag is true, and the syntax of the Fn:escapexml function is:

Fn:escapexml (string source)----string

The source string above specifies the return type as String.

The out.jsp of routine 18-1 demonstrates the use of the Fn:escapexml function.

Routine 18-1 out.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%>

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

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

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<title>out</title>

<body>

1.${fn:escapexml ("<b> denotes bold </b>")}<br/>

2.<c:out value= "<b> denotes bold </b>" escapexml= "true" ></c:out><br/>

3.${"<b> denotes bold </b>"}<br/>

</body>

For the following code in OUT.JSP:

1.${fn:escapexml ("<b> denotes bold </b>")}<br/>

2.<c:out value= "<b> denotes bold </b>" escapexml= "true" ></c:out><br/>

3.${"<b> denotes bold </b>"}<br/>

The output is:

1.&lt;b&gt; denotes Bold &lt;/b&gt;<br/>

2.&lt;b&gt; denotes Bold &lt;/b&gt;<br/>

3.<b> denotes Bold </b><br/>

The output of the out.jsp is shown in the browser in 18-1.

Figure 18-1 out.jsp Page

Fn:length function

The Fn:length function is used to return the number of characters in a string, or the number of elements in the collection and array, with the following syntax:

Fn:length (source)----int

The source parameter above can be a string, a collection, or an array, and the return type is int.

The length.jsp of routine 18-2 demonstrates the use of the Fn:length function.

Routine 18-2 length.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "

pageencoding= "UTF-8"%>

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

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

<% @page import= "Java.util.ArrayList"%>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>

<title>length</title>

<body>

<%

Int[] array = {1,2,3,4};

ArrayList list = new ArrayList ();

List.add ("one");

List.add ("both");

List.add ("three");

%>

<c:set value= "<%=array%>" var= "Array" ></c:set>

<c:set value= "<%=list%>" var= "list" ></c:set>

Array Length: ${fn:length (array)}<br/>

Collection Length: ${fn:length (list)}<br/>

String length: ${fn:length ("Tomcat")}<br/>

</body>

From the browser azimuth length.jsp, the resulting page 18-2 is shown.

Figure 18-2 Length.jsp

17 Summary

The Functions Tag library provides some common El functions, including the following.

L Fn:contains function: Used to determine whether the target string is included in the source string.

L fn:containsignorecase function: Used to determine whether the target string is contained in the source string and ignores the case when judged.

L Fn:startswith function: Used to determine whether the source string starts with the specified target string.

L Fn:endswith function: Used to determine whether the source string ends with the specified target string.

L Fn:indexof function: Used to find the target string in the source string, and returns the index of the first character in the source string that matches the target string first.

L Fn:replace function: Used to replace a part of the source string with another string and return the replaced string.

L fn:substring function: Used to get a specific substring in the source string.

L Fn:substringbefore function: Used to get the substring before the specified substring in the source string.

L Fn:substringafter function: Used to get substrings after the specified substring in the source string

L Fn:split function: Used to split the source string into an array of strings.

L Fn:join function: Used to concatenate all strings in the source string array into a single string.

L fn:tolowercase function: Used to change all characters in the source string to lowercase.

L fn:touppercase function: Used to change all characters in the source string to uppercase.

L Fn:trim function: Used to delete the beginning and end of a space in the source string.

L Fn:escapexml function: Used to convert Characters "<", ">", "" "and" & "in the source string to escape characters.

L Fn:length Function: The number of characters used to return a string, or the number of elements in the collection and array

Common usage of jstl and El Combination

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.