posts - 1,comments - 3,trackbacks - 0

JSTL使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及session中的数据的访问非常方便,但是我们在实际应用中经常需要在页面调用对象的某些方法,例如我需要调用字符串的length方法来获取字符串的长度时,在以往的开发过程中我们必须把对象先转为String类,然后在调用其length方法,这样的代码繁琐而且容易出错。

因此JSTL内置了几个用于字符串操作的方法,可以直接在表达式中使用,大大的简化了代码,提供代码的可读性。在JSTL的表达是中要使用一个函数,其格式如下

${ns:methodName(args....)}

在使用这些函数之前必须在JSP中引入标准函数的声明

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

下面是JSTL中自带的方法列表以及其描述

函数名 函数说明 使用举例
contains 判断字符串是否包含另外一个字符串 <c:if test="${fn:contains(name, searchString)}">
containsIgnoreCase 判断字符串是否包含另外一个字符串(大小写无关) <c:if test="${fn:containsIgnoreCase(name, searchString)}">
endsWith 判断字符串是否以另外字符串结束 <c:if test="${fn:endsWith(filename, ".txt")}">
escapeXml 把一些字符转成XML表示,例如<字符应该转为&lt; ${fn:escapeXml(param:info)}
indexOf 子字符串在母字符串中出现的位置 ${fn:indexOf(name, "-")}
join 将数组中的数据联合成一个新字符串,并使用指定字符格开 ${fn:join(array, ";")}
length 获取字符串的长度,或者数组的大小 ${fn:length(shoppingCart.products)}
replace 替换字符串中指定的字符 ${fn:replace(text, "-", "•")}
split 把字符串按照指定字符切分 ${fn:split(customerNames, ";")}
startsWith 判断字符串是否以某个子串开始 <c:if test="${fn:startsWith(product.id, "100-")}">
substring 获取子串 ${fn:substring(zip, 6, -1)}
substringAfter

获取从某个字符所在位置开始的子串

${fn:substringAfter(zip, "-")}
substringBefore 获取从开始到某个字符所在位置的子串 ${fn:substringBefore(zip, "-")}
toLowerCase 转为小写 ${fn.toLowerCase(product.name)}
toUpperCase 转为大写字符 ${fn.UpperCase(product.name)}
trim 去除字符串前后的空格 ${fn.trim(name)}


JSTL中自带的方法列表
面是JSTL中自带的方法列表以及其描述
contains
判断字符串是否包含另外一个字符串
<c:if test="${fn:contains(name, searchString)}">

containsIgnoreCase
判断字符串是否包含另外一个字符串(大小写无关)
<c:if test="${fn:containsIgnoreCase(name, searchString)}">

endsWith
判断字符串是否以另外字符串结束
<c:if test="${fn:endsWith(filename, ".txt")}">

escapeXml
把一些字符转成XML表示,例如<字符应该转为<
${fn:escapeXml(param:info)}

indexOf
子字符串在母字符串中出现的位置
${fn:indexOf(name, "-")}

join
将数组中的数据联合成一个新字符串,并使用指定字符格开
${fn:join(array, ";")}

length
获取字符串的长度,或者数组的大小
${fn:length(shoppingCart.products)}

replace
替换字符串中指定的字符
${fn:replace(text, "-", "?")}

split
把字符串按照指定字符切分
${fn:split(customerNames, ";")}

startsWith
判断字符串是否以某个子串开始
<c:if test="${fn:startsWith(product.id, "100-")}">

substring
获取子串
${fn:substring(zip, 6, -1)}

substringAfter
获取从某个字符所在位置开始的子串
${fn:substringAfter(zip, "-")}

substringBefore
获取从开始到某个字符所在位置的子串
${fn:substringBefore(zip, "-")}

toLowerCase
转为小写
${fn.toLowerCase(product.name)}

toUpperCase
转为大写字符
${fn.UpperCase(product.name)}

trim
去除字符串前后的空格
${fn.trim(name)}

posted on 2009-08-28 13:26 姜昆鹏 阅读(282) 评论(2)  编辑  收藏 所属分类: java

FeedBack:
# re: jstl字符串处理
2009-08-28 13:29 | 姜昆鹏
ok  回复  更多评论
  
# re: jstl字符串处理[未登录]
2009-08-28 13:32 | 哈哈
我评论评论  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: