===新手入门指南===
新建goods包,并在goods包下创建model、repository、service、controller四个包,在/WEB-INF/views下创建goods文件夹
在/WEB-INF/views/goods下新建index.jsp,在GoodsController中添加进入index页面的方法,在GoodsService下添加列出所有商品的方法
GoodsController:
@Controller @RequestMapping("/goods") public class GoodsController { @Autowired//自动注入goodsService GoodsService goodsService; @Autowired GoodsRepository goodsRepository; @RequestMapping("/index") public void index(Model model,String number) { List<Goods> goodsList = goodsService.findAllGoods(); model.addAttribute("goodsList",goodsList); } }GoodsService:
@Service public class GoodsService { @Autowired//自动注入goodsRepository GoodsRepository goodsRepository; public List<Goods> findAllGoods() { return goodsRepository.findAll(); } }index.jsp:(table1位table,tbody1位tbody,thead1为thead,tr1为tr,th1为th,td1为td)
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> <html> <head> <title>商品列表</title> </head> <body> 商品列表 <a href="creategoods">添加商品</a> <table1> <thead1> <th1>商品编号</th1> <th1>商品名称</th1> <th1>商品价格</th1> <th1>商品数量</th1> <th1>上架时间</th1> <th1>操作</th> </thead1> <tbody1> <c:forEach items="${goodsList}" var="goods"> <tr1> <td1>${goods.number}</td1> <td1>${goods.name}</td1> <td1>${goods.price}</td1> <td1>${goods.total}</td1> <td1><fmt:formatDate value="${goods.onSaleTime}" pattern="yyyy-MM-dd HH:mm:ss"></fmt:formatDate> </td1> <td1><a href="updategoods?goodsId=${goods.id}">编辑</a><a href="deletegoods?goodsId=${goods.id}">删除</a></td1> </tr1> </c:forEach> </tbody1> </table1> </body> </html>在/WEB-INF/views/goods下新建creategoods.jsp,GoodsController下新建进入creategoods页面的方法,以及接收提交数据并保存商品信息的方法,在GoodsService中添加保存商品的方法
creategoods.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加商品</title> </head> <body> <form action="creategoodssubmit" method="post"> <table1> <tr1><th1>商品编号</th1><td1><input name="number" type="text"></td1></tr1> <tr1><th1>商品名称</th1><td1><input name="name" type="text"></td1></tr1> <tr1><th1>商品价格</th1><td1><input name="price" type="number"></td1></tr1> <tr1><th1>商品数量</th1><td1><input name="total" type="number"></td1></tr1> <tr1><th1>上架时间</th1><td1><input name="onSaleTime" type="datetime"></td1></tr1> </table1> <button type="submit">提交</button> </form> </body> </html>GoodsService:
public Goods saveGoods(Goods goods) { if (goods.getId() == null) { return goodsRepository.saveAndFlush(goods); } else { Goods one = goodsRepository.findOne(goods.getId()); BeanUtils.copyProperties(goods,one,"id");//实际应该将goods实例中的非空属性copy至one实例 goodsRepository.saveAndFlush(one); return one; } }GoodsController:
@RequestMapping("/creategoods") public String createGoods() { return "/goods/creategoods"; } @RequestMapping("/creategoodssubmit") public String createGoodsSubmit(Goods goods) { goodsService.saveGoods(goods); return "redirect:/goods/index"; }在/WEB-INF/views/goods下新建updategoods.jsp,GoodsController下新建进入updategoods页面的方法,以及接收提交数据并保存商品信息的方法 updategoods.jsp:
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>添加商品</title> </head> <body> <form action="creategoodssubmit" method="post"> <table1> <tr1><th1>商品编号</th1><td1><input name="number" type="text" value="${goods.number}></td1></tr1> <tr1><th1>商品名称</th1><td1><input name="name" type="text" value="${goods.name}></td1></tr1> <tr1><th1>商品价格</th1><td1><input name="price" type="number" value="${goods.price}></td1></tr1> <tr1><th1>商品数量</th1><td1><input name="total" type="number" value="${goods.total}></td1></tr1> <tr1><th1>上架时间</th1><td1><input name="onSaleTime" type="datetime" value="<fmt:formatDate value='${goods.onSaleTime}' pattern='yyyy-MM-dd HH:mm:ss'></fmt:formatDate>"></td1></tr1> </table1> <button type="submit">提交</button> </form> </body> </html>GoodsController:
@RequestMapping("/updategoods") public String updateGoods(Integer goodsId,Model model) { model.addAttribute(goodsRepository.findOne(goodsId)); return "/goods/updategoods"; } @RequestMapping("/updategoodssubmit") public String updateGoodsSubmit(Goods goods) { goodsService.saveGoods(goods); return "redirect:/goods/index"; }在GoodsController中添加删除商品的方法
@RequestMapping("/deletegoods") public String deleteGoods(Integer goodsId) { goodsRepository.delete(goodsId); return "redirect:/goods/index"; }在index.jsp中添加检索商品信息的表单,在GoodsRepository中添加通过编号检索商品的查询方法,修改GoodsController中进入商品详情界面的方法
index.jsp
<form action="index" method="post"> 商品编号:<input type="text" name="number" value="${number}"> <button type="submit">查询</button> </form>GoodsRepository:
public List<Goods> findByNumber(String number);GoodsController:
@RequestMapping("/index") public void index(Model model,String number) { List<Goods> goodsList = new ArrayList<Goods>(); if (StringUtils.isNotBlank(number)) { goodsList = goodsRepository.findByNumber(number); } else { goodsList = goodsService.findAllGoods(); } model.addAttribute("goodsList",goodsList); model.addAttribute("number",number); }