JSTL(Java Server Pages Standard Tag Library)标签函数库

JSTL(Java Server Pages Standard Tag Library)标签函数库
本文地址:http://blog.csdn.net/shanglianlm/article/details/52288497

JSTL所提供的标签函数库主要分为五大类:

  1. 核心标签库 (Core tag library)
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  2. I18N 格式标签库 (I18N-capable formatting tag library)
    <%@taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
  3. SQL 标签库 (SQL tag library)
    <%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
  4. XML 标签库 (XML tag library)
    <%@taglib prefix="x" uri="http://java.sun.com/jsp/jstl/xml" %>
  5. 函数标签库 (Functions tag library)
    <%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>

1 核心标签库 (Core tag library)

JSTL核心标签库是对 JSP 页面一般处理的封装。在该标签库中的标签一共有 14 个,分为四类:

  • 通用核心标签: <c:out> 、 <c:set> 、 <c:remove> 、 <c:catch>
  • 条件控制标签: <c:if> 、 <c:choose> 、 <c:when> 、 <c:otherwise>
  • 循环控制标签: <c:forEach> 、 <c:forTokens>
  • URL 相关标签: <c:import> 、 <c:url> 、 <c:redirect> 、 <c:param>

<c:out>

<c:out>主要用来显示数据的内容

<c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"] />
  • value 为需要显示出来的值。
  • 默认会将 <、>、’、” 和 & 转换为 &lt;、&gt;、&#039;、&#034; 和 &amp;。若不想转换时,只需要设定escapeXml 为fasle 即可。
  • 如果value的值为null则显示default的值。

<c:set>

<c:set>主要用来将变量储存至JSP范围中或是JavaBean的属性中。

<c:set value="value" var="varName" [scope="{ page|request|session|application }"]/>
或者
<c:set value="value" target="target" property="propertyName" />

<c:remove>

<c:remove>主要用来移除变量。

<c:remove var="varName" [scope="{ page|request|session|application }"] />
  • 若我们不设定scope,则<c:remove>将会从Page、Request、Session 及Application 中顺序寻找是否存在名称为number 的数据,若能找到时,则将它移除掉,反之则不会做任何的事情。

<c:catch>

<c:catch>主要用来处理产生错误的异常状况,并且将错误信息储存起来。

<c:catch [var="varName"] >
  … 欲抓取错误的部分
</c:catch>

<c:if>

<c:if test="testCondition" [var="varName"] [scope="{page|request|session|application}"]>
  具体内容
</c:if>

<c:choose> 、 <c:when> 、 <c:otherwise>

<c:set var="score">85</c:set>
<c:choose>
  <c:when test="${score>=90}">
    你的成绩为优秀!
  </c:when>
  <c:when test="${score>=70&&score<90}">
    您的成绩为良好!
  </c:when>
  <c:when test="${score>60&&score<70}">
    您的成绩为及格
  </c:when>
  <c:otherwise>
    对不起,您没有通过考试!
  </c:otherwise>
</c:choose>
  • <c:choose>本身只当做 <c:when><c:otherwise> 的父标签,<c:choose>的本体内容只能有:空白、1 或多个 <c:when>、0 或多个<c:otherwise>

<c:forEach>

<c:forEach> 标签根据循环条件遍历集合 Collection 中的元素。

<c:forEach [var="varName"] items="collection" [varStatus="varStatusName"] [begin="begin"][end="end"] [step="step"]>
  本体内容
< /c:forEach>
  • var 用于存储从集合中取出的元素;
  • items 指定要遍历的集合;
  • varStatus 用于存放集合中元素的信息,一共有4种状态属性:index、count、first和last。
    示例
<c:out value="输出整个迭代的信息:" /><br>
   <c:forEach var="fuwa" items="${a}" begin="3" end="4" step="1" varStatus="s">
   &nbsp;<c:out value="${fuwa}" />的四种属性:<br>
   &nbsp;&nbsp;所在位置,即索引:<c:out value="${s.index}" /><br>
   &nbsp;&nbsp;总共已迭代的次数:<c:out value="${s.count}" /><br>
   &nbsp;&nbsp;是否为第一个位置:<c:out value="${s.first}" /><br>
   &nbsp;&nbsp;是否为最后一个位置:<c:out value="${s.last}" /><br>
</c:forEach>

<c:forTokens>

<c:forTokens>用来浏览一字符串中所有的成员,其成员是由定义符号(delimiters)所分隔的。

<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
  本体内容
</c:forTokens>  

<c:import>

<c:import> 把其他静态或动态文件包含到 JSP 页面。

<c:import url="url" [context="context"] [value="value"] [scope="..."] [charEncoding="encoding"]></c:import>
  • <jsp:include>的区别是后者只能包含同一个web应用中的文件,前者可以包含其他web应用中的文件,甚至是网络上的资源。

<c:url>

<c:url> 用于动态生成一个 String 类型的URL,可以同上个标签共同使用,也可以使用HTML的标签实验超链接。

<c:url value="http://127.0.0.1:8080" var="url" scope="session"></c:url>
<a href="${url}">Tomcat首页</a>

<c:redirect>

<c:redirect> 该标签用来实现请求的重定向。例如,对用户输入的用户名和密码进行验证,不成功则重定向到登录页面。或者实现Web应用不同模块之间的衔接

<c:redirect url="http://127.0.0.1:8080">
    <c:param name="uname">lihui</c:param>
    <c:param name="password">11111</c:param>
</c:redirect>

<c:param>

<c:param>用来给包含或重定向的页面传递参数,常与其他标签配合使用。

<c:param name="uname">lihui</c:param>
<c:param name="password">11111</c:param>

2 I18N 格式标签库 (I18N-capable formatting tag library)

JSTL格式化标签用来格式化并输出文本、日期、时间、数字。

<fmt:formatNumber>

<fmt:formatNumber>使用指定的格式或精度格式化数字

<h3>数字格式化:</h3>
<c:set var="balance" value="120000.2309" />
<p>格式化数字 (1): <fmt:formatNumber value="${balance}"  type="currency"/></p>
<p>格式化数字 (2): <fmt:formatNumber type="number" maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (3): <fmt:formatNumber type="number" maxFractionDigits="3" value="${balance}" /></p>
<p>格式化数字 (4): <fmt:formatNumber type="number" groupingUsed="false" value="${balance}" /></p>
<p>格式化数字 (5): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (6): <fmt:formatNumber type="percent" minFractionDigits="10" value="${balance}" /></p>
<p>格式化数字 (7): <fmt:formatNumber type="percent" maxIntegerDigits="3" value="${balance}" /></p>
<p>格式化数字 (8): <fmt:formatNumber type="number" pattern="###.###E0" value="${balance}" /></p>
<p>美元 :<fmt:setLocale value="en_US"/>
<fmt:formatNumber value="${balance}" type="currency"/></p>

<fmt:parseNumber>

<fmt:parseNumber> 解析一个代表着数字,货币或百分比的字符串

<h3>数字解析:</h3>
<c:set var="balance" value="1250003.350" />
<fmt:parseNumber var="i" type="number" value="${balance}" />
<p>数字解析 (1) : <c:out value="${i}" /></p>
<fmt:parseNumber var="i" integerOnly="true" type="number" value="${balance}" />
<p>数字解析 (2) : <c:out value="${i}" /></p>

<fmt:formatDate>

<fmt:formatDate>使用指定的风格或模式格式化日期和时间

<h3>日期格式化:</h3>
<c:set var="now" value="<%=new java.util.Date()%>" />
<p>日期格式化 (1): <fmt:formatDate type="time" value="${now}" /></p>
<p>日期格式化 (2): <fmt:formatDate type="date" value="${now}" /></p>
<p>日期格式化 (3): <fmt:formatDate type="both" value="${now}" /></p>
<p>日期格式化 (4): <fmt:formatDate type="both" dateStyle="short" timeStyle="short" value="${now}" /></p>
<p>日期格式化 (5): <fmt:formatDate type="both" dateStyle="medium" timeStyle="medium" value="${now}" /></p>
<p>日期格式化 (6): <fmt:formatDate type="both" dateStyle="long" timeStyle="long" value="${now}" /></p>
<p>日期格式化 (7): <fmt:formatDate pattern="yyyy-MM-dd" value="${now}" /></p>

<fmt:parseDate>

<fmt:parseDate>解析一个代表着日期或时间的字符串

<h3>日期解析:</h3>
<c:set var="now" value="20-10-2015" />
<fmt:parseDate value="${now}" var="parsedEmpDate" pattern="dd-MM-yyyy" />
<p>解析后的日期为: <c:out value="${parsedEmpDate}" /></p>

<fmt:bundle>

<fmt:bundle> 绑定资源

<fmt:bundle baseName="<string>" prefix="<string>"/>
  • basename 指定被载入的资源束的基础名称
  • prefix 指定标签key属性的前缀
    示例
package com.runoob;

import java.util.ListResourceBundle;

public class Example_En extends ListResourceBundle {
  public Object[][] getContents() {
    return contents;
  }
  static final Object[][] contents = {
  {"count.one", "One"},
  {"count.two", "Two"},
  {"count.three", "Three"},
  };
}

编译以上文件为Examble.class,然后放在Web应用程序的CLASSPATH能找到的地方。

<fmt:bundle basename="com.runoob.Example" prefix="count.">
   <fmt:message key="one"/><br/>
   <fmt:message key="two"/><br/>
   <fmt:message key="three"/><br/>
</fmt:bundle>

<fmt:setLocale>

<fmt:setLocale>指定地区

<fmt:setLocale value="<string>" variant="<string>" scope="<string>"/>
  • value 指定ISO-639 语言码和ISO-3166 国家码,默认为en_US
  • variant 特定浏览器变体
  • scope Locale配置变量的作用域
    示例
<!-- 修改地区-->
<fmt:setLocale value="es_ES"/>

<fmt:setBundle>

<fmt:setBundle>指定资源

<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.runoob.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>

<fmt:timeZone>

<fmt:timeZone>指定时区

<c:set var="now" value="<%=new java.util.Date()%>" />
<fmt:timeZone value="${zone}">
     <fmt:formatDate value="${now}" timeZone="${zn}" type="both" />
</fmt:timeZone>

<fmt:setTimeZone>

<fmt:setTimeZone> 指定时区

<c:set var="now" value="<%=new java.util.Date()%>" />
<p>当前时区时间: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" /></p>
<p>修改为 GMT-8 时区:</p>
<fmt:setTimeZone value="GMT-8" />
<p>Date in Changed Zone: <fmt:formatDate value="${now}" type="both" timeStyle="long" dateStyle="long" /></p>

<fmt:message>

<fmt:message>显示资源配置文件信息,映射一个关键字给局部消息,然后执行参数替换。

<fmt:message key="<string>" bundle="<string>" var="<string>" scope="<string>"/>
  • key 要检索的消息关键字
  • bundle 要使用的资源束
  • var 存储局部消息的变量名
  • scope var属性的作用域
    示例
<fmt:setLocale value="en"/>
<fmt:setBundle basename="com.runoob.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>

<fmt:requestEncoding>

<fmt:requestEncoding>设置request的字符编码,用来指定返回给Web应用程序的表单编码类型。

<fmt:requestEncoding value="UTF-8" />
<fmt:setLocale value="es_ES"/>
<fmt:setBundle basename="com.runoob.Example" var="lang"/>

<fmt:message key="count.one" bundle="${lang}"/><br/>
<fmt:message key="count.two" bundle="${lang}"/><br/>
<fmt:message key="count.three" bundle="${lang}"/><br/>

3 SQL 标签库 (SQL tag library)

JSTL SQL标签库提供了与关系型数据库(Oracle,MySQL,SQL Server等等)进行交互的标签。

<sql:setDataSource>

<sql:setDataSource>指定数据源

<sql:setDataSource var="<string>" scope="<string>" dataSource="<string>" driver="<string>" url="<string>" user="<string>" password="<string>"/>
  • driver 要注册的JDBC驱动
  • url 数据库连接的JDBC URL
  • user 数据库用户名
  • password 数据库密码
  • dataSource 事先准备好的数据库
  • var 代表数据库的变量
  • scope var属性的作用域
    示例
<sql:setDataSource var="snapshot" driver="com.mysql.jdbc.Driver"
     url="jdbc:mysql://localhost/TEST" user="user_id"  password="mypassword"/>

<sql:query>

<sql:query>运行SQL查询语句

<sql:query var="<string>" scope="<string>" sql="<string>" dataSource="<string>" startRow="<string>" maxRows="<string>"/>

示例

<sql:query dataSource="${snapshot}" var="result">
  SELECT * from Employees;
</sql:query>

<sql:update>

<sql:update>运行SQL更新语句

<sql:update var="<string>" scope="<string>" sql="<string>" dataSource="<string>"/>

示例

<sql:update dataSource="${snapshot}" var="count">
   INSERT INTO Employees VALUES (104, 2, 'Nuha', 'Ali');
</sql:update>

<sql:param>

<sql:param>将SQL语句中的参数设为指定值

<c:set var="empId" value="103"/>

<sql:update dataSource="${snapshot}" var="count">
  DELETE FROM Employees WHERE Id = ?
  <sql:param value="${empId}" />
</sql:update>

<sql:dateParam>

<sql:dateParam> 将SQL语句中的日期参数设为指定的java.util.Date 对象值

<sql:dateParam value="<string>" type="<string>"/>
  • value 需要设置的日期参数(java.util.Date)
  • type DATE (只有日期),TIME(只有时间), TIMESTAMP (日期和时间)
<%
 Date DoB = new Date("2001/12/16");
 int studentId = 100;
%>
<sql:update dataSource="${snapshot}" var="count">
   UPDATE Students SET dob = ? WHERE Id = ?
   <sql:dateParam value="<%=DoB%>" type="DATE" />
   <sql:param value="<%=studentId%>" />
</sql:update>

<sql:transaction>

<sql:transaction>在共享数据库连接中提供嵌套的数据库行为元素,将所有语句以一个事务的形式来运行

<sql:transaction dataSource="<string>" isolation="<string>"/>
  • dataSource 所使用的数据库(覆盖默认值)
  • isolation 事务隔离等级 (READ_COMMITTED,,READ_UNCOMMITTED, REPEATABLE_READ或 SERIALIZABLE)
    示例
<sql:transaction dataSource="${snapshot}">
   <sql:update var="count">
      UPDATE Students SET last = 'Ali' WHERE Id = 102
   </sql:update>
   <sql:update var="count">
      UPDATE Students SET last = 'Shah' WHERE Id = 103
   </sql:update>
   <sql:update var="count">
     INSERT INTO Students VALUES (104,'Nuha', 'Ali', '2010/05/26');
   </sql:update>
</sql:transaction>

4 XML 标签库 (XML tag library)

JSTL XML标签库提供了创建和操作XML文档的标签。

 在使用xml标签前,你必须将XML 和 XPath 的相关包拷贝至你的<Tomcat 安装目录>\lib下:
XercesImpl.jar
- 下载地址: http://www.apache.org/dist/xerces/j/
xalan.jar
- 下载地址: http://xml.apache.org/xalan-j/index.html

<x:out>

<x:out><%= ... >,类似,不过只用于XPath表达式

<x:out select="<string>" escapeXml="<true|false>"/>
  • select 需要计算的XPath表达式,通常使用XPath 变量。
  • escapeXml 是否忽略XML 特殊字符
    示例
<c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

<x:parse xml="${xmltext}" var="output"/>
<b>The title of the first book is</b>: 
<x:out select="$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>: 
<x:out select="$output/books/book[2]/price" />

<x:parse>

<x:parse>解析 XML 数据

<x:parse var="<string>" varDom="<string>" scope="<string>" scopeDom="<string>" doc="<string>" systemId="<string>" filter="<string>"/>
  • var 包含已解析XML数据的变量
  • xml 需要解析的文档的文本内容(String或Reader)
  • systemId 系统标识符URI,用来解析文档
  • filter 应用于源文档的过滤器
  • doc 需要解析的XML文档
  • scope var属性的作用域
  • varDom 包含已解析XML数据的变量
  • scopeDom varDom属性的作用域
    示例
    books.xml
<books>
<book>
  <name>Padam History</name>
  <author>ZARA</author>
  <price>100</price>
</book>
<book>
  <name>Great Mistry</name>
  <author>NUHA</author>
  <price>2000</price>
</book>
</books>

main.jsp

<h3>Books Info:</h3>
<c:import var="bookInfo" url="http://localhost:8080/books.xml"/>

<x:parse xml="${bookInfo}" var="output"/>
<b>The title of the first book is</b>: 
<x:out select="$output/books/book[1]/name" />
<br>
<b>The price of the second book</b>: 
<x:out select="$output/books/book[2]/price" />

<x:set>

<x:set> 设置XPath表达式

<%-- 后面将一直用到下面这段代码。--%>
<c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

<x:parse xml="${xmltext}" var="output"/>
<x:set var="fragment" select="$output//book"/>
<b>The price of the second book</b>: 
<c:out value="${fragment}" />
  • 如果XPath表达式的值是boolean类型,则<x:set>将会设置一个java.lang.Boolean对象,若是字符串,则设置一个java.lang.String对象,若是数字,则设置一个java.lang.Number对象。

<x:if>

<x:if>判断XPath表达式,若为真,则执行本体中的内容,否则跳过本体

<x:parse xml="${xmltext}" var="output"/>
<x:if select="$output//book">
   Document has at least one <book> element.
</x:if>
<br />
<x:if select="$output/books[1]/book/price > 100">
   Book prices are very high
</x:if>

<x:forEach>

<x:forEach>迭代XML文档中的节点

<x:forEach var="<string>" select="<string>" begin="<int>" end="<int>" step="<int>" varStatus="<string>">

示例

<x:forEach select="$output/books/book/name" var="item">
   <li>Book Name: <x:out select="$item" /></li>
</x:forEach>

<x:choose><x:when><x:otherwise>##

<x:choose>
 <x:when select="<string>">
     ...
 </x:when>
 <x:when select="<string>">
     ...
 </x:when>
     ...
     ...
 <x:otherwise>
     ...
 </x:otherwise>
</x:choose>

<x:transform>

<x:transform> 将XSL转换应用在XML文档中

<x:transform var="<string>" scope="<string>" result="<string>" doc="<string>" docSystemId="<string>" xslt="<string>" xsltSystemId="<string>"/>
  • doc 源XML文档
  • docSystemId 源XML文档的URI
  • xslt XSLT 样式表
  • xsltSystemId 源XSLT文档的URI
  • result 接收转换结果的对象
  • var 代表被转换的XML文档的变量
  • scope var属性的作用域

style.xsl

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl=
"http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output method="html" indent="yes"/>

<xsl:template match="/">
  <html>
  <body>
   <xsl:apply-templates/>
  </body>
  </html>
</xsl:template>

<xsl:template match="books">
  <table border="1" width="100%">
    <xsl:for-each select="book">
      <tr>
        <td>
          <i><xsl:value-of select="name"/></i>
        </td>
        <td>
          <xsl:value-of select="author"/>
        </td>
        <td>
          <xsl:value-of select="price"/>
        </td>
      </tr>
    </xsl:for-each>
  </table>
</xsl:template>

main.jsp

<h3>Books Info:</h3>
<c:set var="xmltext">
  <books>
    <book>
      <name>Padam History</name>
      <author>ZARA</author>
      <price>100</price>
    </book>
    <book>
      <name>Great Mistry</name>
      <author>NUHA</author>
      <price>2000</price>
    </book>
  </books>
</c:set>

<c:import url="http://localhost:8080/style.xsl" var="xslt"/>
<x:transform xml="${xmltext}" xslt="${xslt}"/>

运行结果
这里写图片描述

<x:param>

<x:param>与`共同使用,用于设置XSL样式表

<x:param name="<string>" value="<string>"/>

5 函数标签库 (Functions tag library)

JSTL包含一系列标准函数,大部分是通用的字符串处理函数。

fn:contains()

fn:contains() 测试输入的字符串是否包含指定的子串

<c:set var="theString" value="I am from runoob"/>
<c:if test="${fn:contains(theString, 'runoob')}">
   <p>找到 runoob<p>
</c:if>

fn:containsIgnoreCase()

fn:containsIgnoreCase()测试输入的字符串是否包含指定的子串,大小写不敏感

<c:set var="theString" value="I am from runoob"/>
<c:if test="${fn:containsIgnoreCase(theString, 'runoob')}">
   <p>找到 runoob<p>
</c:if>

fn:endsWith()

fn:endsWith() 测试输入的字符串是否以指定的后缀结尾

<c:set var="theString" value="I am from runoob 123"/>
<c:if test="${fn:endsWith(theString, '123')}">
   <p>字符串以 123 结尾<p>
</c:if>

fn:escapeXml()##

fn:escapeXml() 跳过可以作为XML标记的字符

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="This <abc>is second String.</abc>"/>

<p>使用 escapeXml() 函数:</p>
<p>string (1) : ${fn:escapeXml(string1)}</p>
<p>string (2) : ${fn:escapeXml(string2)}</p>

<p>不使用 escapeXml() 函数:</p>
<p>string (1) : ${string1}</p>
<p>string (2) : ${string2}</p>

运行结果

使用 escapeXml() 函数:
string (1) : This is first String.
string (2) : This <abc>is second String.</abc>

不使用 escapeXml() 函数:
string (1) : This is first String.
string (2) : This is second String.

fn:indexOf()

fn:indexOf() 返回指定字符串在输入字符串中出现的位置

<c:set var="string1" value="This is first String."/>
<p>Index (1) : ${fn:indexOf(string1, "first")}</p>

fn:join()##

fn:join() 将数组中的元素合成一个字符串然后输出

<c:set var="string1" value="www runoob com"/>
<c:set var="string2" value="${fn:split(string1, ' ')}" />
<c:set var="string3" value="${fn:join(string2, '-')}" />
<p>字符串为 : ${string3}</p>

fn:length()

fn:length() 返回字符串长度

<c:set var="string1" value="This is first String."/>
<p>字符串长度: ${fn:length(string1)}</p>

fn:replace()

fn:replace()将输入字符串中指定的位置替换为指定的字符串然后返回

<c:set var="string1" value="I am from google"/>
<c:set var="string2" value="${fn:replace(string1, 'google', 'runoob')}" />
<p>替换后的字符串 : ${string2}</p>

fn:split()

fn:split()将字符串用指定的分隔符分隔然后组成一个子字符串数组并返回

<c:set var="string1" value="www runoob com"/>
<c:set var="string2" value="${fn:split(string1, ' ')}" />
<c:set var="string3" value="${fn:join(string2, '-')}" />
<p>string3 字符串 : ${string3}</p>

fn:startsWith()

fn:startsWith()测试输入字符串是否以指定的前缀开始

<c:set var="string" value="Runoob: I am from Runoob."/>
<c:if test="${fn:startsWith(string, 'Google')}">
   <p>字符串以 Google 开头</p>
</c:if>

fn:substring()

fn:substring()返回字符串的子集

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substring(string1, 5, 15)}" />
<p>生成的子字符串为 : ${string2}</p>

fn:substringAfter()

fn:substringAfter() 返回字符串在指定子串之后的子集

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringAfter(string1, 'is')}" />
<p>生成的子字符串 : ${string2}</p>
  • 包含 ‘is’字符串

fn:substringBefore()

fn:substringBefore()返回字符串在指定子串之前的子集

<c:set var="string1" value="This is first String."/>
<c:set var="string2" value="${fn:substringBefore(string1, 'first')}" />
<p>生成的子字符串 : ${string2}</p>
  • 包含 ‘is’字符串

fn:toLowerCase()

fn:toLowerCase()将字符串中的字符转为小写

<c:set var="string1" value="I am from RUNOOB"/>
<c:set var="string2" value="${fn:toLowerCase(string1)}" />
<p>字符串为 : ${string2}</p>

fn:toUpperCase()

fn:toUpperCase()将字符串中的字符转为大写

<c:set var="string1" value="I am from runoob"/>
<c:set var="string2" value="${fn:toUpperCase(string1)}" />
<p>字符串为 : ${string2}</p>

fn:trim()

fn:trim()移除首位的空白符

<c:set var="string1" value="I am from runoob         "/>
<c:set var="string2" value="${fn:trim(string1)}" />
<p>string2 长度 : ${fn:length(string2)}</p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mingo_敏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值