public String update(HttpServletRequest req, HttpServletResponse resp) throws FileNotFoundException, IOException { Product p = (Product) RequestUtil.setParam(Product.class, req); RequestUtil.Validate(Product.class, req); String img = req.getParameter("img"); System.out.println(img + "----------------------"); int cid = 0; try { cid = Integer.parseInt(req.getParameter("cid")); } catch (NumberFormatException e) { } if (cid == 0) { this.getErrors().put("cid", "商品类别必须选择"); } Product tp = productDao.load(Integer.parseInt(req.getParameter("id"))); tp.setIntro(p.getIntro()); tp.setName(p.getName()); tp.setPrice(p.getPrice()); tp.setStock(p.getStock()); boolean updateImg = false; if (img == null || img.trim().equals("")) { // 此时说明不修改图片 } else { // 此时说明需要修改图片 if (!this.hasErrors()) { // 是否要修改文件 byte[] fs = (byte[]) req.getAttribute("fs"); String fname = req.getParameter("img"); fname = FilenameUtils.getName(fname); RequestUtil.uploadFile(fname, "img", fs, req); } updateImg = true; } if (this.hasErrors()) { req.setAttribute("p", tp); req.setAttribute("cs", categoryDao.list()); return "product/updateInput.jsp"; } if (updateImg) { // 先删除原有的图片 String oimg = tp.getImg(); File f = new File(SystemContext.getRealpath() + "/img/" + oimg); f.delete(); tp.setImg(p.getImg()); } productDao.update(tp, cid); return redirPath + "ProductServlet?method=list"; }
public String addInput(HttpServletRequest req, HttpServletResponse resp) { req.setAttribute("cs", categoryDao.list()); return "product/addInput.jsp"; }
public String updateInput(HttpServletRequest req, HttpServletResponse resp) { req.setAttribute("cs", categoryDao.list()); req.setAttribute("p", productDao.load(Integer.parseInt(req.getParameter("id")))); return "product/updateInput.jsp"; }