JSTL中有不少容易忽视的地方,学习小结之:
1 FOREACH学习:
<c:forEach items="${ personList }" var="person">
<tr>
<td>${ person.id }</td>
</tr>
........
</c:forEach>
隔行变色效果:
<c:forEach items="${ personList }" var="person" varStatus="varStatus">
<tr bgcolor="${ varStatus.index % 2 == 1 ? '#EFEFEF' : '#FFFFFF' }">
</c:forEach>
2 <c:import>
导入一个网页:
<c:import var="baidu" url="http://www.baidu.com/" charEncoding="gbk">
</c:import>
3 <c:url>
当客户端不支持COOKIE时,可以用这个
<c:url value="/images/bg.gif">
</c:url>
会自动带上jesessionid
4 <c:redirect>和,<c:param>
<c:redirect url="http://www.baidu.com/s">
<c:param name="wd" value="音乐" ></c:param>
<c:param name="cl" value="3"></c:param>
</c:redirect>
5
<fmt:parseDate var="date" value="2008-12-12 08:00:00" pattern="yyyy-MM-dd HH:mm:ss" parseLocale="ja" ></fmt:parseDate>
<fmt:formatDate value="${ date }" type="both" dateStyle="full" timeStyle="full" timeZone="America/Los_Angeles" />
<fmt:parseDate>会将字符串转换为日期格式输出,并可指定用什么locale去处理
6 JSTL中的SQL标签比较少用,新近小结下:
<sql:setDataSource driver="com.mysql.jdbc.Driver" user="root"
password="admin"
url="jdbc:mysql://localhost:3306/jstl?charachterEncoding=UTF-8"
var="dataSource" />
<sql:query var="rs" dataSource="${ dataSource }"
sql="select * from tb_corporation" >
</sql:query>
<c:forEach var="row" items="${ rs.rows }">
<tr>
<td>${ row['id'] }</td>
<td>${ row['name'] }</td>
<td>${ row['description'] }</td>
</tr>
</c:forEach>
8 JSTL的XML标签库;
<%@ taglib uri="http://java.sun.com/jsp/jstl/xml" prefix="x"%>
<c:import var="file" url="/sina.xml" charEncoding="UTF-8" />
<div class="source">
<c:out value="${ file }" />
</div>
<x:parse var="doc" doc="${ file }"></x:parse>
新浪 RSS <br/>
版本:<x:out select="$doc/rss/@version" /> <br/>
标题:<x:out select="$doc/rss/channel/title" /> <br/>
来源:<x:out select="$doc/rss/channel/generator" /> <br/>
版权:<x:out select="$doc/rss/channel/copyright" /> <br/>
出版时间:<x:out select="$doc/rss/channel/pubDate" /> <br/>
链接地址:<x:out select="$doc/rss/channel/link"/> <br/>
遍历解析之:
<c:import var="file" url="/sina.xml" charEncoding="UTF-8" />
<div class="source">
<c:out value="${ file }" />
</div>
<x:parse var="doc" doc="${ file }"></x:parse>
<x:forEach var="node" select="$doc/rss/channel/item" varStatus="status">
${ status.count }.
<a href="<x:out select="$node/link"/>" target="_blank"><x:out select="$node/title"/></a>
<x:out select="$node/pubDate"/> <br/>
<x:out select="$node/description"/> <br/><br/>
</x:forEach>