今天在做购物车的jsp页面时,遇到一个问题,在jsp页面中不能通过${shopCars.size}取列表长度,那么怎么判断Servlet转发过来的List的长度呢?
方法一:
在 body中加入个隐式标签就可以判断了
如下:
<input type="hidden" name="shopCars" value="${shopCars}" />
<c:if test="${shopCars.size()<1}">
<span style="color: #FF5500;font-size: 28px">亲,你的购物车还没有商品哦!</span>
</c:if>
jsp页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/ShopCar.css">
</head>
<body class="body">
<div class="box-big">
<div class="box-top">
<h3 style="display: inline">我的购物车</h3><a href="/Index" style="text-decoration: none;margin-left: 800px"> <img src="img/index.jpg" width="18" height="17"/>首页</a>
</div>
<div class="box">
<input type="hidden" name="shopCars" value="${shopCars}" />
<c:if test="${shopCars.size()<1}">
<span style="color: #FF5500;font-size: 28px">亲,你的购物车还没有商品哦!</span>
</c:if>
<c:if test="${shopCars.size()>0}">
<table width="1000px" cellpadding="8px">
<thead>
<tr>
<th><input type="checkbox" id="check-all">全选</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>合计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${shopCars}" var="s" begin="0">
<tr>
<td align="center"><input type="checkbox" align="center" name="a"/></td>
<td align="center"><img src="${s.getCimg()}" width="45px" height="45px"/>${s.getCname()}</td>
<td align="center"><span style="font-weight: bold">¥${s.getCprice()}</span><del style="font-size: 13px;color: #7d7d7d">¥${s.getColdprice()}</del></td>
<td align="center">${s.getCnum()}</td>
<td align="center"><span style="color: #FF5500">¥${s.getSumprice()}</span></td>
<td align="center"><a href="/Delcart?cid=${s.getCid()}" style="text-decoration: none">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</div>
<div>
<c:if test="${shopCars.size()>0}">
<input type="button" style="background-color: #FF5500;color: #ffffff;margin-left: 900px;width: 120px;height: 40px " value="结算"/>
</c:if></div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
$("#check-all").click(
function(){
if($(this).is(":checked")){
$("input[name='a']").prop("checked",true);
}
else{
$("input[name='a']").prop("checked",false);
}
}
)
</script>
</body>
</html>
方法二:
使用taglin指令导入Functions标签库:
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
然后使用 ${fn:length(shopCars)} 获取List长度
jsp页面代码如下:
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<html>
<head>
<title></title>
<link rel="stylesheet" href="css/ShopCar.css">
</head>
<body class="body">
<div class="box-big">
<div class="box-top">
<h3 style="display: inline">我的购物车</h3><a href="/Index" style="text-decoration: none;margin-left: 800px"> <img src="img/index.jpg" width="18" height="17"/>首页</a>
</div>
<div class="box">
<c:if test="${fn:length(shopCars)<1}">
<span style="color: #FF5500;font-size: 28px">亲,你的购物车还没有商品哦!</span>
</c:if>
<c:if test="${fn:length(shopCars)>=1}">
<table width="1000px" cellpadding="8px">
<thead>
<tr>
<th><input type="checkbox" id="check-all">全选</th>
<th>商品</th>
<th>单价</th>
<th>数量</th>
<th>合计</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${shopCars}" var="s" begin="0">
<tr>
<td align="center"><input type="checkbox" align="center" name="a"/></td>
<td align="center"><img src="${s.getCimg()}" width="45px" height="45px"/>${s.getCname()}</td>
<td align="center"><span style="font-weight: bold">¥${s.getCprice()}</span><del style="font-size: 13px;color: #7d7d7d">¥${s.getColdprice()}</del></td>
<td align="center">${s.getCnum()}</td>
<td align="center"><span style="color: #FF5500">¥${s.getSumprice()}</span></td>
<td align="center"><a href="/Delcart?cid=${s.getCid()}" style="text-decoration: none">删除</a></td>
</tr>
</c:forEach>
</tbody>
</table>
</c:if>
</div>
<div><input type="button" style="background-color: #FF5500;color: #ffffff;margin-left: 900px;width: 120px;height: 40px " value="结算"/></div>
</div>
<script src="js/jquery-3.3.1.min.js"></script>
<script>
$("#check-all").click(
function(){
if($(this).is(":checked")){
$("input[name='a']").prop("checked",true);
}
else{
$("input[name='a']").prop("checked",false);
}
}
)
</script>
</body>
</html>