行测 中国移动 计算机视觉技术 UI Automator 测试用例 自承式光缆 服务注册中心 iis checkbox History.js 建筑资质 vue教程 vue响应式布局 郑州网络 jquery循环 jquery绑定change事件 bootstrap图表 oracle无效的列索引 excel太长的文字隐藏 sublime分屏快捷键 mysql汉化包 mysql自连接 matlab中如何定义函数 小程序下拉刷新样式 python高级教程 java成员变量 java获取时间 linux入门基础 图吧导航怎么样 打马赛克的软件 销售单打印软件 日历制作模板 图片放大软件 福昕阅读器绿色版 ps扭曲变形 电脑还原软件 winfax android下载文件 geartrax 优酷app播放器下载
当前位置: 首页 > 学习教程  > 编程语言

JSP+Servlet+mysql实现商品管理

2020/12/28 19:46:13 文章标签:

1.需求 (1)打开商品信息管理首页,以列表方式显示所有商品信息,页面列表要求如图-1所示。 图-1 (2)单击商品超链接,则跳转至指定商品的商品详情页面,如图-2所示。 图-2 &#xff0…

1.需求

(1)打开商品信息管理首页,以列表方式显示所有商品信息,页面列表要求如图-1所示。
在这里插入图片描述
图-1
(2)单击商品超链接,则跳转至指定商品的商品详情页面,如图-2所示。
在这里插入图片描述
图-2
(3)设计及实现新增商品功能,如图-3。
在这里插入图片描述
图-3
(4)设计及实现修改商品功能,如图-4。
修 改 商 品
在这里插入图片描述
图-4
(5)单击“删除”链接可以进行商品信息删除,弹出提示框,点击“确定”进行删除,如图-5所示。
在这里插入图片描述
图-5

2.结构

在这里插入图片描述
数据库
在这里插入图片描述

3.代码

3.1 JSP

common.jsp

<%--引入jstl--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--当前项目路径--%>
<c:set var="path" value="${pageContext.request.contextPath}"/>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jsp"%>
<html>
  <head>
    <title>商品管理</title>
  </head>
  <body>
  <a href="${path}/GoodsServlet?m=showAllGoods">点击前往商品管理</a>
  </body>
</html>

showallgoods.jsp


<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jsp"%>
<html>
<head>
    <title>商品管理</title>
</head>
<style>
    table tr{
        text-align: center;
    }
    #div1{
        width: 200px;
        height: 100px;
        border: 1px solid black;
        margin: 30px auto;
        text-align: center;
        display: none;
    }
    span{
        width: 40px;
        height: 25px;
        border: solid 1px black;
        margin: 20px;
    }
    span a{
        text-decoration: none;
    }
</style>
<body>

<table border="1px" cellpadding="0" cellspacing="0">
    <c:forEach items="${goodsList}" var="goods">
        <tr>
            <td width="200">${goods.goodsId}</td>
            <td width="200">${goods.personId}</td>
            <td width="200"><a href="${path}/GoodsServlet?m=showOneGoods&goodsId=${goods.goodsId}">${goods.goodsName}</a></td>
            <td width="200">${goods.goodsPrice}</td>
            <td width="200">${goods.goodsNum}</td>
            <td width="200">${goods.goodsTime}</td>
            <td width="200">
                <a href="${path}/GoodsServlet?m=showNowGoods&goodsId=${goods.goodsId}">修改</a>
                <a href="${path}/GoodsServlet?m=delGoods&goodsId=${goods.goodsId} ">删除</a>
<%--                onclick="show()--%>
            </td>
        </tr>
    </c:forEach>
</table>
<a href="${path}/add.jsp">添加商品</a>
<div id="div1" >
    <p>确定删除吗?</p>
    <span>
        <a href="${path}/GoodsServlet?m=delGoods&goodsId=${goods.goodsId}">确定</a>
    </span>
    <span>
        <a href="${path}/showallgoods.jsp">删除</a>
    </span>
</div>

<script>
    function show() {
        var div1 =document.getElementById("div1");
        div1.style.display="block";
    }
</script>
</body>
</html>

showonegoods.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jsp" %>
<html>
<head>
    <title>单个商品显示</title>
</head>
<body>

<table border="1px" cellpadding="0" cellspacing="0">
    <tr>
        <td>条形码</td>
        <td width="200">${goods.goodsId}</td>
    </tr>
    <tr>
        <td>主管工号</td>
        <td width="200">${goods.personId}</td>
    </tr>
    <tr>
        <td>商品名称</td>
        <td width="200">${goods.goodsName}</td>
    </tr>
    <tr>
        <td>商品价格</td>
        <td width="200">${goods.goodsPrice}</td>
    </tr>
    <tr>
        <td>进货单号</td>
        <td width="200">${goods.goodsNum}</td>
    </tr>
    <tr>
        <td>保质期</td>
        <td width="200">${goods.goodsTime}</td>
    </tr>
    <tr>
        <td colspan="2"><a href="${path}/GoodsServlet?m=showAllGoods">返回</a></td>
    </tr>
</table>

</body>
</html>

update.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="common.jsp" %>
<html>
<head>
    <title>修改界面</title>
</head>
<body>

<form action="${path}/GoodsServlet" method="post">
    <input type="hidden" name="m" value="updateGoods">
    条形码 <input type="text" name="goodsId" value="${goods.goodsId}"><br>
    主管工号
    <select name="personId" id="">
        <option value="201">201</option>
        <option value="202">202</option>
        <option value="203">203</option>
    </select><br>
    商品名称 <input type="text" name="goodsName" value="${goods.goodsName}"><br>
    商品价格 <input type="text" name="goodsPrice" value="${goods.goodsPrice}"><br>
    进货单号 <input type="text" name="goodsNum" value="${goods.goodsNum}"><br>
    保质期 <input type="text" name="goodsTime" value="${goods.goodsTime}"><br>
    <input type="submit" value="提交">
</form>

</body>
</html>

3.2 bean

Goods

package beans;

//商品
public class Goods {
    private int goodsId;//条形码
    private String personId;//主管工号
    private String goodsName;//商品名称
    private String goodsPrice;//商品价格
    private int goodsNum;//进货单号
    private String goodsTime;//保质期

    public Goods() {
    }

    public Goods(int goodsId, String personId, String goodsName, String goodsPrice, int goodsNum, String goodsTime) {
        this.goodsId = goodsId;
        this.personId = personId;
        this.goodsName = goodsName;
        this.goodsPrice = goodsPrice;
        this.goodsNum = goodsNum;
        this.goodsTime = goodsTime;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "goodsId=" + goodsId +
                ", personId='" + personId + '\'' +
                ", goodsName='" + goodsName + '\'' +
                ", goodsPrice='" + goodsPrice + '\'' +
                ", goodsNum=" + goodsNum +
                ", goodsTime='" + goodsTime + '\'' +
                '}';
    }

    public int getGoodsId() {
        return goodsId;
    }

    public void setGoodsId(int goodsId) {
        this.goodsId = goodsId;
    }

    public String getPersonId() {
        return personId;
    }

    public void setPersonId(String personId) {
        this.personId = personId;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName;
    }

    public String getGoodsPrice() {
        return goodsPrice;
    }

    public void setGoodsPrice(String goodsPrice) {
        this.goodsPrice = goodsPrice;
    }

    public int getGoodsNum() {
        return goodsNum;
    }

    public void setGoodsNum(int goodsNum) {
        this.goodsNum = goodsNum;
    }

    public String getGoodsTime() {
        return goodsTime;
    }

    public void setGoodsTime(String goodsTime) {
        this.goodsTime = goodsTime;
    }
}

3.3 dao

GoodsDao

package dao;

import beans.Goods;

import java.util.List;

public interface GoodsDao {
    //显示所有信息
    List<Goods> showAllGoods();
    //显示单个信息
    Goods showOneGoods(String goodsId);
    //新增
    boolean addGoods(Goods goods);
    //更新
    boolean updateGoods(Goods goods);
    //删除
    boolean delGoods(String goodsId);
}

GoodsDaoImpl

package dao.impl;

import beans.Goods;
import dao.GoodsDao;
import utils.DBUtils;

import java.util.List;

public class GoodsDaoImpl implements GoodsDao {
    /**
     * 返回所有信息
     * @return
     */
    @Override
    public List<Goods> showAllGoods() {
        String sql="select *from goods";
        return DBUtils.queryAllList(sql, Goods.class);
    }

    /**
     * 返回单个商品
     * @param goodsId
     * @return
     */
    @Override
    public Goods showOneGoods(String goodsId) {
        String sql="select *from goods where goods_id=?";
        return DBUtils.queryOneToBean(sql,Goods.class,Integer.parseInt(goodsId));
    }

    /**
     * 新增
     * @param goods
     * @return
     */
    @Override
    public boolean addGoods(Goods goods) {
        String sql="insert into goods values(?,?,?,?,?,?)";
        return DBUtils.update(sql,goods.getGoodsId(),goods.getPersonId(),goods.getGoodsName(),
                goods.getGoodsPrice(),goods.getGoodsNum(),goods.getGoodsTime())>0;
    }

    /**
     * 更新
     * @param goods
     * @return
     */
    @Override
    public boolean updateGoods(Goods goods) {
        String sql="update goods set goods_id=?,person_id=?,goods_name=?,goods_price=?,goods_num=?,goods_time=? where goods_id=?";
        return DBUtils.update(sql,goods.getGoodsId(),goods.getPersonId(),goods.getGoodsName(),
                goods.getGoodsPrice(),goods.getGoodsNum(),goods.getGoodsTime(),goods.getGoodsId())>0;
    }

    /**
     * 删除
     * @param goodsId
     * @return
     */
    @Override
    public boolean delGoods(String goodsId) {
        String sql="delete from goods where goods_id=?";
        return DBUtils.update(sql,Integer.parseInt(goodsId))>0;
    }
}

3.4 service

GoodsService

package service;

import beans.Goods;

import java.util.List;

public interface GoodsService {
    //显示所有信息
    List<Goods> showAllGoods();
    //显示单个信息
    Goods showOneGoods(String goodsId);
    //新增
    boolean addGoods(Goods goods);
    //更新
    boolean updateGoods(Goods goods);
    //删除
    boolean delGoods(String goodsId);
}

GoodsServiceImpl

package service.impl;

import beans.Goods;
import dao.GoodsDao;
import dao.impl.GoodsDaoImpl;
import service.GoodsService;

import java.util.List;

public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao=new GoodsDaoImpl();

    @Override//返回所有信息
    public List<Goods> showAllGoods() {
        return goodsDao.showAllGoods();
    }

    @Override//返回单个商品
    public Goods showOneGoods(String goodsId) {
        return goodsDao.showOneGoods(goodsId);
    }

    @Override//新增
    public boolean addGoods(Goods goods) {
        return goodsDao.addGoods(goods);
    }

    @Override//更细
    public boolean updateGoods(Goods goods) {
        return goodsDao.updateGoods(goods);
    }

    @Override//删除
    public boolean delGoods(String goodsId) {
        return goodsDao.delGoods(goodsId);
    }
}

3.5 servlet

BaseServlet

package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;

@WebServlet("/BaseServlet")
public class BaseServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            //设置字体
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        //获取传进的m的参数
        String m = req.getParameter("m");
        //获取当前对象的class方法
        Class clazz = this.getClass();
            System.out.println("反射拿到的class是"+clazz+";反射调用的方法是:"+m);
        //反射调用方法
            Method method = clazz.getDeclaredMethod(m, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this, req, resp);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //请求转发
    public void reDis(String url,HttpServletRequest req, HttpServletResponse resp){
        try {
            req.getRequestDispatcher(url).forward(req,resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //重定向
    public void sendRed(String url,HttpServletRequest req, HttpServletResponse resp){
        try {
            resp.sendRedirect(req.getContextPath()+url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

GoodsServlet

package servlet;

import beans.Goods;
import service.GoodsService;
import service.impl.GoodsServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/GoodsServlet")
public class GoodsServlet extends BaseServlet {
    //静态变脸
    private GoodsService goodsService=new GoodsServiceImpl();

    //返回所有信息
    public void showAllGoods(HttpServletRequest req, HttpServletResponse resp){
        List<Goods> goodsList = goodsService.showAllGoods();
        req.setAttribute("goodsList",goodsList);
        reDis("showallgoods.jsp",req,resp);
    }
    //返回单个商品信息
    public void showOneGoods(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        Goods goods = goodsService.showOneGoods(goodsId);
        req.setAttribute("goods",goods);
        reDis("showonegoods.jsp",req,resp);
    }
    //新增商品信息
    public void addGoods(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        int goodsId1 = Integer.parseInt(goodsId);
        String personId = req.getParameter("personId");
        String goodsName = req.getParameter("goodsName");
        String goodsPrice = req.getParameter("goodsPrice");
        String goodsNum = req.getParameter("goodsNum");
        int goodsNum1 = Integer.parseInt(goodsNum);
        String goodsTime = req.getParameter("goodsTime");
        Goods goods=new Goods(goodsId1,personId,goodsName,goodsPrice,goodsNum1,goodsTime);
        goodsService.addGoods(goods);
        showAllGoods(req,resp);
    }
    //返回单个商品信息
    public void showNowGoods(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        Goods goods = goodsService.showOneGoods(goodsId);
        System.out.println(goods);
        req.setAttribute("goods",goods);
        reDis("update.jsp",req,resp);
    }
    //修改商品信息
    public void updateGoods(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        int goodsId1 = Integer.parseInt(goodsId);
        String personId = req.getParameter("personId");
        String goodsName = req.getParameter("goodsName");
        String goodsPrice = req.getParameter("goodsPrice");
        String goodsNum = req.getParameter("goodsNum");
        int goodsNum1 = Integer.parseInt(goodsNum);
        String goodsTime = req.getParameter("goodsTime");
        Goods goods=new Goods(goodsId1,personId,goodsName,goodsPrice,goodsNum1,goodsTime);
        System.out.println(goods);
        goodsService.updateGoods(goods);
        showAllGoods(req,resp);
    }
    //删除商品信息
    public void delGoods(HttpServletRequest req, HttpServletResponse resp){
        String goodsId = req.getParameter("goodsId");
        goodsService.delGoods(goodsId);
        showAllGoods(req,resp);
    }

}

4.效果图

首页
在这里插入图片描述
所有信息
在这里插入图片描述
新增
在这里插入图片描述
修改
在这里插入图片描述
删除
在这里插入图片描述
在这里插入图片描述


本文链接: http://www.dtmao.cc/news_show_550375.shtml

附件下载

相关教程

    暂无相关的数据...

共有条评论 网友评论

验证码: 看不清楚?