在5-11作业的基础之上, 将之前的HTML页面替换为 jsp页面 ;
修改:
执行查询功能的时候,将所有商品内容 ,直接在当前页面显示,不再跳转页面。
答:
Product:
package com.qiku.entity;
/*
商品表 对应 Product
*/
public class Product {
private String pid;//商品编号
private String pname;//商品名称
private double price;//商品价格
private String pdesc;//商品介绍
private int pflag;//商品的状态 1 上架 0 下架
private String cid;//外键 对应分类表的主键
public Product() {
}
public Product(String pid, String pname, double price, String pdesc, int pflag, String cid) {
this.pid = pid;
this.pname = pname;
this.price = price;
this.pdesc = pdesc;
this.pflag = pflag;
this.cid = cid;
}
@Override
public String toString() {
return "Product{" +
"pid='" + pid + '\'' +
", pname='" + pname + '\'' +
", price=" + price +
", pdesc='" + pdesc + '\'' +
", pflag=" + pflag +
", cid='" + cid + '\'' +
'}';
}
public String getPid() {
return pid;
}
public void setPid(String pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
public int getPflag() {
return pflag;
}
public void setPflag(int pflag) {
this.pflag = pflag;
}
public String getCid() {
return cid;
}
public void setCid(String cid) {
this.cid = cid;
}
}
productDao:
package com.qiku.dao;
import com.qiku.Utils.DruidUtils;
import com.qiku.entity.Product;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
public class ProductDao {
QueryRunner qr = new QueryRunner(DruidUtils.dataSource);
// 查询所有商品
public List<Product> findAllProduct() throws SQLException {
String sql = "select * from product";
List<Product> productList = qr.query(sql, new BeanListHandler<Product>(Product.class));
return productList;
}
// 新增商品
public void addProduct(Product product) {
try {
String sql = " insert into product values(?,?,?,?,?,?)";
qr.update(sql,product.getPid(),product.getPname(),product.getPrice(),product.getPdesc(),product.getPflag(),product.getCid());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
addProductServlet:
@WebServlet(name = "addProductServlet" , urlPatterns = "/addProduct")
public class addProductServlet extends HttpServlet {
ProductDao productDao = new ProductDao();
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
// 获取商品信息
String pid = req.getParameter("pid");
String pname = req.getParameter("pname");
double price =Double.parseDouble(req.getParameter("price")) ;
String pdesc = req.getParameter("pdesc");
int pflag =Integer.parseInt(req.getParameter("pflag")) ;
String cid = req.getParameter("cid");
//将上述信息封装成一个 product对象
Product product = new Product(pid, pname, price, pdesc, pflag, cid);
// 将product作为参数传递给ProducyDao 去存入数据库
productDao.addProduct(product);
// 重定向到 查询页面
resp.sendRedirect("allProduct.jsp");
}
}
addProduct.jsp:
<%--
Created by IntelliJ IDEA.
User: fate.j
Date: 2022/5/14
Time: 9:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加商品</title>
</head>
<body>
<form action="addProduct" method="post">
<p>商品编号:<input type="text" name="pid"></p>
<p>商品名称:<input type="text" name="pname"></p>
<p>商品价格:<input type="text" name="price"></p>
<p>商品描述:<input type="text" name="pdesc"></p>
<p>商品状态:<input type="text" name="pflag"></p>
<p>商品分类:<input type="text" name="cid"></p>
<input type="submit" value="确定">
</form>
</body>
</html>
allProduct.jsp:
<%@ page import="com.qiku.dao.ProductDao" %>
<%@ page import="com.qiku.entity.Product" %>
<%@ page import="java.util.List" %><%--
Created by IntelliJ IDEA.
User: fate.j
Date: 2022/5/14
Time: 9:29
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查询所以商品</title>
</head>
<body>
<table border="1">
<tr>
<td>pid</td>
<td>pname</td>
<td>price</td>
<td>pdesc</td>
<td>pflag</td>
<td>cid</td>
</tr>
<tr>
<%
ProductDao productDao = new ProductDao();
List<Product> allProduct = productDao.findAllProduct();
for (Product product : allProduct) {
String pid = product.getPid();
String pname = product.getPname();
double price = product.getPrice();
String pdesc = product.getPdesc();
int pflag = product.getPflag();
String cid = product.getCid();
%>
<td><% out.print(pid); %></td>
<td><% out.print(pname); %></td>
<td><% out.print(price); %></td>
<td><% out.print(pdesc); %></td>
<td><% out.print(pflag); %></td>
<td><% out.println(cid); %></td>
</tr>
<%
out.flush();
}
%>
</table>
</body>
</html>
运行结果查询: