最近在学习 孙鑫《JAVA WEB开放详解》,看到第13章,其中网上书店程序作者把结算功能部门留给读者自己完成,我照着葫芦自己画了一个,不是很周全,请大家指正。
首先修改了showcart.jsp页面,在最后加入了进入计算中心的链接
<a href="index.jsp">继续购物</a>
<a href="balance.jsp">进入结算中心</a>然后增加了两个页面,分别是用来输入客户信息的balanace.jsp和进行结算确认的balance_check.jsp页面,代码如下:
balance.jsp

<%...@page contentType="text/html;charset=gb2312" %>
<%...@include file="common.jsp"%>
<%...@page import="java.util.Collection, java.util.Iterator"%>
<jsp:useBean id="cart" scope="session" class="com.ybcj.ch13.bookstore.CartBean"></jsp:useBean>
<html>
<head><title>欢迎光临网上书店</title></head>
<body>
<center>
<h1>结算中心</h1>
</center>
<%...
Collection cl = cart.getItems();
if(cl.size() <= 0) {
out.println("购物车中没有图书<p>");
%>
<a href="index.jsp">继续购物</a>
<%...
return;
}
Iterator it = cl.iterator();
%>
你所选购的图书:<br>
<table border="1">
<tr>
<th>书名</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
</tr>
<%...
int i =0;
while(it.hasNext()) {
CartItemBean cartItem =(CartItemBean)it.next();
BookBean book = cartItem.getBook();
int bookId = book.getId();
%>
<tr>
<td><%=book.getTitle() %></td>
<td><%=book.getPrice() %></td>
<td><%=cartItem.getQuantity() %></td>
<td><%=cartItem.getItemPrice() %></td>
</tr>
<%...
i++;
}
%>
<tr>
<td>合计</td>
<td colspan="3"><%=cart.getTotalPrice() %><td>
</tr>
</table><p>
<br>
<form name="orderform" action="balance_check.jsp" method="post">
<table>
<caption>个人信息</caption>
<tr>
<th align="right">姓名:</th>
<th align="left"><input type="text" name="name"></th>
</tr>
<tr>
<th align="right">电话:</th>
<th align="left"><input type="text" name="tel"></th>
</tr>
<tr>
<th align="right">送货地址:</th>
<th align="left"><input type="text" name="address" size="50"></th>
</tr>
<tr>
<th align="right">付款方式:</th>
<th align="left">
<input type="radio" name="payment" value="0" checked>现金
<input type="radio" name="payment" value="1">电汇
<input type="radio" name="payment" value="2">支票
<input type="radio" name="payment" value="3">银行
</th>
</tr>
<tr>
<th colspan="2" align="center">
<input type="submit" value="确认">
<a href="index.jsp">继续购物
</th>
</tr>
</table>
</form>
</body>
</html>balance_check.jsp

<%...@page contentType="text/html;charset=gb2312" %>
<%...@include file="common.jsp"%>
<%...@page import="java.util.Collection, java.util.Iterator"%>
<jsp:useBean id="cart" scope="session" class="com.ybcj.ch13.bookstore.CartBean"></jsp:useBean>
<html>
<head><title>欢迎光临网上书店</title></head>
<body>
<center>
<h1>订单确认</h1>
</center>
<%...
request.setCharacterEncoding("gb2312");
Collection cl = cart.getItems();
if(cl.size() <= 0) {
out.println("你没有购买任何图书<p>");
%>
<a href="index.jsp">继续购物</a>
<%...
return;
}
%>
<%...
String action = request.getParameter("action");
if(action != null && action.equals("确认")){
bookdb.buyBooks(cart);
out.println("订单确认成功,我们将在24小时内送货,请注意查收。");
cart.clear();
return;
}
%>
<%...
String name = request.getParameter("name");
String tel = request.getParameter("tel");
String address = request.getParameter("address");
String payment = request.getParameter("payment");
if(null == name || "".equals(name)) {
out.println("请填写收货人姓名");
//response.sendRedirect("balance.jsp");
out.println("<a href="balance.jsp">返回重填</a>");
return;
}
if(null == tel || "".equals(tel)){
out.println("请填写联系电话");
//response.sendRedirect("balance.jsp");
out.println("<a href="balance.jsp">返回重填</a>");
return;
}
if(null == address || "".equals(address)){
out.println("请填写送货地址");
//response.sendRedirect("balance.jsp");
out.println("<a href="balance.jsp">返回重填</a>");
return;
}
%>
<%...
Iterator it = cl.iterator();
%>
<table border="1">
<caption>你的订单</caption>
<tr>
<th>收货人姓名</th>
<th><%=name%></th>
</tr>
<tr>
<th>联系电话</th>
<th><%=tel%></th>
</tr>
<tr>
<th>送货地址</th>
<th><%=address%></th>
</tr>
<tr>
<th>选购的图书</th>
<td>
<table>
<tr>
<th>书名</th>
<th>价格</th>
<th>数量</th>
<th>小计</th>
</tr>
<%...
int i = 0;
while(it.hasNext()){
CartItemBean cartItem =(CartItemBean)it.next();
BookBean book = cartItem.getBook();
int bookId = book.getId();
%>
<tr>
<td><%=book.getTitle()%></td>
<td><%=book.getPrice()%></td>
<td><%=cartItem.getQuantity() %></td>
<td><%=cartItem.getItemPrice() %></td>
</tr> 
<%...
i++;
}
%>
</table>
</td>
</tr>
<tr>
<td>合计金额</td>
<td colspan="3"><%=cart.getTotalPrice() %><td>
</tr>
<tr>
<td>付款方式</td>
<td>
<%...
if("0".equals(payment))
out.println("货到付款");
else if("1".equals(payment))
out.println("电汇");
else if("2".equals(payment))
out.println("支票");
else if("3".equals(payment))
out.println("银行");
%>
</td>
</tr>
</table><p>
<form action="balance_check.jsp" method="post">
<input type="submit" name="action" value="确认">
<a href="balance.jsp">返回重填</a>
</form>
</html>运的效果如下:



程序应该存在很多错误和不合理的地方,希望能得到大家的意见。
本文分享了根据孙鑫《JAVAWEB开放详解》一书第13章网上书店项目的结算功能实现过程,包括修改购物车展示页面、创建客户信息输入页面及结算确认页面等。
415

被折叠的 条评论
为什么被折叠?



