SpringMVC学习指南【笔记6】JSTL标签、函数

JSTL可以完成一般的任务(如遍历、集合和条件)、处理XML文档、格式化文本、访问数据库、操作数据等等。

下面内容介绍了,操作有界对象的标签(out、set、remove),执行条件测试的标签(if、choose、when、otherwise),遍历集合或token的标签(forEach、forTokens),解析和格式化日期与数字的标签(parseNumber、formatNumber、parseDate、formatDate),以及可以在EL表达式中使用JSTL1.2函数。

引用JSTL需要使用taglib指令

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

out标签

ouy标签在运算表达式时,是将结果输出带当前的JspWriter。out的语法有两种,一种是没有body content的,一种是有body content的。

第一种:

<c:out value="value" [escapeXml="true|false"] [default="default Value"] />

第二种:

<c:out value="value" [escapeXml="true|false"] >
    default Value
</c:out>

注意:在标签的语法中,[]表示可选的属性。

out中的default属性可以赋一个默认值,当赋予其value属性的EL表达式返回null时,就会显示默认值。default属性可以赋动态值,如果这个动态值返回null,out就会显示一个空的字符串。

例如,在下面的out标签中,如果在HttpSession中没有找到myVar变量,就会显示应用程序范围的变量myVar值,如果没找到,就输出一个空的字符串。

<c:out value="${sessionScope.myVar}" default="${applicationScope.myVar}" />

set标签

作用:1、创建一个字符串和一个引用该字符串的有界变量;

   2、创建一个引用现存有界对象的有界变量;

   3、设置有界对象的属性。

如果用set创建有界变量,那么在该标签出现后的整个JSP页面中都可以使用该变量。

set标签的4种形式

第一种:<c:set value="value" var="varName" [scope="{page|request|session|application}"] />这里的scope属性指定了有界变量的范围。

第二种:<c:set value="value" var="varName" [scope="{page|request|session|application}"] >body content</c:set>允许在body content中有JSP代码。

第三种:<c:set target="target" property="propertyName" value="value" />

第四种:<c:set target="target" property="propertyName">body content</c:set>

例如,将字符串"ShenZhen"赋予有界对象address的city属性

<c:set target="${address}" property="city" value="ShenZhen" />

或者

<c:set target="${address}" property="city">ShenZhen</c:set>

remove标签

remove标签用于删除有界变量

<c:remove var="varName" [scope="{page|request|session|application}"] />

注意:有界变量引用的对象不能删除。

例如,删除页面范围的变量job

<c:remove var="job" scope="page" />

条件行为

4个标签:if、choose、when、otherwise。

if标签

第一种:<c:if test="testCondition" var="varName" [scope="{page|request|session|application}"] />

第二种:<c:if test="testCondition" [var="varName"] [scope="{page|request|session|application}"]>body content</c:if>

choose、when、otherwise标签

<c:choose>
    <C:when test="${param.status=='full'}">
        you are a full member
    </c:when>
    <C:when test="${param.status=='student'}">
        you are a student member
    </c:when>
</c:choose>
<c:choose>
    <C:when test="${param.status=='full'}">
        you are a full member
    </c:when>
    <C:when test="${param.status=='student'}">
        you are a student member
    </c:when>
    <c:otherwise>
        please register
    </c:otherwise>
</c:choose>

遍历行为

forEach标签

 第一种:固定次数地重复body content

<c:forEach [var="varName"] begin="begin" end="end" step="step">
    body content
</c:forEach>

第二种:遍历对象集合

<c:forEach items="collection" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"]>
    body content
</c:forEach>

forTokens标签

用于遍历以特定分隔符隔开的令牌

<c:forTokens items="stringOfTokens" delims="delimiters" [var="varName"] [varStatus="varStatusName"] [begin="begin"] [end="end"] [step="step"]>
    body content
<c:forTokens>

例如

<c:forTokens var="item" items="Argentina,Brazil,Chile" delims=",">
    <c:out value="${item}" /><br/>
<c:forTokens>

产生的结果是:

Argentina
Brazil
Chile

与URL相关的行为

url标签、redirect标签

格式化行为

JSTL提供了格式化和解析数字和日期的标签,formatNumber、formatDate、timeZone、setTimeZone、parseNumber、parseDate

formatNumber的两种格式

第一种:

<fmt:formatNumber value="numericValue" [type="{number|currency|percent}"] pattern="customPattern" [currencyCode="currencyCode"] [currencySymbol="currencySymbol"]
 [groupingUsed="{true|false}"] [maxIntegerDigits="maxIntegerDigits"] [minIntegerDigits="maxIntegerDigits"] [maxFractionDigits="maxFractionDigits"] [minFractionDigits="minFractionDigits"]
 [var="varName"] [scope="{page|request|session|application}"] />

type默认是number,scope默认是page

第二种:

<fmt:formatNumber [type="{number|currency|percent}"] pattern="customPattern" [currencyCode="currencyCode"] [currencySymbol="currencySymbol"] [groupingUsed="{true|false}"]
 [maxIntegerDigits="maxIntegerDigits"] [minIntegerDigits="maxIntegerDigits"] [maxFractionDigits="maxFractionDigits"] [minFractionDigits="minFractionDigits"]
 [var="varName"] [scope="{page|request|session|application}"] >
    numeric value to be formatted
 </fmt:formatNumber>

type默认是number,groupingUsed默认是true,scope默认是page

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>

运行效果如下

数字格式化:
格式化数字 (1): ¥120,000.23
格式化数字 (2): 000.231
格式化数字 (3): 120,000.231
格式化数字 (4): 120000.231
格式化数字 (5): 023%
格式化数字 (6): 12,000,023.0900000000%
格式化数字 (7): 023%
格式化数字 (8): 120E3
美元 : $120,000.23

formatNumber标签的用途之一就是将数字格式化成货币,可以利用currencyCode属性来定义货币代码

如:加拿大元CAD,人民币CNY,欧元EUR,日元JPY,英镑GBP,美元USD

formatDate标签

<fmt:formatDate value="date" [type="{time|date|both}"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"]
 [pattern="customPattern"] [timeZone="timeZone"] [var="varName"] [scope="{page|request|session|application}"] />

timeZone标签

用于定义时区

<fmt:timeZone value="timeZone">
    body content
</fmt:timeZone>

如果value属性为null或者empty,则使用GMT时区。

例如:

<fmt:timeZone value="GMT+1:00">
    <fmt:formatDate value="${now}" type="{both}" dateStyle="full" timeStyle="full" />
</fmt:timeZone>
<fmt:timeZone value="HST">
    <fmt:formatDate value="${now}" type="{both}" dateStyle="full" timeStyle="full" />
</fmt:timeZone>
<fmt:timeZone value="CST">
    <fmt:formatDate value="${now}" type="{both}" dateStyle="full" timeStyle="full" />
</fmt:timeZone>

setTimeZone标签

用于指定时区保存在一个有界变量或者时间配置变量中。

<fmt:setTimeZone value="timeZone" [var="varName"] [scope="{page|request|session|application}"] />

parseNumber标签

用于将以字符串表示的数字、货币或者百分比解析成数字

两种语法形式

第一种

<fmt:parseNumber value="numericValue" [type="{number|currency|percent}"] [pattern="customPattern"] [parseLocale="parseLocale"]
 [integerOnly="{true|false}"] [var="varName"] [scope="{page|request|session|application}"] />

第二种

<fmt:parseNumber [type="{number|currency|percent}"] [pattern="customPattern"] [parseLocale="parseLocale"]
 [integerOnly="{true|false}"] [var="varName"] [scope="{page|request|session|application}"] >
    numeric value to be parsed
</fmt:parseNumber>

parseDate标签

区分地域的格式解析以字符串表示的日期和时间

第一种

<fmt:parseDate value="dateString" [type="{time|date|both}"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"]
 [pattern="customPattern"] [timeZone="timeZone"] [parseLocale="parseLocale"] [var="varName"] [scope="{page|request|session|application}"] />

第二种

<fmt:parseDate [type="{time|date|both}"] [dateStyle="{default|short|medium|long|full}"] [timeStyle="{default|short|medium|long|full}"]
 [pattern="customPattern"] [timeZone="timeZone"] [parseLocale="parseLocale"] [var="varName"] [scope="{page|request|session|application}"] />
 date value to be parsed
</fmt:parseDate>

引用EL表达式的标准函数需要使用taglib指令

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

调用函数时使用格式

${fn:functionName}这里functionName指的是函数名

contains函数
测试一个字符串中是否包含指定的子字符串contains(string,substring)
<C:set var="myString" value="hello world"/>
${fn:contains(myString,"hello")}返回true
${fn:contains("hello abby","ABBY")}返回false

containsIgnoreCase函数
测试一个字符串中是否包含指定的子字符串,但不区分大小写containsIgnoreCase(string,substring)
${fn:containsIgnoreCase("hello abby","ABBY")}返回true

endsWith函数
测试一个字符串是否以指定的后缀结尾endsWith(string,suffix)
${fn:endsWith("hello world","world")}

escapeXml函数
用于给String编码escapeXml(string),这种转换与out标签将其escapeXml属性设为true一样
${fn:escapeXml("use <br/> to change lines")}
将被渲染成:use &lt;br/&gt; to change lines

indexOf函数
返回指定子字符串在某个字符串中第一次出现时的索引,如果没有找到就返回-1。indexOf(string,substring)
${fn:indexOf("hello world","world")}将返回6

jojn函数
将一个String数组中的所有元素都合并成一个字符串,并用指定的分隔符分开join(array,separator)如果这个数组为null,就会返回一个空字符串
如果array是一个String数组,有两个元素"hello"和"world"
${fn:join(array,",")}
将返回hello,world

split函数
用于将一个字符串分离成一个子字符串数组
例如,下列代码是分子字符串"hello,world"并将结果保存在有界变量split中,随后利用forEach标签将split格式化成一个html表。
<c:set var="split" value='${fn:split("hello,world",",")}' />
<table>
<c:forEach var="substring" items="${split}">
<tr><td>${substring}</td></tr>
</c:forEach>
</table>
结果为:
<table>
<tr><td>hello</td></tr>
<tr><td>world</td></tr>
</table>

startsWith函数
用于测试一个字符串是否以指定的前缀开头startsWith(string,prefix)
${fn:startsWith("hello world","he")}返回true

substring函数
用于返回一个从指定基于0的起始索引(包含)到指定基于0的终止索引的子字符串substring(string,beginIndex,endIndex)
${fn:substring("hello world",0,4)}返回hell

substringAfter函数
用于返回指定子字符串第一次出现后的字符串部分substringAfter(string,substring)
${fn:substringAfter("hello world",e)}返回llo world

substringBefore函数
用于返回指定子字符串第一次出现前的字符串部分substringBefore(string,substring)
${fn:substringBefore("hello world",e)}返回h

toLowerCase函数
将一个字符串转换成它的小写版本toLowerCase(string)
${fn:toLowerCase("HELLO WOrld")}返回hello world

toUpperCase函数
将一个字符串转换成它的大写版本toUpperCase(string)
${fn:toUpperCase("hello WOrld")}返回HELLO WORLD

trim函数
用于删除一个字符串开头和结尾的空白trim(string)
${fn:trim(" hello world ")}返回hello world

原文地址:https://www.cnblogs.com/xsl1995/p/10171610.html