水果库存系统

首先配置TomCat访问index页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/index.css">
    <script language="JavaScript" src="js/index.js"></script>
</head>
<body>
<div id="div_container" th:method="get">
    <div id="div_fruit_list">
        <p class="center">欢迎使用水果库存系统</p>
        <div style="width: 60%;margin-left: 20%;text-align: right;">
            <form th:action="@{/index}" method="post" style="float: left;">
                <input type="hidden" name="oper" value="search"/>
                请输入查询关键字:<input type="text" name="keyword" th:value="${session.keywork}"/>
                <input type="submit" value="查询" class="btn"/>
            </form>
            <a th:href="@{/add.html}" style="margin-bottom: 4px">添加库存记录</a>
        </div>
        <table id="tbl_fruit">
            <tr>
                <th class="w20">名称</th>
                <th class="w20">单价</th>
                <th class="w20">库存</th>
                <th>操作</th>
            </tr>
            <!--thy判断session作用域是否为空-->
            <tr th:if="${#lists.isEmpty(session.fruit)}">
                <td colspan="4">对不起,库存为空!</td>
            </tr>
            <!--不为空,则循环取出作用域中的数据-->
            <tr th:if="${not#lists.isEmpty(session.fruit)}" th:each="f:${session.fruit}">
                <!--设置文本框内容为session作用域中水果的名字代替原来的内容-->
                <!--根据水果的id设置超链接的跳转目标页面-->
                <td><a none th:text="${f.name}"  th:href="@{/edit.do(id=${f.id})}">苹果</a></td>
                <td th:text="${f.price}">5</td>
                <td th:text="${f.count}">20</td>
                <!--点击图片响应删除操作|xxxx|用于拼接||内的字符串如果遇到$()则会执行$()语句-->
                <td><img src="imgs/del.jpg" class="delImg" th:onclick="|delFruit(${f.id})|" /></td>
            </tr>
        </table>
        <div style="width: 60%;margin-left: 20%;padding-top: 5px;" class="center">
            <input type="button" value="首  页" class="btn" th:onclick="|page(1)|" th:disabled="${session.pageNo==1}"/>
            <input type="button" value="上一页" class="btn" th:onclick="|page(${session.pageNo-1})|" th:disabled="${session.pageNo==1}"/>
            <input type="button" value="下一页" class="btn" th:onclick="|page(${session.pageNo+1})|" th:disabled="${session.pageNo==session.count}"/>
            <input type="button" value="尾  页" class="btn" th:onclick="|page(${session.count})|" th:disabled="${session.pageNo==session.count}"/>
        </div>
    </div>
</div>
</body>
</html>

通过index页面映射到FruitService中处理get请求(如果未指定请求方式则为doget请求)

package JavaWeb.Service;

import JavaWeb.Tool.Utils;
import JavaWeb.dao.BaseServlet;
import JavaWeb.dao.FruitDAO;
import JavaWeb.daomain.Fruit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

/**
 * @author whlie(true){learn}
 */
//从Servlet3.0开始支持注解的注册
@WebServlet("/index")
public class FruitService extends BaseServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        HttpSession session = req.getSession();
        int no=1;

        String oper = req.getParameter("oper");
        //如果oper!=null说明是通过表单的查询按钮点击过来的
        //如果oper是空的,说明不是通过表单的查询按钮点击过来的
        String keyword=null;
        if (!Utils.isNull(oper)&&"search".equals(oper)){
            no=1;
            keyword = req.getParameter("keyword");
            //设置默认第一次查询的情况
            if(Utils.isNull(keyword)){
                keyword = "" ;
            }
            session.setAttribute("keyword",keyword);
        }else {
            String pageNo = req.getParameter("pageNo");
            if (!Utils.isNull(pageNo)){
                no=Integer.parseInt(pageNo);
            }
            Object keyword1 = session.getAttribute("keyword");
            //设置默认第一次查询的情况
            if(keyword1!=null){
                keyword = (String)keyword1 ;
            }else{
                keyword = "" ;
            }
        }

        FruitDAO fruitDAO = new FruitDAO();
        List<Fruit> fruit1 = null;
        //获取水果总数
        try {
            //获取指定名字和备注的水果集合
            fruit1 = fruitDAO.getFruit(keyword,no);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //得到指定名字和备注的水果的数量
        int count = fruit1.size();
        //获取页数
        String pageNo = req.getParameter("pageNo");
        if (!Utils.isNull(pageNo)){
            no=Integer.parseInt(pageNo);
        }
        try {
            //分页查询
            List<Fruit> fruit = fruitDAO.getFruitC(keyword,no);
            //key:fruit,value:fruit
            session.setAttribute("fruit",fruit);
            //保存页数
            session.setAttribute("pageNo",no);
            //保存数量
            session.setAttribute("count",(count+4)/5);
            //设置跳转页面
            super.processTemplate("index",req,resp);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

上述实现了水果系统的显示功能

修改

如果点击了超链接则会跳转到EditSer中根据你点击的水果名称获取对应的水果id

package JavaWeb.Service;

import JavaWeb.Tool.Utils;
import JavaWeb.dao.BaseServlet;
import JavaWeb.dao.FruitDAO;
import JavaWeb.daomain.Fruit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @author whlie(true){learn}
 */
@WebServlet("/edit.do")
public class EditSer extends BaseServlet {
    private FruitDAO fruitDAO = new FruitDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (!Utils.isNull(id)){
            int i = Integer.parseInt(id);
            try {
                Fruit fruitId = fruitDAO.getFruitId(i);
                req.setAttribute("fruitId",fruitId);
                super.processTemplate("edit",req,resp);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}

然后跳转到edit页面(修改页面),根据水果id获取原先的信息显示在文本框中

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/edit.css">
</head>
<body>
<div id="div_container" >
    <div id="div_fruit_list">
        <p class="center f30">编辑库存信息</p>
        <form th:action="@{/update.do}" method="post" th:object="${fruit}">
            <!--隐藏域:功能类似于文本框,它的值会随表单的发送也发送给服务器,但界面上不显示-->
            <input type="hidden" name="id" th:value="*{id}" />
            <table id="tbl_fruit">
                <tr>
                    <th class="w20">名称:</th>
                    <td><input type="text" name="fname" th:value="*{name}"/></td>
                </tr>
                <tr>
                    <th class="w20">单价:</th>
                    <td><input type="text" name="price" th:value="*{price}"/></td>
                </tr>
                <tr>
                    <th class="w20">库存:</th>
                    <td><input type="text" name="count" th:value="*{count}"/></td>
                </tr>
                <tr>
                    <th class="w20">备注:</th>
                    <td><input type="text" name="remark" th:value="*{remark}"/></td>
                </tr>
                <tr>
                    <th colspan="2">
                        <input type="submit" value="修改"/>
                    </th>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>

点击提交按钮后,会发出post请求进入UpdateSer

package JavaWeb.Service;

import JavaWeb.dao.BaseServlet;
import JavaWeb.dao.FruitDAO;
import JavaWeb.daomain.Fruit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author whlie(true){learn}
 */
@WebServlet("/update.do")
public class UpdateSer extends BaseServlet {
    private FruitDAO fruitDAO = new FruitDAO();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        String fname = req.getParameter("fname");
        String price = req.getParameter("price");
        String count = req.getParameter("count");
        String remark = req.getParameter("remark");
        fruitDAO.updateId(new Fruit(Integer.parseInt(id),fname,Double.parseDouble(price),
                Integer.parseInt(count),remark));
        resp.sendRedirect("index");
    }
}

在UpdateSer中获取文本中的值替换原来数据库中的值后,进行重定向,从index中获取更新后的值显示在页面上

删除

当在index页面点击删除图标时会调用js方法发出确认弹窗,如果确认删除则会映射到DelSer中

package JavaWeb.Service;

import JavaWeb.Tool.Utils;
import JavaWeb.dao.BaseServlet;
import JavaWeb.dao.FruitDAO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author whlie(true){learn}
 */
@WebServlet("/del.do")
public class DelSer extends BaseServlet {
    private FruitDAO fruitDAO = new FruitDAO();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        if (!Utils.isNull(id)){
            int i = Integer.parseInt(id);
            fruitDAO.delId(i);
            resp.sendRedirect("index");
        }
    }
}

执行完删除操作后会重新定向到index中

添加

在index页面中点击添加库存记录后进入add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="css/edit.css">
</head>
<body>
<div id="div_container" >
    <div id="div_fruit_list">
        <p class="center f30">新增库存信息</p>
        <form action="add.do" method="post">
            <table id="tbl_fruit">
                <tr>
                    <th class="w20">名称:</th>
                    <td><input type="text" name="fname" /></td>
                </tr>
                <tr>
                    <th class="w20">单价:</th>
                    <td><input type="text" name="price" /></td>
                </tr>
                <tr>
                    <th class="w20">库存:</th>
                    <td><input type="text" name="count" /></td>
                </tr>
                <tr>
                    <th class="w20">备注:</th>
                    <td><input type="text" name="remark" /></td>
                </tr>
                <tr>
                    <th colspan="2">
                        <input type="submit" value="添加"/>
                    </th>
                </tr>
            </table>
        </form>
    </div>
</div>
</body>
</html>

点击提交按钮后跳转到addSer中

package JavaWeb.Service;

import JavaWeb.dao.BaseServlet;
import JavaWeb.dao.FruitDAO;
import JavaWeb.daomain.Fruit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author whlie(true){learn}
 */
@WebServlet("/add.do")
public class addSer extends BaseServlet {
    private FruitDAO fruitDAO = new FruitDAO();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String name = req.getParameter("fname");
        double price = Double.parseDouble(req.getParameter("price"));
        int count = Integer.parseInt(req.getParameter("count"));
        String remark = req.getParameter("remark");
        fruitDAO.add(new Fruit(name,price,count,remark));
        resp.sendRedirect("index");
    }
}

获取到文本框中的信息后加入数据库,并重定向到index中

分页

在第一页的时候首页和上一页不可用,在最后一页的时候下一页和尾页不可用

获取到数据库中的(所有水果个数+4)/5得到相应的页码

单击对应的按钮,对应页码增加或减少

通过数据库的limit方法实现数据库的分页显示::select *from fruit limit ?,5

根据关键字进行查找

当在index页面中的文本框中输入查找的关键字并点击查找按钮时,会映射到FruitSer中进行处理

首先接收文本框中的值,如果为空说明是第一次查询或者查询全部,如果不为空则则根据文本框中的值在数据库中进行查找名字或者备注符合的水果

通过FruitDAO中的getFruit方法得到总数,然后根据getFruitC方法进行分页显示

package JavaWeb.dao;

import JavaWeb.daomain.Fruit;

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

/**
 * @author whlie(true){learn}
 */
public class FruitDAO extends BasicDAO<Fruit> {
    /**
     * 查询表中的所有数据
     */
    public List<Fruit> getFruit(String keyword,int no) throws SQLException {
        return super.queryMulti("select *from fruit where name like ? or remark like ?",Fruit.class,"%"+keyword+"%","%"+keyword+"%");
    }

    /**
     * 查询表中的所有数据(分页查询)
     */
    public List<Fruit> getFruitC(String keyword,int no) throws SQLException {
        return super.queryMulti("select *from fruit where name like ? or remark like ? limit ?,5"
                ,Fruit.class,"%"+keyword+"%","%"+keyword+"%",(no-1)*5);
    }

    /**
     * 根据id得到对应的水果
     */
    public Fruit getFruitId(int id) throws SQLException {
        return super.querySingle("select *from fruit where id=?",Fruit.class,id);
    }

    /**
     * 修改指定水果的信息
     */
    public void updateId(Fruit fruit){
        String sql="update fruit set name=?,price=?,count=?,remark=? where id=?";
        try {
            super.update(sql,fruit.getName(),fruit.getPrice(),fruit.getCount(),fruit.getRemark(),fruit.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 删除指定id的水果
     */
    public void delId(int id) {
        try {
            super.update("delete from fruit where id=?",id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加水果库存
     */
    public void add(Fruit fruit){
        String sql="insert into fruit values(null,?,?,?,?)";
        try {
            super.update(sql,fruit.getName(),fruit.getPrice(),fruit.getCount(),fruit.getRemark());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1while(true){learn}

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值