hello,大家好,我又来啦,今天要和大家分享的是EL与JSTL的使用:
今天的内容是基于上次的知识哦
目录
1.index.jsp 主界面
<%@page import="com.zking.pojo.Goods"%>
<%@page import="com.zking.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@page import="com.zking.pojo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(3)::before{
content: "$";
}
</style>
</head>
<body>
<c:if test="${user==null}">
<c:redirect url="login.jsp"></c:redirect>
</c:if>
<c:if test="${empty goods}"><c:redirect url="doIndex.jsp"></c:redirect></c:if>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临 🌌源星球SuperMarket</h1>
<p>尊贵的${user.uname}</p>
<p><a href="Car.jsp" class="btn btn-primary">🛒</a></p>
<p><a href="doExit.jsp" class="btn btn-warning">退出</a></p>
</div>
</div>
<table class="table">
<tr>
<th>商品序号</th>
<th>商品名称</th>
<th>商品单价</th>
<th>商品描述</th>
<th>操作</th>
</tr>
<c:forEach items="${goods}" var="g">
<tr>
<td>${g.id}</td>
<td>${g.name}</td>
<td>${g.price}</td>
<td>${g.info}</td>
<td>
<div class="btn-group btn-group-xs">
<button class="btn btn-primary"><span class="glyphicon glyphicon-tags"></span></button>
<%--链接这里需要带id过去 --%>
<a href="doAddCar.jsp?id=${g.id}" class="btn btn-primary">添加购物车</a>
</div>
</td>
</tr>
</c:forEach>
</table>
<div style="text-align:center">
<ul class="pagination" style="">
<li>
<a href="doIndex.jsp?ipage=${Math.max(i-1,1)}">
<span aria-hidden="true">«</span>
</a>
</li>
<c:forEach begin="1" end="${maxPage}" var="i">
<li class="${ipage==i?"active":""}"><a href="doIndex.jsp?ipage=${i}">${i}</a></li>
</c:forEach>
<li><a href="doIndex.jsp?ipage=${i+1==maxPage?maxPage:i+1}" aria-label="Next"><span aria-hidden="true">»</span></a></li>
</ul>
</div>
</body></html>
2.doIndex.jsp
<%@page import="com.zking.pojo.Goods"%>
<%@page import="java.util.List"%>
<%@page import="com.zking.biz.impl.GoodsBizImpl"%>
<%@page import="com.zking.biz.IGoodsBiz"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%
String parameter=request.getParameter("ipage");
int ipage=1;//默认第一页
if(parameter!=null){
ipage=Integer.parseInt(parameter);
}
//得到商品biz
IGoodsBiz goodsBiz=new GoodsBizImpl();
//将商品查询出来,并且放到请求域当中
session.setAttribute("goods",goodsBiz.getAll(ipage));
//算出页数 带到index。jsp
int row=goodsBiz.getRowCount();
session.setAttribute("maxPage",(int)Math.ceil(row*1.0/2) );
session.setAttribute("ipage",ipage);
//希望把数据携带index.jsp中
response.sendRedirect("index.jsp");
%>
3.购物车优化界面(Car.jsp)
<%@page import="com.zking.pojo.User"%>
<%@page import="com.zking.vo.CarItem"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css">
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
<script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
<style>
td:nth-child(4)::before,
small {
content: "$";
}
* {
outline: none !important;
}
td,
th {
text-align: center;
}
input {
text-align: center;
}
</style>
</head>
<%
Object obj=session.getAttribute("user");
if(obj==null){
response.sendRedirect("login.jsp");
return;
}
%>
<body>
<div class="jumbotron">
<div class="container">
<h1>欢迎光临🌌源星球购物车🛒</h1>
<p>尊贵的${user.uname}</p>
<p><a href="doExit.jsp" class="btn btn-warning">退出</a></p>
</div>
</div>
<table class="table">
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品个数</th>
<th>商品总价</th>
<th>操作</th>
</tr>
<c:forEach items="${car}" var="c">
<tr>
<td style="line-height: 30.5px;">${c.goods.id}</td>
<td style="line-height: 30.5px;">${c.goods.name}</td>
<td>
<div class="input-group" style="width: 120px;margin: auto;">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id}&type=0" class="btn btn-default" type="button">-</a>
</span>
<input type="number" onblur="location.href='doUpdCar.jsp?id=${c.goods.id}&count='+this.value" value="${c.count}" class="form-control">
<span class="input-group-btn">
<a href="doUpdCar.jsp?id=${c.goods.id}&type=1" class="btn btn-default" type="button">+</a>
</span>
</div>
</td>
<td style="line-height: 30.5px;">${c.sum}</td>
<td style="line-height: 30.5px;">
<a href="doDelCar.jsp?id=${c.goods.id}" class="btn btn-primary">删除</a>
</td>
</tr>
</c:forEach>
</table>
<h1 class="alert alert-info">
当前购物车总价
<small></small>
<a href="doClear.jsp" class="btn btn-danger">点我结算</a>
</h1>
</body></html>