EL函数库(fn方法库)
- 由于在JSP页面中显示数据时,经常需要对显示的字符串进行处理,SUN公司针对于一些常见处理定义了一套EL函数库供开发者使用。
- 其实EL函数库就是fn方法库,是JSTL标签库中的一个库,也有人称之为fn标签库,但是该库长得不像是标签,所以称之为fn方法库
- 既然作为JSTL标签库中的一个库,要使用fn方法库就需要导入JSTL标签!要想使用JSTL标签库就要导入jstl.jar和standard.jar包!
- 所以,要对fn方法库做测试,首先导入开发包(jstl.jar、standard.jar)
- 在JSP页面中指明使用标签库
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
- fn方法库全都是跟字符串有关的(可以把它想成是String的方法)
- fn:toLowerCase
- fn:toUpperCase
- fn:trim
- fn:length
- fn:split
- fn:join 【接收字符数组,拼接字符串】
- fn:indexOf
- fn:contains
- fn:startsWith
- fn:replace
- fn:substring
- fn:substringAfter
- fn:endsWith
- fn:escapeXml【忽略XML标记字符】
- fn:substringBefore
- 测试代码:
contains:${fn:contains("zhongfucheng",zhong )}<br> containsIgnoreCase:${fn:containsIgnoreCase("zhongfucheng",ZHONG )}<br> endsWith:${fn:endsWith("zhongfucheng","eng" )}<br> escapeXml:${fn:escapeXml("<zhongfucheng>你是谁呀</zhongfucheng>")}<br> indexOf:${fn:indexOf("zhongfucheng","g" )}<br> length:${fn:length("zhongfucheng")}<br> replace:${fn:replace("zhongfucheng","zhong" ,"ou" )}<br> split:${fn:split("zhong,fu,cheng","," )}<br> startsWith:${fn:startsWith("zhongfucheng","zho" )}<br> substring:${fn:substring("zhongfucheng","2" , fn:length("zhongfucheng"))}<br> substringAfter:${fn:substringAfter("zhongfucheng","zhong" )}<br> substringBefore:${fn:substringBefore("zhongfucheng","fu" )}<br> toLowerCase:${fn:toLowerCase("zhonGFUcheng")}<br> toUpperCase:${fn:toUpperCase("zhongFUcheng")}<br> trim:${fn:trim(" zhong fucheng ")}<br> <%--将分割成的字符数组用"."拼接成一个字符串--%> join:${fn:join(fn:split("zhong,fu,cheng","," ),"." )}<br>
- 效果:
- 使用fn方法库数据回显
<% User user = new User(); String likes[] = {"sing"}; user.setLikes(likes); //数据回显 request.setAttribute("user",user); %> <%--java的字符数组以","号分割开,首先拼接成一个字符串,再判读该字符串有没有包含关键字,如果有就checked--%> <input type="checkbox"${ fn:contains(fn:join(user.likes,","),"sing")?'checked':'' }>唱歌 <input type="checkbox"${ fn:contains(fn:join(user.likes,","),"dance")?'checked':'' }>跳舞
- 效果:
原文链接:https://dwz.cn/bOdF7S6R
作者:Java3y