jstl标签库手册.docx
《jstl标签库手册.docx》由会员分享,可在线阅读,更多相关《jstl标签库手册.docx(11页珍藏版)》请在冰点文库上搜索。
jstl标签库手册
标签库手册
1.JSTL1.1标签库
核心标签库,国际化标签库,数据库标签库,XML标签库,函数标签库
2.EL表达式的默认变量
(1)pageScope,requestScope,sessionScope,applicationScope,
这个4个变量包含Scope作用范围得参数集合,相当于保存在java.util.Map种得某个参数
(2)默认变量param和paramValues
param表明请求包含的参数为单一控件,paramValues表明请求包含的参数为控件数组
(3)默认变量header和headerValues
包含请求参数头部信息得集合,header变量表示单一头部信息,headerValue则表示数组型得头部信息
(4)默认变量cookie
cookie集合,集合中得每个对象对应javax.servlet.http.Cookie
(5)默认变量initParam包含所有应用程序初始化参数得集合
(6)默认变量pageContext
用来提供访问不同得请求参数
EL表达式操作符
empty:
用来对一个空变量进行判断:
null,一个空String,空数组,空map,没有条目得Collection集合
func:
调用方法,func是方法名,args是参数,可以没有,或者有一个,多个参数,参数间用都好隔开
3.JSTLCore标签库
多用途核心标签:
out>set>remove>catch>
条件控制标签if>choose>when>otherwise>
循环控制标签forEach>forToken>
URL相关标签import>url>redirect>param>
out>:
outvalue="{$sessonScope.anyValue}"default="novalue"excapeXml="false"/>
从Session查找名为anyValue的参数,并显示在页面,若咩有找到则显示novalue
xcapeXml当设置为true时会主动更换特殊字符,比如“<>&”,默认为true
set>:
setvalue="thisisandy"var="oneString"/>
${oneString}
将名为oneString得变量赋值为thisisandy,其作用范围时page
target:
被赋值的javaBean实例名
property,javaBean属性名
scope:
作用范围,默认为page
remove>:
removevar="sampleValue"scope="session"/>
catch>:
catchvar="err">
${param.smpleSingleValue[9]==3}
catch>
${err}
从变量var的err中得到异常内容
if>
iftest="{paramValue.sampleValue[2]==12}"var"visits"/>
Itis12
if>
${visits}
将判断得结果保存在visits中
choose>when>otherwise>:
choose>
whentest="$paramvalues.sampleValue[2]==11"/>
not12not13,itis11
when>
whentest="$paramvalues.sampleValue[2]==12"/>
not11not13,itis12
when>
whentest="$paramvalues.sampleValue[2]==13"/>
not11not12,itis13
when>
otherwise>
not111213
otherwise>
choose>
forEach>:
forEachitems="${seesionScope.arrayList}"var"arrayListI"/>
${arrayListI}
forEach>
var用来接收集合的对象,该循环的变量名
forTokens>:
forTokensitems="aa,bb,cc,dd"begin="0"end="2"step"2"delims=","var="aValue"/>
${aValue}
forTokens>
结果时"aacc",跳两格,截取逗号前面的字符
delims分隔符,
varStatus显示循环状态得变量
import>
importurl="/MyHtml.html"var="thisPage"/>在同一个Context下导入
importurl="/MyHtml.html"context="/sample2"var="thisPage"/>在不同Context下导入
importurl="var="thisPage"/>导入任意一个URL
charEncoding导入页面的字符集
url>
importurl="/MyHtml.html"var="urlPage"/>
Link
redirect>:
redirecturl="/MyHtml.html"/>
一般和if>等标签一起使用
param>:
redirecturl="/MyHtml.html"/>
paramname="userName"value="RW">
redirect>
传递参数
4.JSTLXMLprocessing标签库
XML核心标签库:
parse>out>set>
XML流控制标签:
if>choose>when>otherwise>
XML转换标签transform>param>
解析XML文件的parse>
importvar="xmlFile"url="http:
//localhost:
8080/booksamplejstl/SampleXml.xml">
parsevar="xmlFileValue"doc="${xmlFile}">
varDom:
制定保存得变量为org.w3c.dom.Document接口类型
scopeDom:
org.w3c.dom.Document接口类型的作用范围
filter:
该属性必须为org.xml.sax.XMLFilter类的一个实例,可以使用EL表达式传入,将对XML文件做过滤得到自身需要的部分
out>
parsevar="xmlFileValue"doc="${xmlFile}"/>
name:
outselect="$xmlFileValue/xml-body/name"/>
password:
outselect="$xmlFileValue/xml-body/password"/>
age:
outselect="$xmlFileValue/xml-body/age"/>
加上$作为XPath表达式得开头,select中的表达式将从xml-body根元素下得各个子元素中取得尸体内容
set>:
setvalue="thisisandy"var="oneString"/>
if>
iftest="{paramValue.sampleValue[2]==12}"var"visits"/>
Itis12
if>
${visits}
将判断得结果保存在visits中
choose>when>otherwise>:
choose>
whentest="$paramvalues.sampleValue[2]==11"/>
not12not13,itis11
when>
whentest="$paramvalues.sampleValue[2]==12"/>
not11not13,itis12
when>
whentest="$paramvalues.sampleValue[2]==13"/>
not11not12,itis13
when>
otherwise>
not111213
otherwise>
choose>
forEach>:
forEachitems="${seesionScope.arrayList}"var"arrayListI"/>
${arrayListI}
forEach>
var用来接收集合的对象,该循环的变量名
格式化XML显示数据得transform>标签
transform>标签允许使用XSLT(转换XML格式的语言)为页面得显示数据做格式化的处理
importvar="xmlFile"url="http:
//localhost:
8080/booksamplejstl/SampleXml.xml">
setvar="xsltdoc">
xmlversion="1.0"?
>
stylesheetxmlns:
xsl="http:
//www.w3.org/1999/XSL/Transform"version="1.0">
templatematch="/">
apply-templates/>
template>
templatematch="xml-body">
UserName:
value-ofselect="name"/>
PassWord:
value-ofselect="password"/>
value-ofselect="age"/>
template>
stylesheet>
set>
transformxslt="${xsltdoc}"doc="${xmlFile}">
将XML文件保存的数据信息利用import>保存在"xmlFile"中,然后使用set>标签保存在一个XSLT得源到"xsltdoc",最后通过tranform>格式化XML文件得数据显示在页面上
param>:
redirecturl="/MyHtml.html"/>
paramname="userName"value="RW">
redirect>
传递参数
5.I18Nfromattion标签库
国际化核心标签:
setLocale>bundle>setBundle>message>param>requestEncoding>
格式化标签:
timeZone>setTimeZone>formatNumber>parseNumber>formatDate>
parseDate>
用于设置本地化环境的setLocale>
setLocalevalue="zh_TW">
用于资源文件绑定的bundle>setBundle>标签
bundle>标签将资源配置文件绑定于标签体中得显示
setBundle>标签允许将资源配置文件保存为一个变量,之后的工作可以根据该变量来进行
setBundlebasename="applicationMessage"var="applicationBundle"/>
该实例将会查找一个名为applicationMessage_zh_CN.properties的资源配置文件,来作为显示Resource标签
basename:
指定资源配置文件,只需要指定文件名而无须扩展名,是两组标签的共有属性
用于显示资源配置文件信息的message>
setBundlebasename="applicationMessage"var="applicationBundle"/>
bundlebaseanme="applicationAllMessage">
messagekey="userName"/>
messagekey="password"bundle="${applicationBundle}"/>
bundle>
第一个message>标签将使用"applicationAllMessage"资源配置文件中”键“为"userName"的信息显示
第二个message>标签虽然被定义在bundle>标签体内,但是它使用了bundle属性,因此将指定之前由setBundle>标签保存的"applicationMessage"资源配置文件,该”键“为"passWord"的信息显示
用于参数传递得param>,只有value属性
用于为请求设置字符编码得requestEncoding>标签,只有value属性
用于设定失去得timeZone>setTimeZone>
timeZone>使得在其标签体内得工作可以用该失去设置
setTimeZone>将失去设置保存为一个变量
用于格式化数字得formatNumber>
formatNumbervalue="1000.888"type="currency"var="money">
type:
格式化类型,由currency(货币),number(数字),percent(百分比)
pattern:
格式化模式
var:
结果保存变量
用于解析数字得parseNumber>
parseNumbervalue="15%"type="precent"var="num">
结果是0.15
type:
解析格式化的类型
用于格式化日期的formatDate>
timeZone:
指定格式化日期的时区
和timeZone>setTimeZone>一起用
用于解析日期的parseDate>
parseDate>parseNumber>尽量少用,替代工作得地方应该在服务器端表示层的后段,比如在servlet
6.Databaseaccess标签库
用于设置数据源得setDataSource>
现在这个标签已不用,因为违反了MVC得规范
7.functions标签
为EL表达式语句提供了许多更有用得功能
长度函数:
fn:
length
字符串处理函数:
fn:
contains,fn:
containsIgnoreCase,fn:
endsWith,fn:
escapeXml,fn:
indexOf,fn:
join,
fn:
replace,fn:
split,fn:
startsWith,fn:
substring,fn:
substringAfter,fn:
substringBefore,fn:
toLowerCase,fn:
toUpperCase,fn:
trim
fn:
length
${fn:
length(sessionScope.arrayList1)}
参数为input,将计算通过该属性传入的对象长度,该对象应该为集合类型或String类型,其返回结果是一个int类型的值,
判断函数fn:
contains
判断源字符串是否含有子字符串,返回结果是boolean类型的值
contains("string","substring"),两个都是stirng类型
${fn:
contains("ABC","a")}返回false
${fn:
contains("ABC","A")}返回ture
fn:
containslgnoreCase函数
和fn:
contains区别是fn:
containslgnoreCase函数的子字符串忽略大小写
${fn:
ontainslgnoreCase("ABC","a")}返回ture
${fn:
ontainslgnoreCase("ABC","A")}返回ture
词头判断fn:
startsWith
${fn:
startsWith("ABC","ab")}返回false
${fn:
startsWith("ABC","AB")}返回ture
词尾判断fn:
endsWith
字符实体转换函数fn:
escapeXml
用于将所有特殊字符转化为字符实体码,返回一个string类型
字符匹配函数fn:
indexOf
${fn:
indexOf("ABC","aBC")}返回-1
${fn:
indexOf("ABC","BC")}返回1
返回-1或1
分隔符fn:
join
<%String[]stringArray=("a","b","c");%>
<%request.getSession().setAttribute("stringArray",stringArray);%>
${fn:
join(sessionScope.stringArray,",")}
定义数组并防止到Session中,然后通过Session得到该字符串数组,使用fn:
join函数并传入分隔符";",得到的结果为"a;b;c"
替换函数fn:
replace
${fn:
replace("ABC","A","B")}
"ABC"是原字符串,"A"是被替换的字符,"B"替换后的字符
分隔符转换数组函数fn:
split
${fn:
split("A,B,C",",")}
将"A,B,C"字符串转换为数组{A,B,C}
字符串截取函数fn:
substring函数
${fn:
substring("ABC","1","2")}
从0开始
截取结果为B
定位到结束截取字符串函数fn:
substringAfter函数
${fn:
substringAfter("ABC","BC")},结果为D(不包括BC)
允许截取源字符串中某个字符串开始到结束的所有字符
起始到定位截取字符串函数fn:
substringBefore
${fn:
substringAfter("ABC","BC")}结果为A(不包括BC,BC前的数)
小写转换函数fn:
toLowerCase
${fn:
toLowerCase("ABC")}
大写转换函数fn:
toUpperCase
${fn:
toLowerCase("abc")}
空格删除函数fn:
trim函数
${fn:
trim("ABC")}D
转换结果为"ABCD",只删除词尾得空格而不是全部,
8.define>,write>被EL表达式替换
cookie>header>parameter>write>被EL表达式替换
include>被import>替换
message>被bundle>