从书上摘下来的片段,以备忘.
以下属性基于JSTL1.1
一般用途的标签:<c:out>, <c:set>, <c:remove>, <c:catch>
1.<c:out>
把计算的结果输出到JspWriter对象
语法1:没有Body的情况
<c:out value="value" [escapeXml="{true|false}"] [default="defaultValue"]/>
语法2:有Body的情况
<c:out value="value" [excapeXml="{true|false}"]
default value
</c:out>
<c:out>的Body可以是JSP代码,
属性说明:
Name | Type | Description |
value | Object | 将要计算的表达式 |
escapeXml | Boolean | 确定以下字符:<,>,&,',"在结果字符串中是否被转换成字符实例代码,默认为true |
default | Object | 如果value是null,那么输出这个default值 |
2.<c:set>
用于在某个范围(Request, Session, Application等)中设置某个值,或者设置某个对象的属性.
语法1.使用value属性设置一个特定范围中的属性.
<c:set value="value" var="varName" [scope="{page|request|session|application}"]/>
语法2.使用value属性设置一个特定范围中的属性,并且带有一个Body.
<c:set var="varName" [scope="{page|request|session|application}"]>
body content
</c:set>
语法3.设置某个特定对象的一个属性.
<c:set value="value" target="target" property="propertyName"/>
语法4.设置某个特定对象的一个属性,并且带有一个Body
<c:set target="target" property="propertyName"/>
body content
</c:set>
属性说明:
Name | Type | Description |
value | Object | 将要计算的表达式 |
var | String | 用于表示Value值的属性 |
scope | String | var的有效范围 |
target | String | 将要设置属性的对象,它必须是JavaBeans或者java.util.Map对象 |
property | Object | 待设置的Target对象中的属性名字 |
3.<c:remove>
用于删除某个变量或者属性
语法:
<c:remove var="varname" [scope="{page|request|session|application}"]/>
属性说明:
Name | Type | Description |
scope | String | 参数的范围 |
var | String | 待删除的参数变量名 |
4.<c:catch>
捕获嵌套在它里面的标签抛出的异常.
语法:
<c:catch [var="varName"]>
nested actions
</c:catch>
属性说明:
Name | Type | Description |
var | String | 用于标识这个异常的名字 |
条件标签:<c:if>, <c:choose>, <c:when>, <c:otherwise>
1.<c:if>
用于进行条件判断,如果它的Test属性为true,那么就计算它的body.
语法1.
<c:if test="testCondition" var="varName" [scope="{page|request|session|application}"]/>
语法2.
<c:if test="testCondition" [value="varName"] [scope="{page|request|session|application}"]>
body content
</c:if>
属性说明:
Name | Type | Description |
test | Boolean | 表达式的条件 |
var | String | test条件表达式计算的变量值,它的类型为Boolean |
scope | String | var的范围 |
2.<c:choose>
用于条件选择,它和<c:when>以及<c:otherwise>一起使用
语法
<c:choose>
body content(<when> and <otherwise> subtags)
</c:choose>
注意,它的body content只能由以下的元素构成:
(1) 空格
(2) 0个或多个<when>,<when>必须在,<otherwise>之前出现
(3) 0个或一个<otherise>
3.<c:when>
代表<c:choose>的一个分支
语法:
<c:when test="testCondition">
body content
</c:when>
属性说明:
Name | Type | Description |
test | Boolean | 表达式的条件 |
有两个限制:
(1)必须以<c:choose>作为它的父标签
(2)必须在<c:otherwise>之前出现
4.<c:otherwise>
代表<c:choose>的最后选择
语法:
<c:otherwise>
conditional block
</c:otherwise>
它的使用有两个限制
(1)必须以<c:choose>作为它的父标签
(2)必须是<c:choose>的最后分支
迭代标签:<c:forEach>,<c:forTokens>
1.<c:forEach>
在一个包含一等系列对象的Collection中迭代计算它的bodyContent,或者重复迭代固定的次数
语法1:在Collection中迭代
<c:forEach [var="varName"] items="collection"
[varStatus="varStatusName"] [begin="begin"][end="end"][step="step"]>
body content
</c:forEach>
语法2:迭代固定的次数
<c:forEach [var="varName"] [varStatus="varStausName"]
begin="begin" end="end" [step="step"]>
body content
</c:forEach>
属性说明:
Name | Type | Description |
var | String | 迭代参数的变量名 |
items | 支持的类型 | 将要迭代的集合 |
varStatus | String | 表示迭代的状态,可以访问迭代自身的信息 |
begin | int | 开始索引 |
end | int | 结束索引 |
step | int | 迭代的步长 |
varStaus使用举例:
<c:forEach var="user" items="${users}" varStatus="status">
<c:out valur="${user.userName}"/><br/>
<c:out value="${status.index}"/><br/>
<c:out value="${status.count}"/><br/>
<c:out value="${sataus.first}"/><br/>
<c:out value="${status.last}"/><br/>
</c:forEach>
<c:forEach>可以迭代java.util.Collection, Array, Map, Tokened, String等.
2.<c:forTokens>
专门用于处理TokeString的迭代,可以指定一个或者多个分隔符(delimiters)
语法:
<c:forTokens items="stringOfTokens" delims="delimiters"
[var="varName"] [varStatus="varStatusName"]
[begin="begin"] [end="end"] [step="step"]>
body content
</c:forTokens>
属性说明:
Name | Type | Description |
var | String | 迭代的参数名 |
items | String | 将要迭代的String |
varStatus | String | 表示迭代的状态,可以访问迭代自身的信息 |
delims | String | 分隔符 |
begin | int | 开始索引 |
end | int | 结束索引 |
step | int | 迭代的步长 |
<h4>使用"|"和","作为分割符</h4>
<c:forTokens var="token" items="blue,red,green|yellow|pink|,black|white"
delims="|,">
<c:out value="${token}"/>©
</c:forTokens>
URL相关的标签<c:import>,<c:redirect>,<c:url>,<c:param>
1.<c:import>
导入一个基于URL的资源
语法1:
<c:import url="url" [context="context"] [var="varName"]
[scope="{page|request|session|application}"]
[charEncoding="charEncoding"]>
optional body content for <c:param> subtags
</c:import>
语法2:
<c:import="url" [context="context"] varReader="varReaderName"
[charEncoding="charEncoding"]>
body content where varReader is consumed by another action
</c:import>
属性说明:
Name | Type | Description |
url | String | 待导入资源的URL |
context | String | 当使用相对路径访问外部context资源时,context指定了这个资源的名字 |
var | String | 参数的名称 |
scope | String | var参数的作用范围 |
charEncoding | String | 输入资源的字符编码 |
varReader | String | 这个参数的类型是Reader,用于读取资源 |
2.<c:redirect>
把客户的请求重定向到另一个资源
语法1.
<c:redirect url="value" [context="context"]/>
语法2.
<c:redirect url="value" [context="context"]>
<c:param>subtags
</c:redirect>
属性说明:
Name | Type | Description |
url | String | 待定向资源的URL |
context | String | 当使用相对路径访问外部context资源时,context指定的这个资源的名字. |
3.<c:url>
用于构造URL,主要的用途是URL重写
语法1.
<c:url value="value" [context="context"]
[var="varName"] [scope="{page|request|session|application}"]/>
语法2.
<c:url value="value" [context="context"]
[var="varName"] [scope="{page|request|session|application}"]>
<c:param>subtags
</c:url>
4.<c:param>
在<c:import>, <c:url>,<c:redirect>中添加请求的参数
语法1.
<c:param name="name" value="value"/>
语法2.
<c:param name="name">
paramter value
</c:param>
后记:还有XML标签(<x:parse>, <x:out>, <x:set>),SQL标签,和国际化输出标签.XML标签和SQL标签比较不常用,所以不准备整理了,国际化输出标签稍后会根据SUN上面的资料整理一些备参考.