JSTL uses expressions to simplify the code of the page, which is very convenient for some standard methods, such as the getter/setter method of beans, request parameters, context and data in session, but in actual applications, we often need to call certain methods of objects on the page. For example, when I need to call the length method of the string to get the length of the string, in the past development process, we had to convert the object to the String class first, and then call its length method. Such code is cumbersome and error-prone.
Therefore, JSTL has several built-in methods for string operation, which can be used directly in expressions, greatly simplifying the code and providing the readability of the code. In the JSTL expression, a function should be used, and its format is as follows
${ns:methodName(args....)}
Before using these functions, you must introduce a declaration of standard functions in JSP
<%@ taglib prefix="fn" uri="/jsp/jstl/functions" %>
Below is a list of methods and their descriptions that come with JSTL
Function name | Function description | Examples of use |
contains | Determine whether a string contains another string | <c:if test="${fn:contains(name, searchString)}"> |
containsIgnoreCase | Determine whether a string contains another string (case irrelevant) | <c:if test="${fn:containsIgnoreCase(name, searchString)}"> |
endsWith | Determine whether the string ends with another string | <c:if test="${fn:endsWith(filename, ".txt")}"> |
escapeXml | Convert some characters to XML representation, for example < characters should be converted to < | ${fn:escapeXml(param:info)} |
indexOf | Where the substring appears in the parent string | ${fn:indexOf(name, "-")} |
join | Join the data in the array into a new string and use the specified character grid | ${fn:join(array, ";")} |
length | Get the length of the string, or the size of the array | ${fn:length()} |
replace | Replace the specified character in the string | ${fn:replace(text, "-", "•")} |
split | Slice the string according to the specified characters | ${fn:split(customerNames, ";")} |
startsWith | Determine whether a string starts with a substring | <c:if test="${fn:startsWith(, "100-")}"> |
substring | Get substrings | ${fn:substring(zip, 6, -1)} |
substringAfter |
Gets a substring that starts at the location of a character |
${fn:substringAfter(zip, "-")} |
substringBefore | Gets a substring from the beginning to the location of a character | ${fn:substringBefore(zip, "-")} |
toLowerCase | Convert to lowercase | ${()} |
toUpperCase | Convert to capital characters | ${()} |
trim | Remove spaces before and after strings | ${(name)} |