JSTL-Functions标签

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

1.长度函数fn:length

    集合类型的大小或String类型的长度

   

	<%
		String val = "abcdefg";
		request.setAttribute("val", val);

		List<String> list = new ArrayList<String>();
		list.add("a");
		list.add("b");
		list.add("c");
		request.setAttribute("list", list);
	%>
	val的长度:${fn:length(val) }
	<br /> 
	List的大小:${fn:length(list) }

    输出:

        val的长度:7
        List的大小:3

2.判断函数fn:contains

    判断源字符串是否包含子字符串。

    ${fn:contains("ABC", "C")}
    ${fn:contains("ABC", "c")}

    结果:true false

3.fn:containsIgnoreCase

    判断源字符串是否包含子字符串,忽略大小写。

    ${fn:containsIgnoreCase("ABC", "C")}
    ${fn:containsIgnoreCase("ABC", "c")}

    结果:true true

4.词头判断函数fn:startsWith

    ${fn:startsWith("ABC","AB") }
    ${fn:startsWith("ABC","Ab") }

    结果:true false

5.词尾判断函数fn:endsWith

    ${fn:endsWith("ABC","BC") }
    ${fn:endsWith("ABC","Bc") }

    结果:true false

6.字符实体转换函数fn:escapeXml

    <font color="red" >TEST</font><br/>
    <c:out value="<font color='red' >TEST</font>"></c:out><br/>
    <c:out value="<font color='red' >TEST</font>" escapeXml="false"></c:out><br/>
    ${fn:escapeXml("<font color='red' >TEST</font>") }

    输出:

    TEST
    <font color='red' >TEST</font>
    TEST
    <font color='red' >TEST</font>

7.字符匹配函数fn:indexOf

    用于取得子字符串与源字符串匹配的开始位置,若子字符串与源字符串中的内容没有匹配成功将返回“-1”。

    ${fn:indexOf("ABC","BC") }
    ${fn:indexOf("ABC","B") }
    ${fn:indexOf("ABC","C") }
    ${fn:indexOf("ABC","ABC") }
    ${fn:indexOf("ABC","aBC") }

    结果:1 1 2 0 -1

8.分隔符函数fn:join

    fn:join函数允许为一个字符串数组中的每一个字符串加上分隔符,并连接起来。

   

	<%
		String[] arrs=new String[3];
		arrs[0]="a";
		arrs[1]="BB";
		arrs[2]="ccc";
		request.setAttribute("arrs", arrs);
	%>
	${fn:join(arrs,",") }
	<br/>
	${fn:join(arrs,"AA") }

    输出:

    a,BB,ccc
    aAABBAAccc

9.替换函数fn:replace

    inputString 源字符串。其类型必须为String类型 

    beforeSubstring 指定被替换字符串。其类型必须为String类型 

    afterSubstring 指定替换字符串。其类型必须为String类型 

   

	${fn:replace("ABC","A","B") }
	${fn:replace("ABC","a","b") }
	${fn:replace("ABC","BC","bc") }

    输出:BBC ABC Abc

10.分隔符转换数组函数fn:split

   

	${fn:split("a,bb,ccc",",") }
	<br/>
	<c:forEach items="${fn:split('a,bb,ccc',',') }" var="item">
		${item }
	</c:forEach>

输出:[Ljava.lang.String;@6d8ced6
           a bb ccc

11.字符串截取函数fn:substring

    string 源字符串。其类型必须为String类型 
    beginIndex 指定起始下标(值从0开始)。其类型必须为int类型 
    endIndex 指定结束下标(值从0开始)。其类型必须为int类型

   

	${fn:substring("ABC",0,1) },
	${fn:substring("ABC",0,2) },
	${fn:substring("ABC",0,3) },
	${fn:substring("ABC",1,2) }

 输出:A, AB, ABC, B

12.起始到定位截取字符串函数fn:substringBefore

    ${fn:substringBefore("ABC","BC") }
    ${fn:substringBefore("ABC","C") }

    结果:A AB

13.定位到终止截取字符串函数fn:substringAfter

    ${fn:substringAfter("ABC","A") }
    ${fn:substringAfter("ABC","AB") }

    结果:BC C

14.小写转换函数fn:toLowerCase、大写转换函数fn:toUpperCase

    ${fn:toLowerCase("Abc") }
    ${fn:toLowerCase("ABC") }
    ${fn:toUpperCase("Abc") }
    ${fn:toUpperCase("abc") }

    结果:abc abc ABC ABC

15.空格删除函数fn:trim

    "${fn:trim(" ABC ") }"
    "${fn:trim(" ABC") }"
    "${fn:trim("ABC ") }"

    结果:"ABC" "ABC" "ABC"

猜你喜欢

转载自xujava.iteye.com/blog/2026013