Jstl uses expressions to simplify the code of the page, which is very convenient for some standard methods, such as the Bean's Getter/setter method, the request parameter or the context, and the data in the session. But in practical applications we often need to invoke some methods of the object on the page, for example, I need to invoke the length method of the string to get the lengths of the string, in the past development we must first convert the object to the string class, and then call its length method, Such code is cumbersome and error-prone.
So Jstl built several methods for string manipulation that can be used directly in the expression, greatly simplifying the code and providing readability of the code. The expression in Jstl is to use a function that has the following format
${ns:methodname (args ...)}
The declaration of a standard function must be introduced in the JSP before using these functions
<%@ taglib prefix= "FN" uri= "Http://java.sun.com/jsp/jstl/functions"%>
The following is a list of the methods that are in Jstl and their descriptions
Name of function |
Function description |
Examples of Use |
Fn:contains |
Determine if a string contains another string |
<c:if test= "${fn:contains (name, SearchString)}" > |
Fn:containsignorecase |
Determines whether a string contains another string (case-insensitive) |
<c:if test= "${fn:containsignorecase (name, SearchString)}" > |
Fn:endswith |
Determines whether a string ends with a different string |
<c:if test= "${fn:endswith (filename, '. txt ')}" > |
Fn:escapexml |
Turn some characters into XML representations, such as < characters should be converted to < |
${fn:escapexml (Param:info)} |
Fn:indexof |
The position where the substring appears in the parent string |
${fn:indexof (name, '-')} |
Fn:join |
Unites the data in the array into a new string and uses the specified character format to open the |
${fn:join (array, '; ')} |
Fn:length |
Gets the length of the string, or the size of the array |
${fn:length (Shoppingcart.products)} |
Fn:replace |
Replace the character specified in the string |
${fn:replace (text, '-', ' · ')} |
Fn:split |
Segmentation the string according to the specified word |
${fn:split (Customernames, '; ')} |
Fn:startswith |
Determines whether a string starts with a substring |
<c:if test= "${fn:startswith (product.id, ' 100-')}" > |
Fn:substring |
Get child string |
${fn:substring (Zip, 6,-1)} |
Fn:substringafter |
Gets the substring starting at the location of a character |
${fn:substringafter (Zip, '-')} |
Fn:substringbefore |
Gets the substring from the beginning to the location of a character |
${fn:substringbefore (Zip, '-')} |
Fn:tolowercase |
Convert to lowercase |
${fn.tolowercase (Product.name)} |
Fn:touppercase |
Convert to uppercase characters |
${fn. Uppercase (Product.name)} |
Fn:trim |
Remove spaces before and after a string |
${fn.trim (name)} |
Function |
Describe |
Fn:contains (string, substring) |
Returns true if the parameter string contains the parameter substring |
Fn:containsignorecase (string, substring) |
Returns true if the parameter string contains the parameter substring (ignoring case) |
Fn:endswith (string, suffix) |
Returns true if the argument string ends with the argument suffix |
Fn:escapexml (String) |
Convert XML (and HTML) that has special meaning to the corresponding XML character entity code, and return |
Fn:indexof (string, substring) |
Returns the position of the first occurrence of the parameter substring in the parameter string |
Fn:join (array, separator) |
Separator a given array array together with the given spacer, forming a new string and returning it. |
Fn:length (item) |
Returns the number of elements contained in the parameter item. The parameter item type is an array, a collection, or a string. If it is of type string, the return value is the number of characters in string. |
Fn:replace (String, before, after) |
Returns a String object. Replaces all occurrences of a parameter before string in a parameter string with a parameter after string, and returns the replaced result |
Fn:split (string, separator) |
Returns an array that splits the argument string with the parameter separator, and each part of the split is an element of the array |
Fn:startswith (string, prefix) |
Returns true if the argument string starts with the argument prefix |
Fn:substring (string, begin, end) |
Returns the string part of the argument, starting with the argument begin to the end position of the argument, including the character of the end position |
Fn:substringafter (string, substring) |
Returns the part of the string that follows the parameter substring in the argument string |
Fn:substringbefore (string, substring) |
Returns the part of the string that precedes the argument substring in the argument string |
Fn:tolowercase (String) |
Converts all characters of the argument string to lowercase and returns it |
Fn:touppercase (String) |
Converts all characters of the argument string to uppercase and returns it |
Fn:trim (String) |
Strips the argument string from the end of the space and returns it |
<%@ Page ContentType="TEXT/HTML;CHARSET=GBK"%><%@ taglib URI="Http://java.sun.com/jsp/jstl/core"prefix="C"%><%@ taglib URI="http://java.sun.com/jsp/jstl/functions"prefix="fn"%><HTML><Head><title>This is Jstl's test.</title></Head><Body><C:setvar= "Str"value= "Stringstring"/><C:setvar= "T"value= "TR"/><C:outvalue= "${str} = ${fn:length (str)}"/><C:outvalue= "${fn:tolowercase (str)}"/><C:outvalue= "${fn:touppercase (')}"/><C:outvalue= "${fn:touppercase (' ABCDEFG ')}"/><BR><C:outvalue= "${fn:substring (' ASDFEFG ', 0,3)}"/><BR><C:outvalue= "${fn:substringafter (' asdf ', ' s ')}"/><Br><C:outvalue= "${fn:substringbefore (str, ' G ')}"/><Br><C:outvalue= "${fn:trim (' SD dew e ')}"/><Br><C:outvalue= "D SD Dew E"/><Br><C:outvalue= "${fn:replace (str, ' ing ', ' in ')}"/><Br></Body></HTML>
<C:setvar= "Teststr"value= "${analyze.command}" /> <C:choose> <C:whenTest= "${fn:length (teststr) >"> <C:outvalue= "${fn:substring (teststr, 0,)}" /> ...... </C:when> <c:otherwise> <C:outvalue= "${teststr}" /> </c:otherwise> </C:choose>
Jstl's FN function