6、显示购物车处理类(ShowCartServlet)
package net.wlq.shop.servlet;
/**
* 功能:显示购物车控制程序
* 作者:邓益春
* 日期:2016年12月16日。
*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.wlq.shop.bean.Product;
import net.wlq.shop.service.ProductService;
@WebServlet("/showCart")
public class ShowCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取商品类别标识符
int categoryId = Integer.parseInt(request.getParameter("categoryId"));
// 获取session对象
HttpSession session = request.getSession();
// 获取购物车
LinkedHashMap<Integer, Integer> cart = (LinkedHashMap<Integer, Integer>) session.getAttribute("cart");
// 判断购物车是否为空
if (cart != null) {
// 定义购物表
List<HashMap<String, Object>> shoppingTable = new ArrayList<HashMap<String, Object>>();
// 创建商品服务对象
ProductService productService = new ProductService();
// 购物总金额
double totalPrice = 0.0;
// 遍历购物车
for (Integer id : cart.keySet()) {
// 获取商品对象
Product product = productService.findProductById(id);
// 生成购物表记录
HashMap<String, Object> shoppingItem = new HashMap<String, Object>();
shoppingItem.put("id", product.getId());
shoppingItem.put("name", product.getName());
shoppingItem.put("price", product.getPrice());
shoppingItem.put("amount", cart.get(id));
shoppingItem.put("sum", product.getPrice() * cart.get(id));
// 将购物表记录添加到购物表中
shoppingTable.add(shoppingItem);
// 累加购买总金额
totalPrice = totalPrice + (Double) shoppingItem.get("sum");
}
// 将购物表和购买总金额保存到session里
session.setAttribute("shoppingTable", shoppingTable);
session.setAttribute("totalPrice", totalPrice);
// 重定向到显示商品控制程序
response.sendRedirect(request.getContextPath() + "/showProduct?categoryId=" + categoryId);
} else {
System.out.println("购物车里没有类别号为[" + categoryId + "]的商品!");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
重启服务器,访问http://localhost:8080/simonshop/showCart?categoryId=1:
7、操作购物车处理类(OperateCartServlet)
package net.wlq.shop.servlet;
/**
* 功能:操作购物车控制程序
* 作者:华卫
* 日期:2016年12月23日
*/
import java.io.IOException;
import java.util.LinkedHashMap;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.wlq.shop.service.ProductService;
@WebServlet("/operateCart")
public class OperateCartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取商品标识符
int id = Integer.parseInt(request.getParameter("id"));
// 获取商品操作
String operation = request.getParameter("operation");
// 从session里获取购物车(键:商品标识符;值:购买数量)
LinkedHashMap<Integer, Integer> cart = (LinkedHashMap<Integer, Integer>) request.getSession()
.getAttribute("cart");
// 判断购物车是否为空
if (cart == null) {
// 创建购物车
cart = new LinkedHashMap<Integer, Integer>();
// 将购物车保存到session里,便于用户在不同页面访问购物车
request.getSession().setAttribute("cart", cart);
}
if (operation.equals("add")) {
// 将商品添加到购物车
if (cart.containsKey(id)) { // 该商品已购买过
// 购买数量增加1
cart.put(id, cart.get(id) + 1);
} else { // 该商品未曾购买
// 购买数量设置为1
cart.put(id, 1);
}
} else if (operation.equals("delete")){
// 将商品从购物车删除
if (cart.get(id) > 1) {
// 购买数量减少1
cart.put(id, cart.get(id) - 1);
} else {
// 从购物车里删除该商品
cart.remove(id);
}
}
// 创建商品服务对象
ProductService productService = new ProductService();
// 获取该商品类别标识符
int categoryId = productService.findProductById(id).getCategoryId();
// 重定向到显示购物车控制程序
response.sendRedirect(request.getContextPath() + "/showCart?categoryId=" + categoryId);
}
}
}
8、生成订单处理类(MakeOrderServlet)
package net.wlq.shop.servlet;
/**
* 功能:生成订单控制程序
* 作者:邓益春
* 日期:2016年12月16日
*/
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.wlq.shop.bean.Order;
import net.wlq.shop.bean.User;
import net.wlq.shop.service.OrderService;
import net.wlq.shop.service.UserService;
import net.wlq.shop.service.OrderService;
import net.wlq.shop.service.UserService;
@WebServlet("/makeOrder")
public class MakeOrderServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
// 获取表单提交数据
String username = request.getParameter("username");
String telephone = request.getParameter("telephone");
Double totalPrice = Double.parseDouble(request.getParameter("totalPrice"));
String deliveryAddress = request.getParameter("deliveryAddress");
// 创建订单对象
Order order = new Order();
// 设置订单属性
order.setUsername(username);
order.setTotalPrice(totalPrice);
order.setTelephone(telephone);
order.setDeliveryAddress(deliveryAddress);
order.setOrderTime(new Date());
// 创建订单服务
OrderService orderService = new OrderService();
// 添加订单
orderService.addOrder(order);
// 获取最后一个订单
Order lastOrder = orderService.findLastOrder();
// 保存到session里
request.getSession().setAttribute("lastOrder", lastOrder);
// 重定向到显示订单页面
response.sendRedirect(request.getContextPath() + "/frontend/showOrder.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
9、支付处理类(PayServlet)
package net.wlq.shop.servlet;
/**
* 功能:支付处理程序
* 作者:邓益春
* 日期:2016年12月16日
*/
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/pay")
public class PayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 设置请求对象的字符编码
request.setCharacterEncoding("utf-8");
///
// 待做部分
// 设置session失效
request.getSession().invalidate();
// 重定向到登录页面
response.sendRedirect(request.getContextPath() + "/login.jsp");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
10、显示用户处理类(ShowUserServlet)
‘
package net.wlq.shop.servlet;
/**
- 功能:显示用户控制程序
- 作者:邓益春
- 日期:2016年12月16日
*/
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.wlq.shop.bean.User;
import net.wlq.shop.service.UserService;
@WebServlet("/showUser")
public class ShowUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 创建用户服务对象
UserService userService = new UserService();
// 获取全部用户
List<User> users = userService.findAllUsers();
// 获取session对象
HttpSession session = request.getSession();
// 把用户列表以属性的方式保存到session里
session.setAttribute("users", users);
// 重定向到显示用户页面(showUser.jsp)
response.sendRedirect(request.getContextPath() + "/backend/showUser.jsp");
// 在控制台输出所有用户信息
for (User user: users) {
System.out.println(user);
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
`
重启服务器,访问http://localhost:8080/simonshop/showUser:
四、实现步骤
(九)准备图片资源
在web目录里创建images目录,存放项目所需图片文件:
(十)CSS样式文件
在web目录里常见css子目录,在里面创建main.css文件:
`/* 样式 */
body {
margin: 0px;
text-align: center;
background: url("…/images/frontBack.jpg") no-repeat;
background-size: 100%
}
table {
margin: 0 auto;
font-size: 14px;
color: #333333;
border-width: 1px;
border-color: khaki;
border-collapse: collapse;
}
table th {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: gainsboro;
background-color: honeydew;
}
table td {
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: gainsboro;
background-color: #ffffff;
}
/登录页面样式/
.login {
width: 400px;
height: 340px;
background-color: honeydew;
border: solid 2px darkgrey;
left: 50%;
top: 50%;
position: absolute;
margin: -170px 0 0 -200px;
}
.login .websiteTitle, .title {
border: solid 1px floralwhite;
}
/注册页面样式/
.register {
width: 400px;
height: 350px;
background-color: honeydew;
border: solid 2px darkgrey;
left: 50%;
top: 50%;
position: absolute;
margin: -175px 0 0 -200px;
}
/显示类别页面样式/
.showCategory {
width: 400px;
height: 350px;
background-color: honeydew;
border: solid 2px darkgrey;
left: 50%;
top: 50%;
position: absolute;
margin: -150px 0 0 -200px;
}
/生成订单页面样式/
.makeOrder {
width: 400px;
height: 400px;
background-color: honeydew;
border: solid 2px darkgrey;
left: 50%;
top: 50%;
position: absolute;
margin: -200px 0 0 -200px;
}
/显示订单页面样式/
.showOrder {
width: 400px;
height: 400px;
background-color: honeydew;
border: solid 2px darkgrey;
left: 50%;
top: 50%;
position: absolute;
margin: -200px 0 0 -200px;
}
`
(十一)JavaScript脚本文件
在web目录里创建scripts子目录,在里面创建check.js文件:
`/**
- 检验登录表单
- @returns {Boolean}
*/
function checkLoginForm() {
var username = document.getElementById(“username”);
var password = document.getElementById(“password”);
if (username.value == “”) {
alert(“用户名不能为空!”);
username.focus();
return false;
}
if (password.value == “”) {
alert(“密码不能为空!”);
password.focus();
return false;
}
return true;
}
/**
-
检验注册表单
-
@returns {Boolean}
*/
function checkRegisterForm() {
var username = document.getElementById(“username”);
var password = document.getElementById(“password”);
var telephone = document.getElementById(“telephone”);
if (username.value == “”) {
alert(“用户名不能为空!”);
username.focus();
return false;
}
if (password.value == “”) {
alert(“密码不能为空!”);
password.focus();
return false;
}var pattern = “/^(13[0-9]|14[0-9]|15[0-9]|18[0-9])\d{8}$/”;
if (!pattern.exec(telephone)) {
alert(“非法手机号!”);
telephone.focus();
return false;
}
return true;
}
`
(十二)添加JSTL的jar包
在WEB-INF\lib目录里添加支持jstl的jar包:
jar包下载地址:http://tomcat.apache.org/taglibs/standard/
(十三)展现层页面(XXX.jsp)
1、登录页面login.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 9:35
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
西蒙购物网
用户登录
账号 | |
密码 | |
<c:if test="
r
e
g
i
s
t
e
r
M
s
g
!
=
n
u
l
l
"
>
<
s
c
r
i
p
t
t
y
p
e
=
"
t
e
x
t
/
j
a
v
a
s
c
r
i
p
t
"
>
a
l
e
r
t
(
"
{registerMsg!=null}"> <script type="text/javascript">alert("
registerMsg!=null"><scripttype="text/javascript">alert("{registerMsg}")
<c:remove var=“registerMsg”/>
</c:if>
<c:if test="
l
o
g
i
n
M
s
g
!
=
n
u
l
l
"
>
<
s
c
r
i
p
t
t
y
p
e
=
"
t
e
x
t
/
j
a
v
a
s
c
r
i
p
t
"
>
a
l
e
r
t
(
"
{loginMsg!=null}"> <script type="text/javascript">alert("
loginMsg!=null"><scripttype="text/javascript">alert("{loginMsg}")
<c:remove var=“loginMsg”/>
</c:if>
`
在web.xml文件里将login.jsp设置为首页文件:
!重启服务器:
不输入用户名与密码,单击【登录】按钮:
输入用户名,但不输入密码,单击【登录】按钮:
输入管理员用户名与密码:邓益春,06101
重启服务器,输入错误的用户名或密码:李文丽,12340
单击【登录】按钮,弹出消息框:用户名或密码错误!
2、注册页面register.jsp
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 10:36
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户注册</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
<script src="scripts/check.js" type="text/javascript"></script>
</head>
<body>
<div class="register">
<div class="websiteTitle">
<h1>西蒙购物网</h1>
</div>
<div class="title">
<h3>用户注册</h3>
</div>
<div class="main">
<form action="register" method="post">
<table>
<tr>
<td>账号</td>
<td><input id="username" type="text" name="username"/></td>
</tr>
<tr>
<td>密码</td>
<td><input id="password" type="password" name="password"/></td>
</tr>
<tr>
<td align="center">电话</td>
<td><input id="telephone" type="text" name="telephone"/></td>
</tr>
<tr align="center">
<td colspan="2">
<input type="submit" value="注册" onclick="return checkRegisterForm();"/>
<input type="reset" value="重置"/></td>
</tr>
</table>
</form>
</div>
<div class="footer">
<p><a href="login.jsp">切换到登录页面</a></p>
</div>
</div>
<c:if test="${registerMsg!=null}">
<script type="text/javascript">alert("${registerMsg}")</script>
<c:set var="registerMsg" value=""/>
</c:if>
</body>
</html>
启动服务器:
什么也不输入,单击【注册】按钮:
输入用户名,单击【注册】按钮
输入用户名、密码和电话,单击【注册】按钮:
单击【确定】按钮,跳转到登录页面:
3、显示商品类别页面showCategory.jsp
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 10:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>显示商品类别</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="showCategory">
<div class="websiteTitle">
<h1>西蒙购物网</h1>
</div>
<div>
登录用户:<span style="color: red;">${username}</span>
<c:forEach var="i" begin="1" end="5">
</c:forEach>
<a href="logout">注销</a>
</div>
<div class="title">
<h3>商品类别</h3>
</div>
<div class="main">
<table>
<tr>
<th>类别编号</th>
<th>商品类别</th>
</tr>
<c:forEach var="category" items="${categories}">
<tr align='center'>
<td>${category.id}</td>
<td width="150">
<a href="showProduct?categoryId=${category.id}">${category.name}</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</body>
</html>
启动服务器,显示登录页面,输入普通用户:郑晓红,11111
单击【登录】按钮,跳转到显示商品类别页面:
单击【家用电器】超链接:
我们去服务器端控制台查看输出信息:
返回到刚才显示商品类别页面:
单击【注销】超链接,返回登录页面:
4、显示购物车页面showCart.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 10:56
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=“fmt” uri=“http://java.sun.com/jsp/jstl/fmt” %>
<c:set var=“path” value=" p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h " / > < c : s e t v a r = " b a s e P a t h " v a l u e = " {pageContext.request.contextPath}"/> <c:set var="basePath" value=" pageContext.request.contextPath"/><c:setvar="basePath"value="{pageContext.request.scheme}😕/ p a g e C o n t e x t . r e q u e s t . s e r v e r N a m e : {pageContext.request.serverName}: pageContext.request.serverName:{pageContext.request.serverPort}${path}/"/>
${username}的购物车
商品编号 | 商品名称 | 销售价格 | 购买数量 | 合计金额 | 用户操作 | |
---|---|---|---|---|---|---|
${shoppingItem.id} | ${shoppingItem.name} | ¥${shoppingItem.price} | ${shoppingItem.amount} | ¥${shoppingItem.sum} | 删除 | |
总金额 | ¥0.00 | ¥${totalPrice} |
生成订单 生成订单 ` 这个页面要包含在显示商品页面里,因此没有导入CSS样式文件。 重启服务器,以普通用户登录后,访问http://localhost:8080/simonshop/frontend/showCart.jsp: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191216105917311.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hvbGxwX3Rv,size_16,color_FFFFFF,t_70)5、显示商品页面showProduct.jsp ![在这里插入图片描述](https://img-blog.csdnimg.cn/20191216110028490.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2hvbGxwX3Rv,size_16,color_FFFFFF,t_70)
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 10:59
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>显示商品信息</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>西蒙购物网</h1>
<hr width="700px">
登录用户:<span style="color: red;">${username}</span>
<c:forEach var="i" begin="1" end="5">
</c:forEach>
<a href="logout">注销</a></td>
<hr width="700px">
欢迎选购【<span style="color: blue; font-weight: bold;">${categoryName}</span>】类商品
<hr width="700px">
<table border="0">
<c:forEach varStatus="status" var="product" items="${products}">
<c:if test="${status.count%5==0}">
<tr>
</c:if>
<td>
<table border="0">
<tr><img src="images/product${product.id}.jpg" width="60px" height="60px"></tr>
<tr>
<td><b>商品编号:</b></td>
<td>${product.id}</td>
</tr>
<tr>
<td><b>商品名称:</b></td>
<td>${product.name}</td>
</tr>
<tr>
<td><b>销售价格:</b></td>
<td>${product.price}</td>
</tr>
<tr>
<td><b>上架时间:</b></td>
<td><fmt:formatDate value="${product.addTime}" pattern="yyyy-MM-dd"/></td>
</tr>
<tr>
<td><b>用户操作:</b></td>
<td><a href="operateCart?id=${product.id}&operation=add">加入购物车</a></td>
</tr>
</table>
</td>
<c:if test="${status.count%4==0}">
</tr>
</c:if>
</c:forEach>
</table>
<hr width="700px">
<a href="showCategory">返回商品类别页面</a>
<hr width="700px">
<jsp:include page="showCart.jsp"/>
</body>
</html>
启动服务器,显示登录页面,输入普通用户:郑晓红,11111
单击【登录】按钮,跳转到显示商品类别页面:
单击【家用电器】超链接:
有点小问题,类别名没有显示出来,页面跳转了,但是类别名没有传递成功。
修改ShowProductServlet,将类别名放在session里,而不是通过url传递参数。
重启服务器,登录成功后,选择【家用电器】类别:
下面测试加入购物车操作:
同一件商品可以多次加入购物车,也可以从购物车删除掉选购的商品。如果一件商品加入购物车的数量大于1,那么执行一次删除操作就是让购物车该商品数量减少1,直到减为0,即购物车里删除掉该商品。
6、生成订单页面makeOrder.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:16
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<c:set var=“path” value=" p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h " / > < c : s e t v a r = " b a s e P a t h " v a l u e = " {pageContext.request.contextPath}"/> <c:set var="basePath" value=" pageContext.request.contextPath"/><c:setvar="basePath"value="{pageContext.request.scheme}😕/ p a g e C o n t e x t . r e q u e s t . s e r v e r N a m e : {pageContext.request.serverName}: pageContext.request.serverName:{pageContext.request.serverPort}${path}/"/>
西蒙购物网
</c:forEach>
<a href="logout">注销</a>
</div>
<div class="title">
<h3>生成订单</h3>
</div>
<div class="main">
<form action="makeOrder" method="post">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username" readonly="readonly"
value="${username}"/></td>
</tr>
<tr>
<td>联系电话</td>
<td><input type="text" name="telephone"/></td>
</tr>
<tr>
<td>总金额</td>
<td><input type="text" name="totalPrice" readonly="readonly"
value="${totalPrice}"/></td>
</tr>
<tr>
<td>送货地址</td>
<td><input type="text" name="deliveryAddress"/></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" value="生成订单"/> <input
type="reset" value="重置"/></td>
</tr>
</table>
</form>
</div>
<div class="footer">
<p><a href="showCategory">返回商品类别页面</a></p>
</div>
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:17
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>显示订单</title>
<base href="${basePath}">
<link href="css/main.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div class="showOrder">
<div class="websiteTitle">
<h1>西蒙购物网</h1>
</div>
<div>
登录用户:<span style="color: red;">${username}</span>
<c:forEach var="i" begin="1" end="5">
</c:forEach>
<a href="logout">注销</a>
</div>
<div class="title">
<h3>生成订单</h3>
</div>
<div class="main">
<table border="1" cellspacing="0">
<tr>
<th>订单编号</th>
<td>${lastOrder.id}</td>
</tr>
<tr>
<th>用户名</th>
<td>${lastOrder.username}</td>
</tr>
<tr>
<th>联系电话</th>
<td>${lastOrder.telephone}</td>
</tr>
<tr>
<th>总金额</th>
<td>${lastOrder.totalPrice}</td>
</tr>
<tr>
<th>送货地址</th>
<td>${lastOrder.deliveryAddress}</td>
</tr>
</table>
</div>
<div class="footer">
<p><a href="pay" οnclick="alert('${lastOrder.username},支付成功!');">支付</a></p>
</div>
</div>
</body>
</html>
下面我们来测试生成订单与显示订单页面。
重启服务器,以普通用户登录,选择【家用电器】类商品,在显示商品页面,将一些商品加入购物车,查看购物车情况:
单击【生成订单】超链接,跳转到【生成订单】页面:
输入联系电话与送货地址:
单击【生成订单】按钮,跳转到【显示订单】页面:
单击【支付】按钮,提示支付成功:
单击【确定】按钮,返回登录页面:
8、后台管理主页面management.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:27
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<c:set var=“path” value=" p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h " / > < c : s e t v a r = " b a s e P a t h " v a l u e = " {pageContext.request.contextPath}"/> <c:set var="basePath" value=" pageContext.request.contextPath"/><c:setvar="basePath"value="{pageContext.request.scheme}😕/ p a g e C o n t e x t . r e q u e s t . s e r v e r N a m e : {pageContext.request.serverName}: pageContext.request.serverName:{pageContext.request.serverPort}${path}/"/>
`
9、后台管理主页面左面板页面left.jsp
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>后台管理左面板</title>
<base href="${basePath}">
<link rel="stylesheet" type="text/css">
<script type="text/javascript">
function show(id) {
var obj = document.getElementById('c_' + id);
if (obj.style.display == 'block') {
obj.style.display = 'none';
} else {
obj.style.display = 'block';
}
}
</script>
</head>
<body>
<table cellSpacing=0 cellPadding=0 width='100%' border=0>
<tbody>
<tr>
<td class=catemenu> <a
style='CURSOR: pointer' οnclick=show(1)><img src="images/folder.png">用户管理</a>
</td>
</tr>
<tbody id=c_1>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="showUser" target="main_frame">查看用户</a></td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">添加用户</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">更新用户</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">删除用户</a>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td class=catemenu> <a
style='CURSOR: pointer' οnclick=show(2)><img src="images/folder.png">
类别管理</a></td>
</tr>
<tbody id=c_2 style='DISPLAY: none'>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">查看类别</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">添加类别</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">更新类别</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">删除类别</a>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td class=catemenu> <a
style='CURSOR: pointer' οnclick=show(3)><img src="images/folder.png">
商品管理</a></td>
</tr>
<tbody id=c_3 style='DISPLAY: none'>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">查看商品</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">添加商品</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">更新商品</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">删除商品</a>
</td>
</tr>
</tbody>
<tbody>
<tr>
<td class=catemenu> <a
style='CURSOR: pointer' οnclick=show(4)><img src="images/folder.png">
订单管理</a></td>
</tr>
<tbody id=c_4 style='DISPLAY: none'>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">查看订单</a>
</td>
</tr>
<tr>
<td class=bar2 height=20> <img src="images/file.png"> <a href="backend/todo.jsp" target="main_frame">删除订单</a>
</td>
</tr>
</tbody>
</table>
</body>
</html>
10、后台管理主页面顶面板页面top.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:31
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<c:set var=“path” value=" p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h " / > < c : s e t v a r = " b a s e P a t h " v a l u e = " {pageContext.request.contextPath}"/> <c:set var="basePath" value=" pageContext.request.contextPath"/><c:setvar="basePath"value="{pageContext.request.scheme}😕/ p a g e C o n t e x t . r e q u e s t . s e r v e r N a m e : {pageContext.request.serverName}: pageContext.request.serverName:{pageContext.request.serverPort}${path}/"/>
![](images/title.png)
<%--
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<c:set var="basePath"
value="${pageContext.request.scheme}://${pageContext.request.serverName}:${pageContext.request.serverPort}${path}/"/>
<!DOCTYPE html>
<html>
<head>
<title>后台管理主面板</title>
<base href="${basePath}">
</head>
<body>
<img src="images/mainBack.gif" width="100%" height="100%"/>
</body>
</html>
重启服务器,以管理员身份登录(郑晓红:121212)进入后台管理页面:
12、查看用户页面showUser.jsp
`<%–
Created by IntelliJ IDEA.
User: 邓益春
Date: 2019/12/16
Time: 11:39
To change this template use File | Settings | File Templates.
–%>
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
<%@ taglib prefix=“fmt” uri=“http://java.sun.com/jsp/jstl/fmt” %>
<c:set var=“path” value=" p a g e C o n t e x t . r e q u e s t . c o n t e x t P a t h " / > < c : s e t v a r = " b a s e P a t h " v a l u e = " {pageContext.request.contextPath}"/> <c:set var="basePath" value=" pageContext.request.contextPath"/><c:setvar="basePath"value="{pageContext.request.scheme}😕/ p a g e C o n t e x t . r e q u e s t . s e r v e r N a m e : {pageContext.request.serverName}: pageContext.request.serverName:{pageContext.request.serverPort}${path}/"/>
登录用户:${username} | 注销 |
用户列表
编号 | 用户名 | 密码 | 电话 | 注册时间 | 权限 |
---|---|---|---|---|---|
${user.id} | ${user.username} | ${user.password} | ${user.telephone} | 管理员 普通用户 |
`
重启服务器,以管理员身份登录,进入后台管理页面,查看用户: