Ejemplo n.º 1
0
 public String add(HttpServletRequest req, HttpServletResponse resp)
     throws FileNotFoundException, IOException {
   Product p = (Product) RequestUtil.setParam(Product.class, req);
   p.setStatus(1);
   RequestUtil.Validate(Product.class, req);
   int cid = 0;
   try {
     cid = Integer.parseInt(req.getParameter("cid"));
   } catch (NumberFormatException e) {
   }
   if (cid == 0) {
     this.getErrors().put("cid", "商品类别必须选择");
   }
   if (!this.hasErrors()) {
     // 文件上传
     byte[] fs = (byte[]) req.getAttribute("fs");
     String fname = req.getParameter("img");
     fname = FilenameUtils.getName(fname);
     RequestUtil.uploadFile(fname, "img", fs, req);
   }
   if (this.hasErrors()) {
     addInput(req, resp);
     return "product/addInput.jsp";
   }
   productDao.add(p, cid);
   return redirPath + "ProductServlet?method=list";
 }
Ejemplo n.º 2
0
 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";
 }