diff --git a/src/main/java/com/how2java/tmall/controller/CategoryController.java b/src/main/java/com/how2java/tmall/controller/CategoryController.java new file mode 100644 index 0000000..20b8dd4 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/CategoryController.java @@ -0,0 +1,88 @@ + + +package com.how2java.tmall.controller; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.how2java.tmall.pojo.Category; +import com.how2java.tmall.service.CategoryService; +import com.how2java.tmall.util.ImageUtil; +import com.how2java.tmall.util.Page; +import com.how2java.tmall.util.UploadedImageFile; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.multipart.MultipartFile; + +import javax.imageio.ImageIO; +import javax.servlet.http.HttpSession; +import java.awt.image.BufferedImage; +import java.io.File; +import java.io.IOException; +import java.util.List; + +@Controller +@RequestMapping("") +public class CategoryController { + @Autowired + CategoryService categoryService; + + @RequestMapping("admin_category_list") + public String list(Model model,Page page){ + PageHelper.offsetPage(page.getStart(),page.getCount()); + List cs= categoryService.list(); + int total = (int) new PageInfo<>(cs).getTotal(); + page.setTotal(total); + model.addAttribute("cs", cs); + model.addAttribute("page", page); + return "admin/listCategory"; + } + + @RequestMapping("admin_category_add") + public String add(Category c, HttpSession session, UploadedImageFile uploadedImageFile) throws IOException { + categoryService.add(c); + File imageFolder= new File(session.getServletContext().getRealPath("img/category")); + File file = new File(imageFolder,c.getId()+".jpg"); + if(!file.getParentFile().exists()) + file.getParentFile().mkdirs(); + uploadedImageFile.getImage().transferTo(file); + BufferedImage img = ImageUtil.change2jpg(file); + ImageIO.write(img, "jpg", file); + return "redirect:/admin_category_list"; + } + + @RequestMapping("admin_category_delete") + public String delete(int id,HttpSession session) throws IOException { + categoryService.delete(id); + + File imageFolder= new File(session.getServletContext().getRealPath("img/category")); + File file = new File(imageFolder,id+".jpg"); + file.delete(); + + return "redirect:/admin_category_list"; + } + + @RequestMapping("admin_category_edit") + public String edit(int id,Model model) throws IOException { + Category c= categoryService.get(id); + model.addAttribute("c", c); + return "admin/editCategory"; + } + + @RequestMapping("admin_category_update") + public String update(Category c, HttpSession session, UploadedImageFile uploadedImageFile) throws IOException { + categoryService.update(c); + MultipartFile image = uploadedImageFile.getImage(); + if(null!=image &&!image.isEmpty()){ + File imageFolder= new File(session.getServletContext().getRealPath("img/category")); + File file = new File(imageFolder,c.getId()+".jpg"); + image.transferTo(file); + BufferedImage img = ImageUtil.change2jpg(file); + ImageIO.write(img, "jpg", file); + } + return "redirect:/admin_category_list"; + } + +} + diff --git a/src/main/java/com/how2java/tmall/controller/ForeController.java b/src/main/java/com/how2java/tmall/controller/ForeController.java new file mode 100644 index 0000000..691138f --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/ForeController.java @@ -0,0 +1,372 @@ + + +package com.how2java.tmall.controller; + +import com.github.pagehelper.PageHelper; +import com.how2java.tmall.pojo.*; +import com.how2java.tmall.service.*; +import comparator.*; +import org.apache.commons.lang.math.RandomUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.util.HtmlUtils; + +import javax.servlet.http.HttpSession; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +@Controller +@RequestMapping("") +public class ForeController { + @Autowired + CategoryService categoryService; + @Autowired + ProductService productService; + @Autowired + UserService userService; + @Autowired + ProductImageService productImageService; + @Autowired + PropertyValueService propertyValueService; + @Autowired + OrderService orderService; + @Autowired + OrderItemService orderItemService; + @Autowired + ReviewService reviewService; + @RequestMapping("forehome") + public String home(Model model) { + List cs= categoryService.list(); + productService.fill(cs); + productService.fillByRow(cs); + model.addAttribute("cs", cs); + return "fore/home"; + } + + @RequestMapping("foreregister") + public String register(Model model,User user) { + String name = user.getName(); + name = HtmlUtils.htmlEscape(name); + user.setName(name); + boolean exist = userService.isExist(name); + + if(exist){ + String m ="用户名已经被使用,不能使用"; + model.addAttribute("msg", m); + + + return "fore/register"; + } + userService.add(user); + + return "redirect:registerSuccessPage"; + } + @RequestMapping("forelogin") + public String login(@RequestParam("name") String name, @RequestParam("password") String password, Model model, HttpSession session) { + name = HtmlUtils.htmlEscape(name); + User user = userService.get(name,password); + + if(null==user){ + model.addAttribute("msg", "账号密码错误"); + return "fore/login"; + } + session.setAttribute("user", user); + return "redirect:forehome"; + } + @RequestMapping("forelogout") + public String logout( HttpSession session) { + session.removeAttribute("user"); + return "redirect:forehome"; + } + + @RequestMapping("foreproduct") + public String product( int pid, Model model) { + Product p = productService.get(pid); + + List productSingleImages = productImageService.list(p.getId(), ProductImageService.type_single); + List productDetailImages = productImageService.list(p.getId(), ProductImageService.type_detail); + p.setProductSingleImages(productSingleImages); + p.setProductDetailImages(productDetailImages); + + List pvs = propertyValueService.list(p.getId()); + List reviews = reviewService.list(p.getId()); + productService.setSaleAndReviewNumber(p); + + model.addAttribute("reviews", reviews); + model.addAttribute("p", p); + model.addAttribute("pvs", pvs); + return "fore/product"; + } + + @RequestMapping("forecheckLogin") + @ResponseBody + public String checkLogin( HttpSession session) { + User user =(User) session.getAttribute("user"); + if(null!=user) + return "success"; + return "fail"; + } + @RequestMapping("foreloginAjax") + @ResponseBody + public String loginAjax(@RequestParam("name") String name, @RequestParam("password") String password,HttpSession session) { + name = HtmlUtils.htmlEscape(name); + User user = userService.get(name,password); + + if(null==user){ + return "fail"; + } + session.setAttribute("user", user); + return "success"; + } + @RequestMapping("forecategory") + public String category(int cid,String sort, Model model) { + Category c = categoryService.get(cid); + productService.fill(c); + productService.setSaleAndReviewNumber(c.getProducts()); + + if(null!=sort){ + switch(sort){ + case "review": + Collections.sort(c.getProducts(),new ProductReviewComparator()); + break; + case "date" : + Collections.sort(c.getProducts(),new ProductDateComparator()); + break; + + case "saleCount" : + Collections.sort(c.getProducts(),new ProductSaleCountComparator()); + break; + + case "price": + Collections.sort(c.getProducts(),new ProductPriceComparator()); + break; + + case "all": + Collections.sort(c.getProducts(),new ProductAllComparator()); + break; + } + } + + model.addAttribute("c", c); + return "fore/category"; + } + + @RequestMapping("foresearch") + public String search( String keyword,Model model){ + + PageHelper.offsetPage(0,20); + List ps= productService.search(keyword); + productService.setSaleAndReviewNumber(ps); + model.addAttribute("ps",ps); + return "fore/searchResult"; + } + @RequestMapping("forebuyone") + public String buyone(int pid, int num, HttpSession session) { + Product p = productService.get(pid); + int oiid = 0; + + User user =(User) session.getAttribute("user"); + boolean found = false; + List ois = orderItemService.listByUser(user.getId()); + for (OrderItem oi : ois) { + if(oi.getProduct().getId().intValue()==p.getId().intValue()){ + oi.setNumber(oi.getNumber()+num); + orderItemService.update(oi); + found = true; + oiid = oi.getId(); + break; + } + } + + if(!found){ + OrderItem oi = new OrderItem(); + oi.setUid(user.getId()); + oi.setNumber(num); + oi.setPid(pid); + orderItemService.add(oi); + oiid = oi.getId(); + } + return "redirect:forebuy?oiid="+oiid; + } + + @RequestMapping("forebuy") + public String buy( Model model,String[] oiid,HttpSession session){ + List ois = new ArrayList<>(); + float total = 0; + + for (String strid : oiid) { + int id = Integer.parseInt(strid); + OrderItem oi= orderItemService.get(id); + total +=oi.getProduct().getPromotePrice()*oi.getNumber(); + ois.add(oi); + } + + session.setAttribute("ois", ois); + model.addAttribute("total", total); + return "fore/buy"; + } + @RequestMapping("foreaddCart") + @ResponseBody + public String addCart(int pid, int num, Model model,HttpSession session) { + Product p = productService.get(pid); + User user =(User) session.getAttribute("user"); + boolean found = false; + + List ois = orderItemService.listByUser(user.getId()); + for (OrderItem oi : ois) { + if(oi.getProduct().getId().intValue()==p.getId().intValue()){ + oi.setNumber(oi.getNumber()+num); + orderItemService.update(oi); + found = true; + break; + } + } + + + if(!found){ + OrderItem oi = new OrderItem(); + oi.setUid(user.getId()); + oi.setNumber(num); + oi.setPid(pid); + orderItemService.add(oi); + } + return "success"; + } + @RequestMapping("forecart") + public String cart( Model model,HttpSession session) { + User user =(User) session.getAttribute("user"); + List ois = orderItemService.listByUser(user.getId()); + model.addAttribute("ois", ois); + return "fore/cart"; + } + + @RequestMapping("forechangeOrderItem") + @ResponseBody + public String changeOrderItem( Model model,HttpSession session, int pid, int number) { + User user =(User) session.getAttribute("user"); + if(null==user) + return "fail"; + + List ois = orderItemService.listByUser(user.getId()); + for (OrderItem oi : ois) { + if(oi.getProduct().getId().intValue()==pid){ + oi.setNumber(number); + orderItemService.update(oi); + break; + } + + } + return "success"; + } + @RequestMapping("foredeleteOrderItem") + @ResponseBody + public String deleteOrderItem( Model model,HttpSession session,int oiid){ + User user =(User) session.getAttribute("user"); + if(null==user) + return "fail"; + orderItemService.delete(oiid); + return "success"; + } + + @RequestMapping("forecreateOrder") + public String createOrder( Model model,Order order,HttpSession session){ + User user =(User) session.getAttribute("user"); + String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000); + order.setOrderCode(orderCode); + order.setCreateDate(new Date()); + order.setUid(user.getId()); + order.setStatus(OrderService.waitPay); + List ois= (List) session.getAttribute("ois"); + + float total =orderService.add(order,ois); + return "redirect:forealipay?oid="+order.getId() +"&total="+total; + } + + + @RequestMapping("forepayed") + public String payed(int oid, float total, Model model) { + Order order = orderService.get(oid); + order.setStatus(OrderService.waitDelivery); + order.setPayDate(new Date()); + orderService.update(order); + model.addAttribute("o", order); + return "fore/payed"; + } + + @RequestMapping("forebought") + public String bought( Model model,HttpSession session) { + User user =(User) session.getAttribute("user"); + List os= orderService.list(user.getId(),OrderService.delete); + + orderItemService.fill(os); + + model.addAttribute("os", os); + + return "fore/bought"; + } + + @RequestMapping("foreconfirmPay") + public String confirmPay( Model model,int oid) { + Order o = orderService.get(oid); + orderItemService.fill(o); + model.addAttribute("o", o); + return "fore/confirmPay"; + } + @RequestMapping("foreorderConfirmed") + public String orderConfirmed( Model model,int oid) { + Order o = orderService.get(oid); + o.setStatus(OrderService.waitReview); + o.setConfirmDate(new Date()); + orderService.update(o); + return "fore/orderConfirmed"; + } + @RequestMapping("foredeleteOrder") + @ResponseBody + public String deleteOrder( Model model,int oid){ + Order o = orderService.get(oid); + o.setStatus(OrderService.delete); + orderService.update(o); + return "success"; + } + @RequestMapping("forereview") + public String review( Model model,int oid) { + Order o = orderService.get(oid); + orderItemService.fill(o); + Product p = o.getOrderItems().get(0).getProduct(); + List reviews = reviewService.list(p.getId()); + productService.setSaleAndReviewNumber(p); + model.addAttribute("p", p); + model.addAttribute("o", o); + model.addAttribute("reviews", reviews); + return "fore/review"; + } + @RequestMapping("foredoreview") + public String doreview( Model model,HttpSession session,@RequestParam("oid") int oid,@RequestParam("pid") int pid,String content) { + Order o = orderService.get(oid); + o.setStatus(OrderService.finish); + orderService.update(o); + + Product p = productService.get(pid); + content = HtmlUtils.htmlEscape(content); + + User user =(User) session.getAttribute("user"); + Review review = new Review(); + review.setContent(content); + review.setPid(pid); + review.setCreateDate(new Date()); + review.setUid(user.getId()); + reviewService.add(review); + + return "redirect:forereview?oid="+oid+"&showonly=true"; + } + +} + + diff --git a/src/main/java/com/how2java/tmall/controller/OrderController.java b/src/main/java/com/how2java/tmall/controller/OrderController.java new file mode 100644 index 0000000..edda45f --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/OrderController.java @@ -0,0 +1,55 @@ + + +package com.how2java.tmall.controller; + +import java.io.IOException; +import java.util.Date; +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.how2java.tmall.pojo.Order; +import com.how2java.tmall.service.OrderItemService; +import com.how2java.tmall.service.OrderService; +import com.how2java.tmall.util.Page; + + +@Controller +@RequestMapping("") +public class OrderController { + @Autowired + OrderService orderService; + @Autowired + OrderItemService orderItemService; + + @RequestMapping("admin_order_list") + public String list(Model model, Page page){ + PageHelper.offsetPage(page.getStart(),page.getCount()); + + List os= orderService.list(); + + int total = (int) new PageInfo<>(os).getTotal(); + page.setTotal(total); + + orderItemService.fill(os); + + model.addAttribute("os", os); + model.addAttribute("page", page); + + return "admin/listOrder"; + } + + @RequestMapping("admin_order_delivery") + public String delivery(Order o) throws IOException { + o.setDeliveryDate(new Date()); + o.setStatus(OrderService.waitConfirm); + orderService.update(o); + return "redirect:admin_order_list"; + } +} + diff --git a/src/main/java/com/how2java/tmall/controller/PageController.java b/src/main/java/com/how2java/tmall/controller/PageController.java new file mode 100644 index 0000000..7b45f82 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/PageController.java @@ -0,0 +1,29 @@ + + +package com.how2java.tmall.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("") +public class PageController { + @RequestMapping("registerPage") + public String registerPage() { + return "fore/register"; + } + @RequestMapping("registerSuccessPage") + public String registerSuccessPage() { + return "fore/registerSuccess"; + } + @RequestMapping("loginPage") + public String loginPage() { + return "fore/login"; + } + @RequestMapping("forealipay") + public String alipay(){ + return "fore/alipay"; + } +} + + diff --git a/src/main/java/com/how2java/tmall/controller/ProductController.java b/src/main/java/com/how2java/tmall/controller/ProductController.java new file mode 100644 index 0000000..2e9d2b0 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/ProductController.java @@ -0,0 +1,77 @@ + + +package com.how2java.tmall.controller; + +import java.util.List; + +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.how2java.tmall.pojo.Category; +import com.how2java.tmall.pojo.Product; +import com.how2java.tmall.service.CategoryService; +import com.how2java.tmall.service.ProductService; +import com.how2java.tmall.util.Page; + +@Controller +@RequestMapping("") +public class ProductController { + @Autowired + CategoryService categoryService; + @Autowired + ProductService productService; + + @RequestMapping("admin_product_add") + public String add(Model model, Product p) { + productService.add(p); + return "redirect:admin_product_list?cid="+p.getCid(); + } + + @RequestMapping("admin_product_delete") + public String delete(int id) { + Product p = productService.get(id); + productService.delete(id); + return "redirect:admin_product_list?cid="+p.getCid(); + } + + @RequestMapping("admin_product_edit") + public String edit(Model model, int id) { + Product p = productService.get(id); + Category c = categoryService.get(p.getCid()); + p.setCategory(c); + model.addAttribute("p", p); + return "admin/editProduct"; + } + + @RequestMapping("admin_product_update") + public String update(Product p) { + productService.update(p); + return "redirect:admin_product_list?cid="+p.getCid(); + } + + @RequestMapping("admin_product_list") + public String list(int cid, Model model, Page page) { + Category c = categoryService.get(cid); + + PageHelper.offsetPage(page.getStart(),page.getCount()); + List ps = productService.list(cid); + + int total = (int) new PageInfo<>(ps).getTotal(); + page.setTotal(total); + page.setParam("&cid="+c.getId()); + + model.addAttribute("ps", ps); + model.addAttribute("c", c); + model.addAttribute("page", page); + + return "admin/listProduct"; + } +} + + diff --git a/src/main/java/com/how2java/tmall/controller/ProductImageController.java b/src/main/java/com/how2java/tmall/controller/ProductImageController.java new file mode 100644 index 0000000..5477d39 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/ProductImageController.java @@ -0,0 +1,123 @@ + + +package com.how2java.tmall.controller; + +import java.awt.image.BufferedImage; +import java.io.File; +import java.util.List; + +import javax.imageio.ImageIO; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.how2java.tmall.pojo.Product; +import com.how2java.tmall.pojo.ProductImage; +import com.how2java.tmall.service.ProductImageService; +import com.how2java.tmall.service.ProductService; +import com.how2java.tmall.util.ImageUtil; +import com.how2java.tmall.util.Page; +import com.how2java.tmall.util.UploadedImageFile; + + +@Controller +@RequestMapping("") +public class ProductImageController { + @Autowired + ProductService productService; + + @Autowired + ProductImageService productImageService; + + + @RequestMapping("admin_productImage_add") + public String add(ProductImage pi, HttpSession session, UploadedImageFile uploadedImageFile) { + productImageService.add(pi); + String fileName = pi.getId()+ ".jpg"; + String imageFolder; + String imageFolder_small=null; + String imageFolder_middle=null; + if(ProductImageService.type_single.equals(pi.getType())){ + imageFolder= session.getServletContext().getRealPath("img/productSingle"); + imageFolder_small= session.getServletContext().getRealPath("img/productSingle_small"); + imageFolder_middle= session.getServletContext().getRealPath("img/productSingle_middle"); + } + else{ + imageFolder= session.getServletContext().getRealPath("img/productDetail"); + } + + File f = new File(imageFolder, fileName); + f.getParentFile().mkdirs(); + try { + uploadedImageFile.getImage().transferTo(f); + BufferedImage img = ImageUtil.change2jpg(f); + ImageIO.write(img, "jpg", f); + + if(ProductImageService.type_single.equals(pi.getType())) { + File f_small = new File(imageFolder_small, fileName); + File f_middle = new File(imageFolder_middle, fileName); + + ImageUtil.resizeImage(f, 56, 56, f_small); + ImageUtil.resizeImage(f, 217, 190, f_middle); + } + + } catch (Exception e) { + e.printStackTrace(); + } + return "redirect:admin_productImage_list?pid="+pi.getPid(); + } + + @RequestMapping("admin_productImage_delete") + public String delete(int id,HttpSession session) { + ProductImage pi = productImageService.get(id); + + String fileName = pi.getId()+ ".jpg"; + String imageFolder; + String imageFolder_small=null; + String imageFolder_middle=null; + + if(ProductImageService.type_single.equals(pi.getType())){ + imageFolder= session.getServletContext().getRealPath("img/productSingle"); + imageFolder_small= session.getServletContext().getRealPath("img/productSingle_small"); + imageFolder_middle= session.getServletContext().getRealPath("img/productSingle_middle"); + File imageFile = new File(imageFolder,fileName); + File f_small = new File(imageFolder_small,fileName); + File f_middle = new File(imageFolder_middle,fileName); + imageFile.delete(); + f_small.delete(); + f_middle.delete(); + + } + else{ + imageFolder= session.getServletContext().getRealPath("img/productDetail"); + File imageFile = new File(imageFolder,fileName); + imageFile.delete(); + } + + + productImageService.delete(id); + + + return "redirect:admin_productImage_list?pid="+pi.getPid(); + } + + @RequestMapping("admin_productImage_list") + public String list(int pid, Model model) { + Product p =productService.get(pid); + List pisSingle = productImageService.list(pid, ProductImageService.type_single); + List pisDetail = productImageService.list(pid, ProductImageService.type_detail); + + + model.addAttribute("p", p); + model.addAttribute("pisSingle", pisSingle); + model.addAttribute("pisDetail", pisDetail); + + return "admin/listProductImage"; + } +} + + diff --git a/src/main/java/com/how2java/tmall/controller/PropertyController.java b/src/main/java/com/how2java/tmall/controller/PropertyController.java new file mode 100644 index 0000000..0b1a4d2 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/PropertyController.java @@ -0,0 +1,75 @@ + + +package com.how2java.tmall.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.how2java.tmall.pojo.Category; +import com.how2java.tmall.pojo.Property; +import com.how2java.tmall.service.CategoryService; +import com.how2java.tmall.service.PropertyService; +import com.how2java.tmall.util.Page; + +@Controller +@RequestMapping("") +public class PropertyController { + @Autowired + CategoryService categoryService; + @Autowired + PropertyService propertyService; + + @RequestMapping("admin_property_add") + public String add(Model model, Property p) { + propertyService.add(p); + return "redirect:admin_property_list?cid="+p.getCid(); + } + + @RequestMapping("admin_property_delete") + public String delete(int id) { + Property p = propertyService.get(id); + propertyService.delete(id); + return "redirect:admin_property_list?cid="+p.getCid(); + } + + @RequestMapping("admin_property_edit") + public String edit(Model model, int id) { + Property p = propertyService.get(id); + Category c = categoryService.get(p.getCid()); + p.setCategory(c); + model.addAttribute("p", p); + return "admin/editProperty"; + } + + @RequestMapping("admin_property_update") + public String update(Property p) { + propertyService.update(p); + return "redirect:admin_property_list?cid="+p.getCid(); + } + + @RequestMapping("admin_property_list") + public String list(int cid, Model model, Page page) { + Category c = categoryService.get(cid); + + PageHelper.offsetPage(page.getStart(),page.getCount()); + List ps = propertyService.list(cid); + + int total = (int) new PageInfo<>(ps).getTotal(); + page.setTotal(total); + page.setParam("&cid="+c.getId()); + + model.addAttribute("ps", ps); + model.addAttribute("c", c); + model.addAttribute("page", page); + + return "admin/listProperty"; + } +} + + diff --git a/src/main/java/com/how2java/tmall/controller/PropertyValueController.java b/src/main/java/com/how2java/tmall/controller/PropertyValueController.java new file mode 100644 index 0000000..4696e12 --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/PropertyValueController.java @@ -0,0 +1,43 @@ + + +package com.how2java.tmall.controller; + +import com.how2java.tmall.pojo.Product; +import com.how2java.tmall.pojo.PropertyValue; +import com.how2java.tmall.service.ProductService; +import com.how2java.tmall.service.PropertyValueService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import java.util.List; + +@Controller +@RequestMapping("") +public class PropertyValueController { + @Autowired + PropertyValueService propertyValueService; + @Autowired + ProductService productService; + + @RequestMapping("admin_propertyValue_edit") + public String edit(Model model,int pid) { + Product p = productService.get(pid); + propertyValueService.init(p); + List pvs = propertyValueService.list(p.getId()); + + model.addAttribute("p", p); + model.addAttribute("pvs", pvs); + return "admin/editPropertyValue"; + } + @RequestMapping("admin_propertyValue_update") + @ResponseBody + public String update(PropertyValue pv) { + propertyValueService.update(pv); + return "success"; + } +} + + diff --git a/src/main/java/com/how2java/tmall/controller/UserController.java b/src/main/java/com/how2java/tmall/controller/UserController.java new file mode 100644 index 0000000..37ffe8c --- /dev/null +++ b/src/main/java/com/how2java/tmall/controller/UserController.java @@ -0,0 +1,43 @@ + + +package com.how2java.tmall.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; + +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import com.how2java.tmall.pojo.User; +import com.how2java.tmall.service.UserService; +import com.how2java.tmall.util.Page; + + +@Controller +@RequestMapping("") +public class UserController { + @Autowired + UserService userService; + + @RequestMapping("admin_user_list") + public String list(Model model, Page page){ + PageHelper.offsetPage(page.getStart(),page.getCount()); + + List us= userService.list(); + + int total = (int) new PageInfo<>(us).getTotal(); + page.setTotal(total); + + model.addAttribute("us", us); + model.addAttribute("page", page); + + return "admin/listUser"; + } + + + +} + diff --git a/src/main/java/com/how2java/tmall/interceptor/LoginInterceptor.java b/src/main/java/com/how2java/tmall/interceptor/LoginInterceptor.java new file mode 100644 index 0000000..2ba6153 --- /dev/null +++ b/src/main/java/com/how2java/tmall/interceptor/LoginInterceptor.java @@ -0,0 +1,96 @@ + + +package com.how2java.tmall.interceptor; +import java.util.Arrays; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import com.how2java.tmall.pojo.Category; +import com.how2java.tmall.pojo.OrderItem; +import com.how2java.tmall.pojo.User; +import com.how2java.tmall.service.CategoryService; +import com.how2java.tmall.service.OrderItemService; + +public class LoginInterceptor extends HandlerInterceptorAdapter { + @Autowired + CategoryService categoryService; + @Autowired + OrderItemService orderItemService; + /** + * 在业务处理器处理请求之前被调用 + * 如果返回false + * 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 + * 如果返回true + * 执行下一个拦截器,直到所有的拦截器都执行完毕 + * 再执行被拦截的Controller + * 然后进入拦截器链, + * 从最后一个拦截器往回执行所有的postHandle() + * 接着再从最后一个拦截器往回执行所有的afterCompletion() + */ + public boolean preHandle(HttpServletRequest request, + HttpServletResponse response, Object handler) throws Exception { + + HttpSession session = request.getSession(); + String contextPath=session.getServletContext().getContextPath(); + String[] noNeedAuthPage = new String[]{ + "home", + "checkLogin", + "register", + "loginAjax", + "login", + "product", + "category", + "search"}; + + String uri = request.getRequestURI(); + uri = StringUtils.remove(uri, contextPath); +// System.out.println(uri); + if(uri.startsWith("/fore")){ + String method = StringUtils.substringAfterLast(uri,"/fore" ); + if(!Arrays.asList(noNeedAuthPage).contains(method)){ + User user =(User) session.getAttribute("user"); + if(null==user){ + response.sendRedirect("loginPage"); + return false; + } + } + } + + return true; + + } + + /** + * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 + * 可在modelAndView中加入数据,比如当前时间 + */ + + public void postHandle(HttpServletRequest request, + HttpServletResponse response, Object handler, + ModelAndView modelAndView) throws Exception { + + + } + + /** + * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 + * + * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() + */ + + public void afterCompletion(HttpServletRequest request, + HttpServletResponse response, Object handler, Exception ex) + throws Exception { + + } + +} + diff --git a/src/main/java/com/how2java/tmall/interceptor/OtherInterceptor.java b/src/main/java/com/how2java/tmall/interceptor/OtherInterceptor.java new file mode 100644 index 0000000..651d8bc --- /dev/null +++ b/src/main/java/com/how2java/tmall/interceptor/OtherInterceptor.java @@ -0,0 +1,89 @@ + + +package com.how2java.tmall.interceptor; +import java.util.Arrays; +import java.util.List; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import org.apache.commons.lang.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.servlet.ModelAndView; +import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; + +import com.how2java.tmall.pojo.Category; +import com.how2java.tmall.pojo.OrderItem; +import com.how2java.tmall.pojo.User; +import com.how2java.tmall.service.CategoryService; +import com.how2java.tmall.service.OrderItemService; + +public class OtherInterceptor extends HandlerInterceptorAdapter { + @Autowired + CategoryService categoryService; + @Autowired + OrderItemService orderItemService; + /** + * 在业务处理器处理请求之前被调用 + * 如果返回false + * 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链 + * 如果返回true + * 执行下一个拦截器,直到所有的拦截器都执行完毕 + * 再执行被拦截的Controller + * 然后进入拦截器链, + * 从最后一个拦截器往回执行所有的postHandle() + * 接着再从最后一个拦截器往回执行所有的afterCompletion() + */ + public boolean preHandle(HttpServletRequest request, + HttpServletResponse response, Object handler) throws Exception { + return true; + + } + + /** + * 在业务处理器处理请求执行完成后,生成视图之前执行的动作 + * 可在modelAndView中加入数据,比如当前时间 + */ + + public void postHandle(HttpServletRequest request, + HttpServletResponse response, Object handler, + ModelAndView modelAndView) throws Exception { + /*这里是获取分类集合信息,用于放在搜索栏下面*/ + List cs = categoryService.list(); + request.getSession().setAttribute("cs", cs); + + /*这里是获取当前的contextPath:tmall_ssm,用与放在左上角那个变形金刚,点击之后才能够跳转到首页,否则点击之后也仅仅停留在当前页面*/ + HttpSession session = request.getSession(); + String contextPath=session.getServletContext().getContextPath(); + request.getSession().setAttribute("contextPath", contextPath); + + /*这里是获取购物车中一共有多少数量*/ + User user =(User) session.getAttribute("user"); + int cartTotalItemNumber = 0; + if(null!=user) { + List ois = orderItemService.listByUser(user.getId()); + for (OrderItem oi : ois) { + cartTotalItemNumber+=oi.getNumber(); + } + + } + request.getSession().setAttribute("cartTotalItemNumber", cartTotalItemNumber); + + } + + /** + * 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等 + * + * 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion() + */ + + public void afterCompletion(HttpServletRequest request, + HttpServletResponse response, Object handler, Exception ex) + throws Exception { + +// System.out.println("afterCompletion(), 在访问视图之后被调用"); + } + +} +