zoukankan      html  css  js  c++  java
  • 商城页面的增删改查

    manage-----index.jsp

            <em>商品管理</em>
                            </a>
                            <dl class="layui-nav-child">
                                <dd><a href="${pageContext.request.contextPath }/ListProductServlet">商品列表</a></dd>
                                

    创建ListProductServlet类:直接调用查询所有商品的方法。

    public class ListProductServlet extends HttpServlet {
    
    private ProductService productService=new ProductService();
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //获取所有商品
            List<Product> list=productService.getProduct();
            //将商品放到request域中
            request.setAttribute("ProductList",list);
            //请求转发
            request.getRequestDispatcher("/manage/product/list.jsp").forward(request, response);
            
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }

    manage---product----list.jsp:展示商品:

    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/view.css" />
        <c:forEach items="${ProductList }" var="pro" varStatus="vs">
                                        <tr>
                                        <td>${vs.count }</td><!-- ${vs.index }从1开始 -->
                                        <td><img alt="" src="${pro.pimage }"></td>
                                        <td>${pro.pname }</td>
                                        <td>${pro.market_price }</td>
                                        <td>${pro.is_hot==1?"是":"否" }</td>
                                        <td>
                                            <div class="layui-btn-group">
                                                <div class="oper">
                                                    <button type="button"
                                                        class="layui-btn layui-btn-blue layui-btn-sm" onClick="editProduct()">
                                                        <i class="layui-icon">&#xe642;</i> 编辑
                                                    </button>
                                                </div>
                                                <div class="del">
                                                    <button type="button"
                                                        class="layui-btn-danger layui-btn layui-btn-red layui-btn-sm">
                                                        <i class="layui-icon">&#xe640;</i>删除
                                                    </button>
                                                </div>
                                            </div>
                                        </td>
    
                                    </tr>
                                </c:forEach>
        <script type="text/javascript">
            function addProduct(){
                location.href="${pageContext.request.contextPath }/ProductAddUIServlet";
            }
            function editProduct(){
                location.href="edit.jsp";
            }
        </script>

    当一点击新增按钮,跳转到ProductAddUIServlet类,创建ProductAddUIServlet类,只需要查询数据,直接写导层。

    先封装实体类:

    package com.oracle.domain;
    
    public class Category {
    private String cid;
    private String cname;
    public String getCid() {
        return cid;
    }
    public void setCid(String cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    @Override
    public String toString() {
        return "Category [cid=" + cid + ", cname=" + cname + "]";
    }
    
    }
    package com.oracle.dao;

    import java.sql.SQLException;
    import java.util.List;

    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;

    import com.oracle.domain.Category;
    import com.oracle.domain.Product;
    import com.oracle.tools.MyDBUtils;

    public class CategoryDao {
    //查询所有分类
        public List<Category> getCategory() throws SQLException{
            QueryRunner qr=new QueryRunner(MyDBUtils.getDataSource());
            String sql="select * from category";
            List<Category> list=qr.query(sql,new BeanListHandler<Category>(Category.class));
            return list;
        }
    }


    service层:

    package com.oracle.service;
    
    import java.sql.SQLException;
    import java.util.List;
    
    import org.apache.commons.dbutils.QueryRunner;
    import org.apache.commons.dbutils.handlers.BeanListHandler;
    
    import com.oracle.dao.CategoryDao;
    import com.oracle.domain.Category;
    import com.oracle.tools.MyDBUtils;
    
    public class CategoryService {
    private CategoryDao categoryDao=new CategoryDao();
    //查询所有分类
    public List<Category> getCategory() {
        List<Category> list=null;
        try {
            list=categoryDao.getCategory();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return list;
    }
    }

    ProductAddUIServlet类:

    package com.oracle.web.product;
    
    import java.io.IOException;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import com.oracle.domain.Category;
    import com.oracle.service.CategoryService;
    
    public class ProductAddUIServlet extends HttpServlet {
        private CategoryService categoryService=new CategoryService();
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Category> list=categoryService.getCategory();
        request.setAttribute("CategoryList",list);
        //请求转发
        request.getRequestDispatcher("/manage/product/add.jsp").forward(request, response);
        
        }
    
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    }

    product下的add.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/layui.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath }/manage/assets/css/view.css" />
    <title></title>
                    <div class="layui-form-item">
                            <label class="layui-form-label">所属分类</label>
                            <div class="layui-input-inline">
                                <select name="cid" lay-verify="required">
                                    <option value="">请选择</option>
                                    <c:forEach items="${CategoryList }" var="c">
                                    <option value="${c.cid }">${c.cname }</option>
                                    </c:forEach>
                                    
                                    
                                </select>
                            </div>

    修改返回按钮返回上一级:

        <script src="${pageContext.request.contextPath }/manage/assets/layui.all.js"></script>
        <script>
            var form = layui.form, layer = layui.layer;
            function go(){
                location.href="${pageContext.request.contextPath }/ListProductServlet";
            }
        </script>
  • 相关阅读:
    模板 无源汇上下界可行流 loj115
    ICPC2018JiaozuoE Resistors in Parallel 高精度 数论
    hdu 2255 奔小康赚大钱 最佳匹配 KM算法
    ICPC2018Beijing 现场赛D Frog and Portal 构造
    codeforce 1175E Minimal Segment Cover ST表 倍增思想
    ICPC2018Jiaozuo 现场赛H Can You Solve the Harder Problem? 后缀数组 树上差分 ST表 口胡题解
    luogu P1966 火柴排队 树状数组 逆序对 离散化
    luogu P1970 花匠 贪心
    luogu P1967 货车运输 最大生成树 倍增LCA
    luogu P1315 观光公交 贪心
  • 原文地址:https://www.cnblogs.com/maxuefeng/p/14016018.html
Copyright © 2011-2022 走看看