Jstl 1.2 的使用 JavaEE5使用
在MyEclipse 7.0 javeEE 5 以上等高级版本中,把.tld文件集成到了Java EE5 Libraries 我所用到的版本是Myeclipse 8.5 是sstl-1.2.jar
打开这个包可以看到里面的。META-INF当中有许多的 .tld ,程序员们可以打开这个里面的 .tld 如 <uri>http://java.sun.com/jsp/jstl/core</uri>
找到这句方便在页面 上调用。jstl 的好处是不像struts 标签依赖于struts 在不采用 struts 的项目中没办法使用。并且struts1.x 2.x 不太一样。
注:有一些标签不常用,这里不在多说如:<c:out/> <c:set /> 如果你想了解全部Jstl 请参阅 JSP2.0 技术手册
Jstl 例:
c 标签:
forEach:
<c:forEach items="${data}" var="dataarc" begin="5" end="8" step="2" varStatus="index">
items="${data}" // 要遍历的对象可以是 map list
var="dataarc" // 给元素起个名字,方便以下调用
begin="5" end="8" // 开始结束范围
step="2" // 这里我翻译为 间距 类似于 VB 的语法就是每隔几个调用一下
${index.index } // varStatus="index" 可以通过 ${index.index }来调用自动下标0~10等,也可以这么写${index.index+1 } 1~10
${dataarc.aid }|${dataarc.title }|${dataarc.price }|
<hr>
</c:forEach>
这 里使用起来比 struts 方便的多,更加灵活调用。
C 标签库结合运用:
例1:
<c:forEach items="${data}" var="arc" varStatus="index">
<c:if test="${index.first}"> 首个</c:if>
<c:if test="${index.last}"> 末个</c:if>
<c:if test="${!index.first&&!index.last}" >${index.count }</c:if> ${arc.title }<br>
</c:forEach>
<c:forEach items="${data}" var="arc" varStatus="index" >
<c:choose>
<c:when test="${index.first}">第一个</c:when>
<c:when test="${index.last}">末个</c:when>
<c:otherwise>其 它</c:otherwise>
</c:choose>
</c:forEach>
效果:
首 个 title0
2 title1
3 title2
4 title3
5 title4
6 title5
7 title6
8 title7
9 title8
末个 title9
第一个 其它 其它 其它 其它 其它 其它 其它 其它 末个
例2:
<c:set var="str" scope="request" value="12,23,34,45,56,"></c:set>
<c:forTokens items="${str}" delims="," var="string" varStatus="index">
${string}|<c:if test="${index.last }">
Ok</c:if>
</c:forTokens>
显 示效果:
12| 23| 34| 45| 56| Ok
这里是为了测试它是否够智能,但发现还是一般般了和PHP 中的 explode 差不多。
典型应用:
<c:out value="" escapeXml="true"></c:out> 可以实现转义HTML 中的标记
- 上一篇:Jsf 快速上手案例
- 下一篇:ecshop二次开发需要注意PHP值类型转换