19.【Function标签库】

function标签库简介

JSTL Functions 标签库中提供了一组常用的 EL 函数,主要用于处理字符串,在 JSP 中可以直接使用这些函数。
在 JSP 文件中使用 Functions 标签库,要先通过 taglib 指令引入该标签库:
<%@taglib uri=”http://java.sun.com/jsp/jstl/functions” prefix=”fn” %.

fn:contains 函数

fn:contains 函数用于判断在源字符串中是否包含目标字符串,其语法为:
fn:contains(String source,String target) -------boolean;
以上 source 参数指定源字符串, target 参数指定目标字符串,返回类型为 boolean 。
例如对于以下 EL 表达式:
${fn:contains(“Tomcat”,”cat”)}
${fn:contains(“Tomcat”,”CAT”)}
第一个 EL 表达式的值为 true ,第二个 EL 表达式的值为 false 。

fn:containsIgnoreCase 函数

fn:containsIgnoreCase 函数用于判断在源字符串中是否包含目标字符串,并且在判断时忽略大小写,其语法为:
fn: containsIgnoreCase (String source,String target) -------boolean;
以上 source 参数指定源字符串, target 参数指定目标字符串,返回类型为 boolean 。
例如对于以下 EL 表达式:
${fn: containsIgnoreCase (“Tomcat”,”CAT”)}
${fn: containsIgnoreCase (“Tomcat”,”Mike”)}
第一个 EL 表达式的值为 true ,第二个 EL 表达式的值为 false 。

fn:startsWith 函数

fn:startsWith 函数用于判断源字符串是否以指定的目标字符串开头,其语法为:
fn:startsWith(String source,String target) ----boolean
以上 source 参数指定源字符串, target 参数指定目标字符串,返回类型为 boolean 。
例如对于以下 EL 表达式:
${fn: startsWith (“Tomcat”,”Tom”)}
${fn: startsWith (“Tomcat”,”cat”)}
第一个 EL 表达式的值为 true ,第二个 EL 表达式的值为 false 。

fn:endsWith 函数

fn: endsWith 函数用于判断源字符串是否以指定的目标字符串结尾,其语法为:
fn: endsWith (String source,String target) ----boolean
以上 source 参数指定源字符串, target 参数指定目标字符串,返回类型为 boolean 。
例如对于以下 EL 表达式:
${fn: endsWith (“Tomcat”,”cat”)}
${fn: endsWith (“Tomcat”,”Tom”)}
第一个 EL 表达式的值为 true ,第二个 EL 表达式的值为 false 。

fn:indexOf 函数

fn:indexOf 函数用于在源字符串中查找目标字符串,并返回源字符串中最先与目标字符串匹配的第一个字符的索引,如果在源字符串中不包含目标字符串,就返回 -1 ,源字符串中的第一个字符的索引为 0 。 fn:indexOf 函数的语法为:
fn: indexOf (String source,String target) ----int
以上 source 参数指定源字符串, target 参数指定目标字符串,返回类型为 int 。
例如对于以下 EL 表达式:
1 ${fn: indexOf (“Tomcat”,”cat”)}

2 ${fn: indexOf (“2211221”,”21”)}

3 ${fn: indexOf (“Tomcat”,”Mike”)}

其输出结果为:
1 3
2 1
3 -1

fn:replace 函数

fn:replace 函数用于把源字符串中的一部分替换为另外的字符串,并返回替换后的字符串。 fn:replace 函数的语法为:
fn: replace (String source,String before,String after) ----String
以上 source 参数指定源字符串, before 参数指定源字符串中被替换的子字符串, after 参数指定用于替换的子字符串,返回类型为 String 。
例如对于以下 EL 表达式:
1 ${ fn: replace(“TomcAt”,”cAt”,”cat”)}

2 ${ fn: replace(“2008/1/9”,”/”,”-”)}

其输出结果为:
1 Tomcat
2 2008-1-9

fn:substring 函数

fn:substring 函数用于获取源字符串中的特定子字符串,它的语法为:
fn:substring(String source,int beginIndex,int endIndex) ------String
以上 source 参数指定源字符串, beginIndex 参数表示子字符串中的第一个字符在源字符串中的索引,endIndex 参数表示子字符串的最后一个字符在源字符串中的索引加 1 ,返回类型为 String ,源字符串中的第一个字符的索引为 0 。
例如对于以下 EL 表达式:
1 ${ fn: substring (“Tomcat”,0,3)}

2 ${ fn: substring (“Tomcat”,3,”6”)}

其输出结果为:
1 Tom
2 cat

fn:substringBefore 函数

fn:substringBefore 函数用于获取源字符串中指定子字符串之前的子字符串,其语法为:
fn:substringBefore(String source,String target) ----String
以上 source 参数指定源字符串, target 参数指定子字符串,返回类型为 String 。如果在源字符串中不包含特定子字符串,就返回空字符串。
例如对于以下 EL 表达式:
1 ${ fn: substringBefore (“Tomcat”,”cat”)}

2 ${ fn: substringBefore (“mydata.txt”,”.txt”)}

其输出结果为:
1 Tom
2 mydata

fn:substringAfter 函数

fn: substringAfter 函数用于获取源字符串中指定子字符串之后的子字符串,其语法为:
fn: substringAfter (String source,String target) ----String
以上 source 参数指定源字符串, target 参数指定子字符串,返回类型为 String 。如果在源字符串中不包含特定子字符串,就返回空字符串。
例如对于以下 EL 表达式:
1 ${ fn: substringAfter (“Tomcat”,”Tom”)}

2 ${ fn: substringAfter (“mydata.txt”,” mydata.”)}

其输出结果为:
1 cat
2 txt

fn:split 函数

fn:split 函数用于将源字符串拆分为一个字符串数组,其语法为:
fn: split (String source,String delimiter) ----String[]
以上 source 参数指定源字符串, delimiter 参数指定用于拆分源字符串的分隔符,返回类型为 String[] 。如果在源字符串中不包含 delimiter 参数指定的分隔符,或者 delimiter 参数为 null ,那么在返回的字符串数组中只有一个元素,为源字符串。
例如对于以下 EL 表达式:

<c:set value='${fn: split("www.mywebsite.org",".")}' var="strs"/>
<c:forEach var="token" item="${strs}">
${token}<br/>
</c:forEach>     

其输出结果为:
www
mywebsite
org
再例如对于以下代码:

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

${strs[0]}
其输出结果为:
www.mywebsite.org

fn:join 函数

fn:join 函数用于将源字符串数组中的所有字符串连接为一个字符串,其语法为:
fn:join(String source[],String separator) ----String
以上 source 参数指定源字符串数组, separator 参数指定用于连接源字符串数组中的各个字符串的分隔符,返回类型为 String 。
例如对于以下代码:

<%
    String strs[] = {"www","mywebsite","org"};
%>
<c:set value="<%=strs%>" var="strs"/>
${fn:join(strs,".")}

其输出结果为:
www. mywebsite. org

fn:toLowerCase 函数

fn:toLowerCase 函数用于将源字符串中的所有字符改为小写,其语法为:
fn:toLowerCase(String source) -----String
以上 source 参数指定源字符串,返回类型为 String 。
例如对于以下 EL 表达式:
fn:toLowerCase(“TomCat”)
其输出结果为:
tomcat

fn:toUpperCase 函数

fn: toUpperCase 函数用于将源字符串中的所有字符改为大写,其语法为:
fn: toUpperCase (String source) -----String
以上 source 参数指定源字符串,返回类型为 String 。
例如对于以下 EL 表达式:
fn: toUpperCase (“TomCat”)
其输出结果为:
TOMCAT

fn:trim 函数

fn:trim 函数用于将源字符串中的开头和末尾的空格删除,其语法为:
fn:trim(String source) ----String
以上 source 参数指定源字符串,返回类型为 String 。
例如对于以下 EL 表达式:
fn:trim(“ Tomcat ”)
以上 EL 表达式的值为“ Tomcat ”。

fn:escapeXml 函数

fn:escapeXml 函数用于将源字符串中的字符“ < ”、“ > ”、“ ” ”和“ & ”等转换为转义字符,本书第 1 章的 1.2 节( HTML 简介)介绍了转义字符的概念。 fn:escapeXml 函数的行为与 <c:out> 标签的 escapeXml 属性为 true 时的转换行为相同, fn:escapeXml 函数的语法为:
fn:escapeXml(String source) ----String
以上 source 参数指定源字符串,返回类型为 String 。

   1.${fn:escapeXml("<b> 表示粗体字 </b>") }<br/>
    2.<c:out value="<b> 表示粗体字 </b>" escapeXml="true"></c:out><br/>
    3.${"<b> 表示粗体字 </b>"}<br/>

fn:length 函数

fn:length 函数用于返回字符串中的字符的个数,或者集合和数组的元素的个数,其语法为:
fn:length(source) ---- int
以上 source 参数可以为字符串、集合或者数组,返回类型为 int 。

  <body>
    <%
        int[] array = {1,2,3,4};
        ArrayList list = new ArrayList();
        list.add("one");
        list.add("two");
        list.add("three");
    %>
    <c:set value="<%=array%>" var="array"></c:set>
    <c:set value="<%=list%>" var="list"></c:set>
    数组长度: ${fn:length(array)}<br/>
    集合长度: ${fn:length(list)}<br/>
    字符串长度: ${fn:length("Tomcat")}<br/>
    </body>

猜你喜欢

转载自blog.csdn.net/lcachang/article/details/82949214