Web层:
@WebServlet(name = "adminProductList",
urlPatterns = {"/adminProductList"})
public class adminProductList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//传递请求到service层
AdminProductService service = new AdminProductService();
List productList = null;
try {
productList = service.findAllProduct();
} catch (SQLException e) {
e.printStackTrace();
}
//将productList放到request域
request.setAttribute("productList", productList);
request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);
}
}
Service层:
public class AdminProductService {
public List findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
}
Dao层:
public class AdminProductDao {
public List findAllProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List productList = runner.query(sql, new BeanListHandler(Product.class));
return productList;
}
}
Jsp:
<c:forEach items="${productList }" var="pro" varStatus="vs">
<tr onmouseover="this.style.backgroundColor = 'white'"
onmouseout="this.style.backgroundColor = '#F5FAFE';">
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="18%">${vs.count }td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">
<img width="40" height="45" src="${pageContext.request.contextPath }/${pro.pimage }">
td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">${pro.pname }td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">${pro.shop_price }td>
<td style="CURSOR: hand; HEIGHT: 22px" align="center"
width="17%">${pro.is_hot==1?"是":"否" }td>
<td align="center" style="HEIGHT: 22px">
<a href="${ pageContext.request.contextPath }/adminUpdateProductUI?pid=${pro.pid}">
<img src="${pageContext.request.contextPath}/images/i_edit.gif"
border="0" style="CURSOR: hand">
a>td>
<td align="center" style="HEIGHT: 22px">
<a href="javascript:void(0);" onclick="delProduct('${pro.pid}')">
<img src="${pageContext.request.contextPath}/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
a>
td>
tr>
c:forEach>
Jsp页面,当点击了添加按钮之后就讲商品页面添加进来:
<script type="text/javascript">
function addProduct(){
window.location.href = "${pageContext.request.contextPath}/adminAddProductUI";
}
script>
Web层:
@WebServlet(name = "adminAddProductUI",
urlPatterns = {"/adminAddProductUI"})
public class adminAddProductUI extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得所有的商品的类别数据
AdminProductService service = new AdminProductService();
List categoryList = null;
try {
categoryList = service.findAllCategory();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("categoryList", categoryList);
request.getRequestDispatcher("/admin/product/add.jsp").forward(request, response);
}
}
Service层:
public class AdminProductService {
public List findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
}
Dao层:
public class AdminProductDao {
public List findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
List categoryList = runner.query(sql, new BeanListHandler(Category.class));
return categoryList;
}
}
add.jsp:
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
所属分类:
td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<select name="categorySecond.csid">
<c:forEach items="${categoryList }" var="category">
<option value="${category.cid}">${category.cname }option>
c:forEach>
select>
td>
tr>
Web层:
@WebServlet(name = "adminTianJiaProduct",
urlPatterns = {"/adminTianJiaProduct"})
public class adminTianJiaProduct extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//1、获取数据
Map properties = request.getParameterMap();
//2、封装数据
Product product = new Product();
try {
BeanUtils.populate(product, properties);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//此位置Product已经封装完毕----将表单的数据封装完毕
//手动设置表单中没有数据
//1)、private String pid;
product.setPid(UUID.randomUUID().toString());
//2)、private String pimage;
product.setPimage("products/1/c_0033.jpg");
//3)、private String pdate;//上架日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String pdate = format.format(new Date());
product.setPdate(pdate);
//4)、private int pflag;//商品是否下载 0代表未下架
product.setPflag(0);
//3、传递数据给service层
AdminProductService service = new AdminProductService();
try {
service.addProduct(product);
} catch (SQLException e) {
e.printStackTrace();
}
//跳转到列表页面
response.sendRedirect(request.getContextPath()+"/adminProductList");
}
}
Service层:
public class AdminProductService {
public List findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
//获得所有的类别
public List findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
//添加数据
public void addProduct(Product product) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.addProduct(product);
}
}
Dao层:
public class AdminProductDao {
public List findAllProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List productList = runner.query(sql, new BeanListHandler(Product.class));
return productList;
}
public List findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
List categoryList = runner.query(sql, new BeanListHandler(Category.class));
return categoryList;
}
public void addProduct(Product product) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid());
}
}
Jsp:
Web层:
@WebServlet(name = "adminDelProduct",
urlPatterns = {"/adminDelProduct"})
public class adminDelProduct extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取要删除的pid
String pid = request.getParameter("pid");
//传递pid到service层
AdminProductService service = new AdminProductService();
try {
service.delProductByPid(pid);
} catch (SQLException e) {
e.printStackTrace();
}
response.sendRedirect(request.getContextPath()+"/adminProductList");
}
}
Service层:
public class AdminProductService {
public List findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
//获得所有的类别
public List findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
//添加数据
public void addProduct(Product product) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.addProduct(product);
}
//根据pid删除商品
public void delProductByPid(String pid) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.delProductByPid(pid);
}
}
Dao层:
public class AdminProductDao {
public List findAllProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List productList = runner.query(sql, new BeanListHandler(Product.class));
return productList;
}
public List findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
List categoryList = runner.query(sql, new BeanListHandler(Category.class));
return categoryList;
}
public void addProduct(Product product) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid());
}
public void delProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from product where pid=?";
runner.update(sql, pid);
}
}
Jsp关键代码:
<script type="text/javascript">
function addProduct(){
window.location.href = "${pageContext.request.contextPath}/adminAddProductUI";
}
function delProduct(pid){
var isDel = confirm("您确认要删除吗?");
if(isDel){
//要删除
location.href = "${pageContext.request.contextPath}/adminDelProduct?pid="+pid;
}
}
script>
<td align="center" style="HEIGHT: 22px">
<a href="javascript:void(0);" onclick="delProduct('${pro.pid}')">
<img src="${pageContext.request.contextPath}/images/i_del.gif"
width="16" height="16" border="0" style="CURSOR: hand">
a>
td>
运行结果:
点击删除就没了:
list.jsp关键代码:
<td align="center" style="HEIGHT: 22px"><a
href="${ pageContext.request.contextPath }/adminUpdateProductUI?pid=${pro.pid}">
<img src="${pageContext.request.contextPath}/images/i_edit.gif"
border="0" style="CURSOR: hand">
a>td>
edit.jsp:
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品名称:
td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="pname" value="${product.pname }" id="userAction_save_do_logonName" class="bg"/>
td>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
是否热门:
td>
<td class="ta_01" bgColor="#ffffff">
<select id="is_hot" name="is_hot">
<option value="1">是option>
<option value="0">否option>
select>
td>
tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
市场价格:
td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="market_price" value="${product.market_price }" id="userAction_save_do_logonName" class="bg"/>
td>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商城价格:
td>
<td class="ta_01" bgColor="#ffffff">
<input type="text" name="shop_price" value="${product.shop_price }" id="userAction_save_do_logonName" class="bg"/>
td>
tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品图片:
td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<input type="file" name="upload" />
td>
tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
所属分类:
td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<select id="cid" name="cid">
<c:forEach items="${categoryList }" var="category">
<option value="${category.cid }">${category.cname }option>
c:forEach>
select>
td>
tr>
<tr>
<td width="18%" align="center" bgColor="#f5fafe" class="ta_01">
商品描述:
td>
<td class="ta_01" bgColor="#ffffff" colspan="3">
<textarea name="pdesc" rows="5" cols="30">${product.pdesc }textarea>
td>
tr>
Web层:
@WebServlet(name = "adminUpdateProductUI",
urlPatterns = {"/adminUpdateProductUI"})
public class adminUpdateProductUI extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得要查询Product的pid
String pid = request.getParameter("pid");
//传递pid查询商品信息
AdminProductService service = new AdminProductService();
Product product = null;
try {
product = service.findProductByPid(pid);
} catch (SQLException e) {
e.printStackTrace();
}
//获得所有的商品的类别数据
List categoryList = null;
try {
categoryList = service.findAllCategory();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("categoryList", categoryList);
request.setAttribute("product", product);
request.getRequestDispatcher("/admin/product/edit.jsp").forward(request, response);
}
}
Service层:
public class AdminProductService {
public List findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
//获得所有的类别
public List findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
//添加数据
public void addProduct(Product product) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.addProduct(product);
}
//根据pid删除商品
public void delProductByPid(String pid) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.delProductByPid(pid);
}
//根据pid查询商品对象
public Product findProductByPid(String pid) throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findProductByPid(pid);
}
}
Dao层:
public class AdminProductDao {
public List findAllProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List productList = runner.query(sql, new BeanListHandler(Product.class));
return productList;
}
public List findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
List categoryList = runner.query(sql, new BeanListHandler(Category.class));
return categoryList;
}
public void addProduct(Product product) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid());
}
public void delProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from product where pid=?";
runner.update(sql, pid);
}
public Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
Product product = runner.query(sql, new BeanHandler(Product.class), pid);
return product;
}
}
运行结果:
当我们点击修改按钮之后,该商品的信息会回显到页面上:
Jsp页面:
Web层:
@WebServlet(name = "adminUpdateProduct",
urlPatterns = {"/adminUpdateProduct"})
public class adminUpdateProduct extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
//1、获取数据
Map properties = request.getParameterMap();
//2、封装数据
Product product = new Product();
try {
BeanUtils.populate(product, properties);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
//此位置Product已经封装完毕----将表单的数据封装完毕
//手动设置表单中没有数据
//2)、private String pimage;
product.setPimage("products/1/c_0033.jpg");
//3)、private String pdate;//上架日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String pdate = format.format(new Date());
product.setPdate(pdate);
//4)、private int pflag;//商品是否下载 0代表未下架
product.setPflag(0);
//3、传递数据给service层
AdminProductService service = new AdminProductService();
try {
service.updateProduct(product);
} catch (SQLException e) {
e.printStackTrace();
}
//跳转到列表页面
response.sendRedirect(request.getContextPath()+"/adminProductList");
}
}
Service层:
public class AdminProductService {
public List findAllProduct() throws SQLException {
//因为没有复杂业务,直接传递请求到dao层
AdminProductDao dao = new AdminProductDao();
return dao.findAllProduct();
}
//获得所有的类别
public List findAllCategory() throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findAllCategory();
}
//添加数据
public void addProduct(Product product) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.addProduct(product);
}
//根据pid删除商品
public void delProductByPid(String pid) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.delProductByPid(pid);
}
//根据pid查询商品对象
public Product findProductByPid(String pid) throws SQLException {
AdminProductDao dao = new AdminProductDao();
return dao.findProductByPid(pid);
}
//更新商品
public void updateProduct(Product product) throws SQLException {
AdminProductDao dao = new AdminProductDao();
dao.updateProduct(product);
}
}
Dao层:
public class AdminProductDao {
public List findAllProduct() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product";
List productList = runner.query(sql, new BeanListHandler(Product.class));
return productList;
}
public List findAllCategory() throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from category";
List categoryList = runner.query(sql, new BeanListHandler(Category.class));
return categoryList;
}
public void addProduct(Product product) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "insert into product values(?,?,?,?,?,?,?,?,?,?)";
runner.update(sql, product.getPid(),product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid());
}
public void delProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "delete from product where pid=?";
runner.update(sql, pid);
}
public Product findProductByPid(String pid) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "select * from product where pid=?";
Product product = runner.query(sql, new BeanHandler(Product.class), pid);
return product;
}
public void updateProduct(Product product) throws SQLException {
QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
String sql = "update product set pname=?,market_price=?,shop_price=?,pimage=?,pdate=?,is_hot=?,pdesc=?,pflag=?,cid=? where pid=?";
runner.update(sql,product.getPname(),product.getMarket_price(),
product.getShop_price(),product.getPimage(),product.getPdate(),product.getIs_hot(),
product.getPdesc(),product.getPflag(),product.getCid(),product.getPid());
}
}
转载请标明出处,原文地址:https://blog.csdn.net/weixin_41835916
如果觉得本文对您有帮助,请点击顶支持一下,您的支持是我写作最大的动力,谢谢。