JSTL中的fn函數

來源:互聯網
上載者:User

一、概念  

JSTL內建了幾個用於字串操作的方法,可以直接在運算式中使用,大大的簡化了代碼,提供代碼的可讀性。

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

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

二、詳解

fn:contains(string, substring)

假如參數string中包含參數substring,返回true

例如:<c:if test="${fn:contains(name, searchString)}">

fn:containsIgnoreCase(string, substring)

假如參數string中包含參數substring(忽略大小寫),返回true

例如:<c:if test="${fn:containsIgnoreCase(name, searchString)}">

 
fn:endsWith(string, suffix)

假如參數 string 以參數suffix結尾,返回true

例如:<c:if test="${fn:endsWith(filename, ".txt")}">

 
fn:escapeXml(string)

將有非凡意義的XML(和HTML)轉換為對應的XML character entity code,如<字元應該轉為&lt,並返回

例如: ${fn:escapeXml(param:info)}

 
fn:indexOf(string, substring)

返回參數substring在參數string中第一次出現的位置

${fn:indexOf(name, "-")}

 
fn:join(array, separator)

將一個給定的數組array用給定的間隔符separator串在一起,組成一個新的字串並返回。

${fn:join(array, ";")}

 

fn:length(item)

返回參數item中包含元素的數量。參數Item類型是數組、collection或者String。假如是String類型,傳回值是String中的字元數。

${fn:length(shoppingCart.products)}

 
fn:replace(string, before, after)

返回一個String對象。用參數after字串替換參數string中所有出現參數before字串的地方,並返回替換後的結果

${fn:replace(text, "-", "&#149;")}

fn:split(string, separator)

返回一個數組,以參數separator 為分割符分割參數string,分割後的每一部分就是數組的一個元素

${fn:split(customerNames, ";")}


 

fn:startsWith(string, prefix)

假如參數string以參數prefix開頭,返回true

<c:if test="${fn:startsWith(product.id, "100-")}">


 

fn:substring(string, begin, end)

返回參數string部分字串,從參數begin開始到參數end位置,包括end位置的字元

${fn:substring(zip, 6, -1)}

fn:substringAfter(string, substring)

返回參數substring在參數string中後面的那一部分字串

${fn:substringAfter(zip, "-")}

fn:substringBefore(string, substring)

返回參數substring在參數string中前面的那一部分字串

${fn:substringBefore(zip, "-")}

fn:toLowerCase(string)

將參數string所有的字元變為小寫,並將其返回

${fn.toLowerCase(product.name)}

 
fn:toUpperCase(string)

將參數string所有的字元變為大寫,並將其返回

${fn.UpperCase(product.name)}

fn:trim(string)

去除參數string 首尾的空格,並將其返回

${fn.trim(name)}

三、例子

<c:set var="testStr" value="${analyze.command}" />  

<c:choose>  
<c:when test="${fn:length(testStr) > 100}">  
<c:out value="${fn:substring(testStr, 0, 100)}" />  ......  
</c:when>  
<c:otherwise>  
<c:out value="${testStr}" />  
</c:otherwise>  
</c:choose> 

原帖地址

http://blog.csdn.net/donghustone/article/details/6711999

http://jakend.iteye.com/blog/654882

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.