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 goodsList = goodsService.listAllGoods();
request.setAttribute(“goodsList”, goodsList);
request.getRequestDispatcher(“index.jsp”).forward(request, response);
}
private void genExcel() throws SQLException {
final List 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”%>
商品列表
<c:forEach items=“${goodsList}” var=“goods”>
${goods.id} ${goods.name} ${goods.price1} ${goods.price2} ${goods.amount}</c:forEach>
更多:Java进阶核心知识集
包含:JVM,JAVA集合,网络,JAVA多线程并发,JAVA基础,Spring原理,微服务,Zookeeper,Kafka,RabbitMQ
必看视频!获取2024年最新Java开发全套学习资料
,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存等等
高效学习视频
4f3f6ea879d59b2788.png)(title-必看视频!获取2024年最新Java开发全套学习资料)]
,Hbase,MongoDB,Cassandra,设计模式,负载均衡,数据库,一致性哈希,JAVA算法,数据结构,加密算法,分布式缓存等等
[外链图片转存中…(img-tSa32cmN-1720156135872)]