案例目的
在jsp页面中使用核心标签库和EL表达式解析Result结果集中的内容
(下列使用代码为一个简单的购物车展示页面)
实现过程
下文中userCart即为一个Result结果集
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>购物车</title>
<style type="text/css">
th,td{
line-height:40px;
text-align:center;
}
</style>
</head>
<body>
购物车<br/>
<table border="1px " cellpadding="0" cellspacing="0" width="100%">
<tr>
<th>序号</th>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品类型</th>
<th>商品个数</th>
</tr>
<c:if test="${userCart.getRowCount()>0 }" var="flag">
<c:set var="index" value="0"/>
<c:set var="totalPrice" value="0"/>
<c:forEach var="map" items="${userCart.getRows()}">
<c:set var="index" value="${index+1 }"/>
<c:set var="totalPrice" value="${map.price*map.number+totalPrice }"/>
<tr>
<td>${index }</td>
<td>${map.id}</td>
<td>${map.name }</td>
<td>${map.price }</td>
<td>${map.type }</td>
<td>${map.number}</td>
</tr>
</c:forEach>
</c:if>
</table>
<span >总价为:${totalPrice }</span>
</body>
</html>
过程提要
1.导入jstl.jar包和standard.jar包
2.通过taglib指令,声明该页面中使用的标签库,同时引入标签库
3.Result集合中存储的每一条记录都是用key-value方式存储