web-demo01

准备工作:Demo01准备工作_梧桐696的博客-CSDN博客

AIM 01

1. GETALL

@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        FruitDAO fruitDAO = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDAO.getAll();
        HttpSession session = request.getSession();
        session.setAttribute("fruitList",fruitList);
        super.processTemplate("index",request,response);

    }

}
<table id="tbl_fruit">
        			<tr>
						<th class="w20">名称</th>
						<th class="w20">单价</th>
						<th class="w20">库存</th>
						<th>操作</th>
					</tr>
					<tr th:if="${#lists.isEmpty(session.fruitList)}">
						<td colspan="4">对不起!库存为空!!</td>
					</tr>
					<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
						<td th:text="${fruit.fname}"></td>
						<td th:text="${fruit.price}"></td>
						<td th:text="${fruit.fcount}"></td>
						<td><img src="imgs/del.jpg" class="delImg"/></td>
					</tr>
</table>

2. SHOWDETAILS

<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
	<td><a th:text="${fruit.fname}" th:href="@{/edit.do(fid=${fruit.fid})}"></a></td>
	<td th:text="${fruit.price}"></td>
	<td th:text="${fruit.fcount}"></td>
	<td><img src="imgs/del.jpg" class="delImg"/></td>
</tr>
package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtils;

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

/**
 * @author crystal
 * @create 2022-11-14 15:39
 */
@WebServlet("/edit.do")
public class EditServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fidStr = request.getParameter("fid");
        if(StringUtils.isNotEmpty(fidStr)){
            int fid = Integer.parseInt(fidStr);
            Fruit fruit = fruitDAO.getFruitById(fid);
            request.setAttribute("fruit",fruit);
            super.processTemplate("edit",request,response);
        }

    }
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>编辑信息</title>
    <style>
        *{
            padding-left: 8%;
        }
    </style>
</head>
<body>
    <form action="#" method="post" th:object="${fruit}">
        <table style="width: 500px">
            <tr>
                <td colspan="2" style="padding-left: 30%"><h1>编辑库存信息</h1></td>
            </tr>
            <tr>
                <td ></td>
                <td>名称:<input type="text" name="fname" th:value="*{fname}"></td>
            </tr>
            <tr>
                <td></td>
                <td>价格:<input type="text" name="price" th:value="*{price}"></td>
            </tr>
            <tr>
                <td ></td>
                <td>库存:<input type="text" name="fcount" th:value="*{fcount}"></td>
            </tr>
            <tr>
                <td ></td>
                <td>备注:<input type="text" name="remark" th:value="*{remark}"></td>
            </tr>
            <tr>
                <td style="padding-left: 50%"><input type="submit" value="修改"></td>
            </tr>
        </table>
    </form>
</body>
</html>

3.UPDATE

<body>
    <form th:action="@{/update.do}" method="post" th:object="${fruit}">
        <table style="width: 500px">
            <tr>
                <td colspan="2" style="padding-left: 30%"><h1>编辑库存信息</h1></td>
            </tr>
            <tr>
                <td>
                    <input type="hidden" name="fid" th:value="*{fid}">
                </td>
            </tr>
            <tr>
                <td ></td>
                <td>名称:<input type="text" name="fname" th:value="*{fname}"></td>
            </tr>
package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;

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

/**
 * @author crystal
 * @create 2022-11-14 17:16
 */
@WebServlet("/update.do")
public class UpdateServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fidStr = request.getParameter("fid");
        int fid = Integer.parseInt(fidStr);
        String fname = request.getParameter("fname");
        String priceStr = request.getParameter("price");
        int price = Integer.parseInt(priceStr);
        String fcountStr = request.getParameter("fcount");
        int fcount = Integer.parseInt(fcountStr);
        String remark = request.getParameter("remark");
        Fruit fruit = new Fruit(fid,fname,price,fcount,remark);
        Boolean isUpdate = fruitDAO.updateFruit(fruit);
        response.sendRedirect("index");
    }
}

4. DELETE

<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
<td><a th:text="${fruit.fname}" th:href="@{/edit.do(fid=${fruit.fid})}"></a></td>
<td th:text="${fruit.price}"></td>
<td th:text="${fruit.fcount}"></td>
<td><img src="imgs/del.jpg" class="delImg" th:onclick="|delFruit($(fruit.fid))|"/></td>
</tr>
<script src="js/index.js"></script>
function delFruit(fid){
    if(confirm('确认删除吗?')){
        window.location.href('del.do?fid='+fid);
    }
}
package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.myssm.springmvc.ViewBaseServlet;

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

/**
 * @author crystal
 * @create 2022-11-14 17:46
 */
@WebServlet("/del.do")
public class DelServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String fidStr = request.getParameter("fid");
        int fid = Integer.parseInt(fidStr);
        Boolean isDel = fruitDAO.delFruit(fid);
        response.sendRedirect("index");
    }
}

5. INSERT

<div id="div_fruit_list">
<p class="center f30">欢迎使用食品库存后台管理系统</p>
<div>
	<a th:href="@{add.html}">添加库存信息</a>
</div>
<table id="tbl_fruit">
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/insert.do}" method="post">
    <table style="width: 500px">
        <tr>
            <td colspan="2" style="padding-left: 30%"><h1>添加库存信息</h1></td>
        </tr>
        <tr>
            <td>名称:<input type="text" name="fname"></td>
        </tr>
        <tr>
            <td>价格:<input type="text" name="price"></td>
        </tr>
        <tr>
            <td>库存:<input type="text" name="fcount"></td>
        </tr>
        <tr>
            <td>备注:<input type="text" name="remark"></td>
        </tr>
        <tr>
            <td><input type="submit" value="添加"></td>
        </tr>
    </table>
</form>
</body>
</html>
package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;

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

/**
 * @author crystal
 * @create 2022-11-14 18:39
 */
@WebServlet("/insert.do")
public class InsertServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String fname = request.getParameter("fname");
        String priceStr = request.getParameter("price");
        int price = Integer.parseInt(priceStr);
        String fcountStr = request.getParameter("fcount");
        int fcount = Integer.parseInt(fcountStr);
        String remark = request.getParameter("remark");
        Fruit fruit = new Fruit(null,fname,price,fcount,remark);
        Boolean isInsert = fruitDAO.insertFruit(fruit);
        response.sendRedirect("index");
    }
}

6. PAGINATION

DAO层
List<Fruit> getAll(Integer pageNo);
Impl层
 @Override
    public List<Fruit> getAll(Integer pageNo) {
        String sql = "select * from t_fruit limit ?,5";
        List<Fruit> fruitList = queryList(sql,(pageNo - 1)* 5);
        return fruitList;
    }

DAO层
Integer getCount();
Impl层
@Override
    public Integer getCount() {
        String sql = "select count(*) from t_fruit";
        return ((Long)getValue(sql)).intValue();
    }
package com.atguigu.fruit.serlvets;

import com.atguigu.fruit.dao.FruitDAO;
import com.atguigu.fruit.impl.FruitDAOImpl;
import com.atguigu.fruit.pojo.Fruit;
import com.atguigu.myssm.springmvc.ViewBaseServlet;
import com.atguigu.myssm.util.StringUtils;


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 javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @author crystal
 * @create 2022-11-10 8:54
 */
@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
    private FruitDAO fruitDAO = new FruitDAOImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNo = 1;
        String pageNoStr = request.getParameter("pageNo");
        if(StringUtils.isNotEmpty(pageNoStr)){
            pageNo = Integer.parseInt(pageNoStr);
            HttpSession session = request.getSession();
            session.setAttribute("pageNo",pageNo);

//            查询总记录条数
            Integer count = fruitDAO.getCount();
            int pageCount = (count + 5 - 1) / 5;
            session.setAttribute("pageCount",pageCount);

            List<Fruit> fruitList = fruitDAO.getAll(pageNo);
            session.setAttribute("fruitList",fruitList);
            super.processTemplate("index",request,response);
        }
    }


}
		<td><img src="imgs/del.jpg" class="delImg" th:onclick="|delFruit($(fruit.fid))|"/></td>
					</tr>
				</table>
				<div>
					<input type="button" value="首 页" th:onclick="|page(1)|" th:disabled="${session.pageNo==1}"/>
					<input type="button" value="上一页" th:onclick="|page(${session.pageNo - 1})|" th:disabled="${session.pageNo==1}"/>
					<input type="button" value="下一页" th:onclick="|page(${session.pageNo + 1})|" th:disabled="${session.pageNo==session.pageCount}"/>
					<input type="button" value="尾 页" th:onclick="|page(${session.pageCount})|" th:disabled="${session.pageNo==session.pageCount}"/>
				</div>
function page(pageNo){
    window.location.href('index.do?pageNo='+pageNo);
}

7.KEYWORD

<div style="width: 60%;margin-left: 20%;text-align: right" >
<form th:action="@{index}" method="post">
<input type="hidden" name="oper" value="search">
请输入查询关键字:<input type="text" name="keyword" th:value="${session.keyword}"/>
<input type="submit" value="查询" class="btn">
</form>
<a th:href="@{add.html}" style="margin-bottom: 5px">添加库存信息</a>
@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String oper = request.getParameter("oper");
        Integer pageNo = 1;
        String keyword = null;
        HttpSession session = request.getSession();
        if(StringUtil.isNotEmpty(oper) && "oper".equals("search")){
            pageNo = 1;
            keyword = request.getParameter("keyword");
            if(StringUtil.isEmpty(keyword)){
                keyword = "";
            }
            session.setAttribute("keyword",keyword);
        }else{
            String pageNoStr = request.getParameter("pageNo");
            if(StringUtil.isNotEmpty(pageNoStr)){
                pageNo = Integer.parseInt(pageNoStr);
            }
            Object keywordObj = session.getAttribute("keyword");
            if(keywordObj != null){
                keyword = (String) keywordObj;
            }else{
                keyword = "";
            }
        }
        session.setAttribute("pageNo",pageNo);
        FruitDAO fruitDAO = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDAO.getAll(keyword , pageNo);
        session.setAttribute("fruitList",fruitList);

        //总记录条数
        int fruitCount = fruitDAO.getFruitCount(keyword);
        //总页数
        int pageCount = (fruitCount+5-1)/5 ;
        session.setAttribute("pageCount",pageCount);
        super.processTemplate("index",request,response);
    }
dao
Integer getFruitCount(String keyword);
List<Fruit> getAll(String keyword,Integer pageNo);
impl
@Override
    public List<Fruit> getAll(String keyword,Integer pageNo) {
        String sql = "select * from t_fruit where fname like ? or remark like ? limit ?,2";
        List<Fruit> fruitList = queryList(sql,"%"+keyword+"%","%"+keyword+"%",(pageNo - 1) * 5);
        return fruitList;
    }

@Override
    public Integer getFruitCount(String keyword) {
        String sql = "select count(*) from t_fruit where fname = ? or remark = ?";
        Object value = getValue(sql,"%"+keyword+"%","%"+keyword+"%");
        return ((Long) value).intValue();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值