`
习惯在马桶上思考
  • 浏览: 112970 次
  • 性别: Icon_minigender_2
  • 来自: 杭州
社区版块
存档分类
最新评论

(转)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.JSTL Core标签库 
多用途核心标签:<c:out><c:set><c:remove><c:catch> 
条件控制标签<c:if><c:choose><c:when><c:otherwise> 
循环控制标签<c:forEach><c:forToken> 
URL相关标签<c:import><c:url><c:redirect><c:param> 

<c:out>: 
<c:out value="{$sessonScope.anyValue}" default="no value" excapeXml="false" /> 
从Session查找名为anyValue的参数,并显示在页面,若咩有找到则显示no value 
xcapeXml当设置为true时会主动更换特殊字符,比如“&lt;&gt;&amp;”,默认为true 

<c:set>: 
<c:set value="this is andy" var="oneString" /> 
${oneString} 
将名为oneString得变量赋值为this is andy,其作用范围时page 
target:被赋值的javaBean实例名 
property,javaBean属性名 
scope:作用范围,默认为page 

<c:remove>: 
<c:remove var="sampleValue" scope="session" /> 

<c:catch>: 
<c:catch var="err"> 
${param.smpleSingleValue[9] == 3} 
</c:catch> 
${err} 
从变量var的err中得到异常内容 

<c:if> 
<c:if test="{paramValue.sampleValue[2] == 12}" var"visits" /> 
It is 12 
</c:if> 
${visits} 
将判断得结果保存在visits中 

<c:choose><c:when><c:otherwise>: 
<c:choose> 
<c:when test="$paramvalues.sampleValue[2] == 11" /> 
not 12 not 13,it is 11 
</c:when> 
<c:when test="$paramvalues.sampleValue[2] == 12" /> 
not 11 not 13,it is 12 
</c:when> 
<c:when test="$paramvalues.sampleValue[2] == 13" /> 
not 11 not 12,it is 13 
</c:when> 
<c:otherwise> 
not 11 12 13 
</c:otherwise> 
</c:choose> 

<c:forEach>: 
<c:forEach items="${seesionScope.arrayList}" var"arrayListI" /> 
${arrayListI} 
</c:forEach> 
var用来接收集合的对象,该循环的变量名 

<c:forTokens>: 
<c:forTokens items="aa,bb,cc,dd" begin="0" end="2" step"2" delims="," var="aValue" /> 
${aValue} 
</c:forTokens> 
结果时"aa cc",跳两格,截取逗号前面的字符 
delims分隔符, 
varStatus显示循环状态得变量 

<c:import> 
<c:import url="/MyHtml.html" var="thisPage" />在同一个Context下导入 
<c:import url="/MyHtml.html" context="/sample2" var="thisPage" />在不同Context下导入 
<c:import url="www.sample.com/MyHtml.html" var="thisPage" />导入任意一个URL 
charEncoding导入页面的字符集 

<c:url> 
<c:import url="/MyHtml.html" var="urlPage" /> 
<a href="${urlPage}">Link</a> 

<c:redirect>: 
<c:redirect url="/MyHtml.html" /> 
一般和<c:if>等标签一起使用 

<c:param>: 
<c:redirect url="/MyHtml.html" /> 
<c:param name="userName" value="RW"> 
</c:redirect> 
传递参数 


4.JSTL XML processing标签库 
XML核心标签库:<x:parse><x:out><x:set> 
XML流控制标签:<x:if><c:choose><c:when><c:otherwise> 
XML转换标签<x:transform><x:param> 

解析XML文件的<x:parse> 
<c:import var="xmlFile" url="http://localhost:8080/booksamplejstl/SampleXml.xml"> 
<x:parse var="xmlFileValue" doc="${xmlFile}"> 
varDom:制定保存得变量为org.w3c.dom.Document接口类型 
scopeDom:org.w3c.dom.Document接口类型的作用范围 
filter:该属性必须为org.xml.sax.XMLFilter类的一个实例,可以使用EL表达式传入,将对XML文件做过滤得到自身需要的部分 

<x:out> 
<x:parse var="xmlFileValue" doc="${xmlFile}" /> 
name:<x:out select="$xmlFileValue/xml-body/name" /><br> 
password:<x:out select="$xmlFileValue/xml-body/password" /><br> 
age:<x:out select="$xmlFileValue/xml-body/age" /><br> 
加上$作为XPath表达式得开头,select中的表达式将从xml-body根元素下得各个子元素中取得尸体内容 

<x:set>: 
<c:set value="this is andy" var="oneString" /> 

<x:if> 
<x:if test="{paramValue.sampleValue[2] == 12}" var"visits" /> 
It is 12 
</x:if> 
${visits} 
将判断得结果保存在visits中 

<x:choose><c:when><c:otherwise>: 
<x:choose> 
<x:when test="$paramvalues.sampleValue[2] == 11" /> 
not 12 not 13,it is 11 
</x:when> 
<x:when test="$paramvalues.sampleValue[2] == 12" /> 
not 11 not 13,it is 12 
</x:when> 
<x:when test="$paramvalues.sampleValue[2] == 13" /> 
not 11 not 12,it is 13 
</x:when> 
<x:otherwise> 
not 11 12 13 
</x:otherwise> 
</x:choose> 

<x:forEach>: 
<x:forEach items="${seesionScope.arrayList}" var"arrayListI" /> 
${arrayListI} 
</x:forEach> 
var用来接收集合的对象,该循环的变量名 

格式化XML显示数据得<x:transform>标签 
<x:transform>标签允许使用XSLT(转换XML格式的语言)为页面得显示数据做格式化的处理 
<c:import var="xmlFile" url="http://localhost:8080/booksamplejstl/SampleXml.xml"> 
<c:set var="xsltdoc"> 
<?xml version="1.0" ?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
<xsl:template match="/"> 
<xsl:apply-templates /> 
</xsl:template> 
<xsl:template match="xml-body"> 
<html> 
<head></head> 
<body> 
UserName: 
<xsl:value-of select="name" /> 
PassWord: 
<xsl:value-of select="password" /> 
<xsl:value-of select="age" /> 
</body> 
</html> 
</xsl:template> 
</xsl:stylesheet> 
</c:set> 

<x:transform xslt="${xsltdoc}" doc="${xmlFile}"> 
将XML文件保存的数据信息利用<c:import>保存在"xmlFile"中,然后使用<c:set>标签保存在一个XSLT得源到"xsltdoc",最后通过<x:tranform>格式化XML文件得数据显示在页面上 


<x:param>: 
<x:redirect url="/MyHtml.html" /> 
<x:param name="userName" value="RW"> 
</x:redirect> 
传递参数 


5.I18N fromattion标签库 

国际化核心标签:<fmt:setLocale><fmt:bundle><fmt:setBundle><fmt:message><fmt:param><fmt:requestEncoding> 

格式化标签:<fmt:timeZone><fmt:setTimeZone><fmt:formatNumber><fmt:parseNumber><fmt:formatDate> 
<fmt:parseDate> 

用于设置本地化环境的<fmt:setLocale> 
<fmt:setLocale value="zh_TW"> 


用于资源文件绑定的<fmt:bundle><fmt:setBundle>标签 
<fmt:bundle>标签将资源配置文件绑定于标签体中得显示 
<fmt:setBundle>标签允许将资源配置文件保存为一个变量,之后的工作可以根据该变量来进行 

<fmt:setBundle basename="applicationMessage" var="applicationBundle" /> 
该实例将会查找一个名为applicationMessage_zh_CN.properties的资源配置文件,来作为显示Resource标签 
basename:指定资源配置文件,只需要指定文件名而无须扩展名,是两组标签的共有属性 


用于显示资源配置文件信息的<fmt:message> 
<fmt:setBundle basename="applicationMessage" var="applicationBundle" /> 
<fmt:bundle baseanme="applicationAllMessage"> 
<fmt:message key="userName" /> 
<p> 
<fmt:message key="password" bundle="${applicationBundle}" /> 
</fmt:bundle> 
第一个<fmt:message>标签将使用"applicationAllMessage"资源配置文件中”键“为"userName"的信息显示 
第二个<fmt:message>标签虽然被定义在<fmt:bundle>标签体内,但是它使用了bundle属性,因此将指定之前由<fmt:setBundle>标签保存的"applicationMessage"资源配置文件,该”键“为"passWord"的信息显示 

用于参数传递得<fmt:param>,只有value属性 

用于为请求设置字符编码得<fmt:requestEncoding>标签,只有value属性 

用于设定失去得<fmt:timeZone><fmt:setTimeZone> 
<fmt:timeZone>使得在其标签体内得工作可以用该失去设置 
<fmt:setTimeZone>将失去设置保存为一个变量 

用于格式化数字得<fmt:formatNumber> 
<fmt:formatNumber value="1000.888" type="currency" var="money"> 
type:格式化类型,由currency(货币),number(数字),percent(百分比) 
pattern:格式化模式 
var:结果保存变量 

用于解析数字得<fmt:parseNumber> 
<fmt:parseNumber value="15%" type="precent" var="num"> 
结果是0.15 
type:解析格式化的类型 

用于格式化日期的<fmt:formatDate> 
timeZone:指定格式化日期的时区 
和<fmt:timeZone><fmt:setTimeZone>一起用 

用于解析日期的<fmt:parseDate> 
<fmt:parseDate><fmt:parseNumber>尽量少用,替代工作得地方应该在服务器端表示层的后段,比如在servlet 

6.Database access标签库 
用于设置数据源得<sql: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("AB C ")}D 
转换结果为"AB CD",只删除词尾得空格而不是全部, 


8.<bean:define>,<bean:write>被EL表达式替换 
<bean:cookie><bean:header><bean:parameter><bean:write>被EL表达式替换 
<bean:include>被<c:import>替换 
<bean:message>被<fmt:bundle><fmt:setBundle>,<fmt:message>合作替换 

所有判断标签(logic,bean)被EL表达式和<c:if>替换 
<logic:iterate>被<c:forEach>和EL表达替换 
<logic:redirect>被<c:redirect>和<c:param>替换 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics