实训——用JSP+JavaBean开发购物车模块
一. 需求描述
首先需要一个列出所有商品的页面。单击 “购买” 链接,则可以将商品添加到购物车里面。单击 “查看购物车” 链接,则可以查看购物车里面的所有商品。
二. 模块实现
文件 | 功能 |
---|---|
ShowCommodityList.jsp | 1.显示商品列表;2.每个商品旁有一个“购买”链接,单击它则可以进行购买;3.包含一个”查看购物车”的链接,单击则可查看购物车 |
AddToCar.jsp | 1.添加商品到购物车;2.显示购物车中所有的商品 |
三. 程序代码
Commodity.java
package com.javaweb.bean;
public class Commodity {
private int commodityId; //商品ID
private String commodityName; //商品名称
private Double price; //商品价格
private Double agio; //商品折扣
public int getCommodityId() {
return commodityId;
}
public void setCommodityId(int commodityId) {
this.commodityId = commodityId;
}
public String getCommodityName() {
return commodityName;
}
public void setCommodityName(String commodityName) {
this.commodityName = commodityName;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public Double getAgio() {
return agio;
}
public void setAgio(Double agio) {
this.agio = agio;
}
}
CommodityDAO.java
package com.javaweb.dao;
import java.util.List;
import com.javaweb.bean.Commodity;;
public interface CommodityDAO {
public void addCommodity(Commodity commodity);//定义添加商品的方法
public void updateCommodity(Commodity commodity);//定义修改商品的方法
public void deleteCommodity(int commodityId);//定义删除商品的方法
public List<Commodity> findAllCommodity();//定义查询商品的方法
public Commodity findCommodityById(int commodityId);//定义按ID查询商品的方法
}
CommodityDAOImpl.java
package com.javaweb.dao;
import com.javaweb.bean.Commodity;
import com.javaweb.util.DBConnection;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CommodityDAOImpl implements CommodityDAO{
public void addCommodity(Commodity commodity){
Connection conn = DBConnection.getConnection();//获得连接对象
String addSQL = "insert into commodity(commodityName,price,agio)values(?,?,?)";
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(addSQL);//获得预处理对象并赋值
pstmt.setString(1, commodity.getCommodityName());//设置第一个参数
pstmt.setDouble(2, commodity.getPrice());//设置第二参数
pstmt.setDouble(3, commodity.getAgio());//设置第三个参数
pstmt.executeUpdate();//执行更新
}catch(SQLException e){
e.printStackTrace();
}finally{
DBConnection.close(pstmt);//关闭预处理对象
DBConnection.close(conn);//关闭连接对象
}
}
public void updateCommodity(Commodity commodity){
Connection conn = DBConnection.getConnection();//获得连接对象
String updateSQL = "update commodity set commodityName = ?," + "price = ?, agio = ? where commodityId = ?";
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(updateSQL);//获得预处理对象并赋值
pstmt.setString(1, commodity.getCommodityName());//设置第一个参数
pstmt.setDouble(2, commodity.getPrice());//设置第二参数
pstmt.setDouble(3, commodity.getAgio());//设置第三个参数
pstmt.setInt(4,commodity.getCommodityId());//设置第四个参数
pstmt.executeUpdate();//执行更新
}catch(SQLException e){
e.printStackTrace();
}finally{
DBConnection.close(pstmt);//关闭预处理对象
DBConnection.close(conn);//关闭连接对象
}
}
public void deleteCommodity(int commodityId){
Connection conn = DBConnection.getConnection();//获得连接对象
String deleteSQL = "delete from commodity where commodityId = ?";
PreparedStatement pstmt = null;
try{
pstmt = conn.prepareStatement(deleteSQL);//获得预处理对象并赋值
pstmt.setInt(1,commodityId);//设置第一个参数
pstmt.executeUpdate();//执行更新
}catch(SQLException e){
e.printStackTrace();
}finally{
DBConnection.close(pstmt);//关闭预处理对象
DBConnection.close(conn);//关闭连接对象
}
}
public List<Commodity> findAllCommodity(){
Connection conn = DBConnection.getConnection();//获得连接对象
String querySQL = "select * from commodity";
PreparedStatement pstmt = null;
List<Commodity> commodityList = new ArrayList<Commodity>();
try{
pstmt = conn.prepareStatement(querySQL);//获得预处理对象并赋值
ResultSet rs = pstmt.executeQuery();
while(rs.next()){
Commodity commodity = new Commodity();//实例化商品对象
commodity.setCommodityId(rs.getInt(1));//获得商品ID
commodity.setCommodityName(rs.getString(2));//获得商品名称
commodity.setPrice(rs.getDouble(3));//获得商品价格
commodity.setAgio(rs.getDouble(4));//获得商品折扣
commodityList.add(commodity);//添加到列表当中
}
pstmt.executeUpdate();//执行更新
}catch(SQLException e){
e.printStackTrace();
}finally{
DBConnection.close(pstmt);//关闭预处理对象
DBConnection.close(conn);//关闭连接对象
}
return commodityList;//返回查询到的所有商品
}
public Commodity findCommodityById(int commodityId){
Connection conn = DBConnection.getConnection();//获得连接对象
String querySQL = "select * form commodity where commodityId = ?";
PreparedStatement pstmt = null;//声明预处理对象
Commodity commodity = new Commodity();
try{
pstmt = conn.prepareStatement(querySQL);//获得预处理对象
pstmt.setInt(1, commodityId);//设置参数
ResultSet rs = pstmt.executeQuery();//执行查询
if(rs.next()){
commodity.setCommodityId(rs.getInt(1));//获得商品ID
commodity.setCommodityName(rs.getString(2));//获得商品名称
commodity.setPrice(rs.getDouble(3));//获得商品价格
commodity.setAgio(rs.getDouble(4));//获得商品折扣
}
}catch(SQLException e){
e.printStackTrace();
}finally{
DBConnection.close(pstmt);//关闭预处理对象
DBConnection.close(conn);//关闭连接对象
}
return commodity;
}
}
ShowCommodityList.jsp:
<%@ page language = "java" pageEncoding = "gb2312" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import = "com.javaweb.dao.CommodityDAO" %>
<%@ page import = "com.javaweb.factory.CommodityDAOFactory" %>
<%@ page import = "com.javaweb.bean.Commodity" %>
<html>
<head>
<title>
商品列表
</title>
</head>
<body>
<%
//通过DAO工厂类获得DAO实现类实例
CommodityDAO commodityDAO = CommodityDAOFactory.getCommodityDAOInstance();
//查找所有商品记录
List<Commodity> commodityList = commodityDAO.findAllCommodity();
//将所有记录保存到page范围
pageContext.setAttribute("commodityList", commodityList);
%>
<table width = "700" border = "1" >
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
<td>商品折扣</td>
<td>优惠价格</td>
<td>购买</td>
</tr>
<%-- 循环输出商品信息 --%>
<c:forEach var = "commodity" items = "${pageScope.commodityList}">
<tr>
<td>${commodity.commodityId}</td>
<td>${commodity.commodityName}</td>
<td><fmt:formatNumber type = "currency" value = "${commodity.price}"/></td>
<td>${commodity.agio}</td>
<td><fmt:formatNumber type = "currency" value = "${commodity.price * commodity.agio}"/></td>
<td><a href = "AddToCar.jsp?commodityId=${commodity.commodityId}">购买</a></td>
</tr>
</c:forEach>
<tr>
<td><a href = "AddToCar.jsp">查看购物车>></a></td>
</tr>
</table>
</body>
</html>
AddToCar.jsp
<%@ page language = "java" pageEncoding = "gb2312" %>
<%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix = "fmt" uri = "http://java.sun.com/jsp/jstl/fmt" %>
<%@ page import = "com.javaweb.dao.CommodityDAO" %>
<%@ page import = "com.javaweb.factory.CommodityDAOFactory" %>
<%@ page import = "com.javaweb.bean.Commodity" %>
<html>
<head>
<title>
购物车列表
</title>
</head>
<body>
<%
if(request.getParameter("commodityId") != null){//如果commodityId参数不为空
List<Commodity> car = null;//声明一个购物车
if(session.getAttribute("car") == null){//如果session中不存在购物车
car = new ArrayList<Commodity>(); //新建一个ArrayList实例
}else{
car = (List<Commodity>)session.getAttribute("car");
}
//通过DAO工厂类获得DAO实例
CommodityDAO commodityDAO = CommodityDAOFactory.getCommodityDAOInstance();
//获得commodityId参数,并进行类型转换
int commodityId = Integer.parseInt(request.getParameter("commodityId"));
//将商品添加到购物车中
car.add(commodityDAO.findCommodityById(commodityId));
session.setAttribute("car", car);//将购物车保存在session中
}
%>
你已经购买了如下商品:
<table width = "500" border = "1">
<tr>
<td>商品ID</td>
<td>商品名称</td>
<td>商品价格</td>
</tr>
<%-- 输出购物车中商品信息 --%>
<c:forEach var = "commodity" items = "${sessionScope.car}">
<tr>
<td>${commodity.commodityId}</td>
<td>${commodity.commodityName}</td>
<td><fmt:formatNumber type = "currency" value = "${commodity.price * commodity.agio}"/></td>
</tr>
</c:forEach>
<tr>
<td><a href = "ShwoCommodityList.jsp"><<继续购物</a></td>
</tr>
</table>
</body>
</html>
在浏览器输入http://localhost:8080/shopping/ShowCommodityList.jsp。如下图:
摘自《Java Web程序设计与项目实践》,电子工业出版社