4:基于html的数据展现#私藏项目实操分享#

2021年11月24日 阅读数:1
这篇文章主要向大家介绍4:基于html的数据展现#私藏项目实操分享#,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

Servlet层

@WebServlet(urlPatterns = "/goods")
public class GoodsServlet extends HttpServlet {
    private static final long serialVersionUID = 4116681024313896922L;

    private GoodsService goodsService = new GoodsServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        final String op = request.getParameter("op");
        switch (op) {
            case "findAllGoods":
                try {
                    findAllGoods(request, response);
                } catch (SQLException | IOException e) {
                    e.printStackTrace();
                }
                break;
            case "getGoodsById":
                try {
                    getGoodsById(request, response);
                } catch (SQLException | IOException e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }

    }

    private void getGoodsById(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        final String idStr = request.getParameter("id");
        if (idStr == null) {
            throw new RuntimeException("待查询的商品的id不能为空");
        }
        Integer id = Integer.parseInt(idStr);
        final Goods goods = goodsService.getGoodsById(id);
        request.setAttribute("goods", goods);
        request.getRequestDispatcher("goods.jsp").forward(request, response);
    }

    private void findAllGoods(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException {
        final List<Goods> goodsList = goodsService.listAllGoods();
        request.setAttribute("goodsList", goodsList);
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    private void genExcel() throws SQLException {
        final List<Goods> arrayList = goodsService.listAllGoods();

        String fileName = "D:/demo" + System.currentTimeMillis() + ".xlsx";
        EasyExcel.write(fileName, Goods.class).sheet("商品信息").doWrite(arrayList);
    }
}

前端代码

  • index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
    <title>首页</title>
</head>
<body>
<h2>商品列表</h2>
<table>
    <tr>
        <th>编号</th>
        <th>名称</th>
        <th>市场价格</th>
        <th>会员价格</th>
        <th>库存数量</th>
        <th>操做</th>
    </tr>
<c:forEach items="${goodsList}" var="goods">
    <tr>
        <td>${goods.id}</td>
        <td>${goods.name}</td>
        <td>${goods.price1}</td>
        <td>${goods.price2}</td>
        <td>${goods.amount}</td>
        <td>
            <a rel="nofollow" href="goods?op=getGoodsById&id=${goods.id}">详情</a>
        </td>
    </tr>
</c:forEach>
</table>
</body>
</html>
  • goods.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品详情</title>
</head>
<body>
    编号:${goods.id} <br>
    名称:${goods.name} <br>
    市场价格:${goods.price1} <br>
    会员价格:${goods.price2} <br>
    库存:${goods.amount}
</body>
</html>

效果

请添加图片描述