基于javaweb+mysql的ssm+maven校园鲜花销售商城系统(java+ssm+jsp+layui+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM+Maven校园鲜花销售商城系统(java+ssm+jsp+layui+mysql)
一、项目简述 环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
功能介绍:用户管理、商品管理、评论管理、订单管理、管理员管理、资讯管理、登录注册 等等。
* @return
*/
@RequestMapping("/foreLoginMsg")
public String foreLoginMsg(HttpServletRequest request){
request.setAttribute("msg","true");
return "forepage/forelogin";
}
/**
* 客户注销
* @param session
* @return
*/
@RequestMapping("/foreCstLoginOut")
public String cstLoginOut(HttpSession session){
session.setAttribute("cst",null);
return "redirect:foreIndex";
}
/**
* 立即购买
* @param session
* @param pid 商品id
* @param number 商品数量
* @return 重定向到支付 , 传入订单项id
*/
@RequestMapping("/forebuyone")
public String forebuyone(HttpSession session, int pid, int number, float totalPrice){
Customer cst = (Customer) session.getAttribute("cst");
Product product = productService.get(pid);
int oiid = 0;
boolean find = false;
List<OrderItem> orderItems = orderItemService.listByCustomer(cst.getId());//获得订单项表中该用户的所有订单id为空的订单项
for (OrderItem oi : orderItems) {
//基于用户对象customer,查询没有生成订单的订单项集合
// 如果产品是一样的话,就进行数量追加
if(oi.getProduct().getId().intValue()==product.getId().intValue()){
//如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
oi.setNumber(oi.getNumber()+number);
/**
* 前台登陆状态拦截器 如果访问的请求没有在noNeedAuthPage数组就跳转登陆
*/
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Autowired
OrderItemService orderItemService;
/**
* 在业务处理器处理请求之前被调用
* 如果返回false
* 从当前的拦截器往回执行所有拦截器的afterCompletion(),再退出拦截器链
* 如果返回true
* 执行下一个拦截器,直到所有的拦截器都执行完毕
* 再执行被拦截的Controller
* 然后进入拦截器链,
* 从最后一个拦截器往回执行所有的postHandle()
* 接着再从最后一个拦截器往回执行所有的afterCompletion()
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
/**
* 不需要登录也可以访问的
* 注册,登录,产品,首页,分类,查询等等
* 需要登录才能够访问的
* 购买行为,加入购物车行为,查看购物车,查看我的订单等等
* 不需要登录也可以访问的已经确定了,但是需要登录才能够访问,截止目前为止还不能确定,所以这个过滤器就判断如果不是注册,登录,产品这些,就进行登录校验
* 1. 准备字符串数组 noNeedAuthPage,存放哪些不需要登录也能访问的路径
* 2. 获取uri
* 3. 去掉前缀/fore
* 4. 如果访问的地址是/fore开头
* 4.1 取出fore后面的字符串,比如是forecart,那么就取出cart
* 4.2 判断cart是否是在noNeedAuthPage
* 4.2 如果不在,那么就需要进行是否登录验证
* 4.3 从session中取出"cst"对象
* 4.4 如果对象不存在,就客户端跳转到login.jsp
* 4.5 否则就正常执行
*/
HttpSession session = request.getSession();
String contextPath=session.getServletContext().getContextPath()+"/fore";
//准备字符串数组 noNeedAuthPage,存放哪些不需要登录也能访问的路径
String[] noNeedAuthPage = new String[]{
"Index", //首页
"DetailUI", //商品详情页
"RegisterUI", //注册页
"Register", //注册
"LoginUI", //登陆页
"Login", //登陆
"IsLogin", //判断是否登陆
"MtLogin", //模态登陆验证
"CstLoginOut", //退出
* @param oiid 订单项id
* @param session
* @return
*/
@RequestMapping("/foreDelOrderItem")
@ResponseBody
public String foreDelOrderItem(int oiid, HttpSession session){
Customer customer = (Customer) session.getAttribute("cst");
if(customer==null){
return "noSuccess";
}
orderItemService.del(oiid);
return "success";
}
/*
点击提交订单
1. 从session中获取cst对象
2. 通过参数Order接受收货人
3. 根据当前时间加上一个4位随机数生成订单号
4. 根据上述参数,创建订单对象
5. 把订单状态设置为未支付
6. 从session中获取订单项集合 ( 在结算功能的ForeController.buy() 13行,订单项集合被放到了session中 )
7. 把订单加入到数据库,并且遍历订单项集合,设置每个订单项的order,更新到数据库
8. 统计本次订单的总金额
9. 客户端跳转到确认支付页forePayed,并带上订单id和总金额
*/
@RequestMapping("/foreCreateOrder")
public String createOrder(Model model, String address, HttpSession session){
/*
提交订单后,设置code,客户id,支付状态,地址
*/
Order order = new Order();
Customer customer =(Customer) session.getAttribute("cst");
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setCode(orderCode);
order.setAddress(address);
order.setCstid(customer.getId());
order.setStatus(0);//未支付
List<OrderItem> ois= (List<OrderItem>) session.getAttribute("ois");
//给每个订单项设置订单id 并且算出订单总价
float total =orderService.add(order,ois);
return "redirect:forePayed?oid="+order.getId() +"&total="+total;
}
/**
* 支付成功跳转
* @param oid 订单id
* @param total 总价
* @param model
* @return
*/
@RequestMapping("/forePayed")
/**
* 前台所有请求controller
*/
@Controller
@RequestMapping("/fore")
public class ForeController {
@Autowired
private ForeService foreService;
@Autowired
private ProductService productService;
@Autowired
private ReviewService reviewService;
@Autowired
private CategoryService categoryService;
@Autowired
private CustomerService customerService;
@Autowired
private OrderItemService orderItemService;
@Autowired
private OrderService orderService;
@Autowired
private ZiXunService ziXunService;
public String PNAME=null;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping("/foreIndex")
public String index(Model model, HttpSession session){
//传入3个分类
List<Category> categories = foreService.listToThree();
List<Category> categories1 = categoryService.list();
//给每个分类设置商品
for (Category c:categories){
List<Product> products = productService.getProductsByCid(c.getId());
//如果分类下的商品超过4个,则只显示4个给前端
if(products.size()>5){
List<Product> products1 = new ArrayList<Product>();
}
/**
* 显示分类下的商品
* @param model
* @param cid
* @return
*/
@RequestMapping("/foreFindCategory")
public String foreFindCategory(Model model, @RequestParam(value = "id") int cid){
List<Product> ps = productService.findByCid(cid);
Category category = categoryService.get(cid);
if(ps.size()>8){
List<Product> ps1 = new ArrayList<Product>();
for(int i=0;i<8;i++){
ps1.add(ps.get(i));
}
model.addAttribute("products",ps1);
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
model.addAttribute("products",ps);
model.addAttribute("proSize",ps.size());
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
@RequestMapping("/faq")
public String faq(){
return "forepage/faq";
}
/**
* 商品评价
* @param pid
* @param model
* @return
*/
@RequestMapping("/forePingjia")
public String forePingjia(int pid, Model model){
return "forePage/pingjia";
}
/**
* 商品评论
* @param session
* @param pid
* @param content
/**
* 全局异常类
*/
@ControllerAdvice
public class DefaultExceptionHandler {
@ExceptionHandler({UnauthorizedException.class}) //异常判断类
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ModelAndView processUnauthenticatedException(NativeWebRequest request, UnauthorizedException e) {
ModelAndView mv = new ModelAndView();
mv.addObject("ex", e);
mv.setViewName("unauthorized");
return mv;
}
}
/**
* 专门用于显示页面的控制器
*/
@Controller
@RequestMapping("")
public class PageController {
/**
* 后台主页页面
* @return
*/
@RequestMapping("/index")
@RequestMapping("/list")
public String list(Model model){
List<Category> list = categoryService.list();
model.addAttribute("list",list);
model.addAttribute("size",list.size());
return "productmodule/category-list";
}
@RequestMapping("/addCategory")
public String add(@RequestParam(value = "name")String name){
Category category = new Category();
category.setName(name);
categoryService.save(category);
return "productmodule/category-list";
}
@RequestMapping("/delCategory")
public String del(@RequestParam(value = "id")int id){
categoryService.del(id);
return "redirect:list";
}
@RequestMapping("/editCategory")
public String edit(@RequestParam(value = "id")int id, Model model){
Category category = categoryService.get(id);
model.addAttribute("category",category);
return "productmodule/category-edit";
}
@RequestMapping("/updateCategory")
public String update(Category category, Model model){
categoryService.update(category);
return "redirect:list";
}
}
* 4. 如果访问的地址是/fore开头
* 4.1 取出fore后面的字符串,比如是forecart,那么就取出cart
* 4.2 判断cart是否是在noNeedAuthPage
* 4.2 如果不在,那么就需要进行是否登录验证
* 4.3 从session中取出"cst"对象
* 4.4 如果对象不存在,就客户端跳转到login.jsp
* 4.5 否则就正常执行
*/
HttpSession session = request.getSession();
String contextPath=session.getServletContext().getContextPath()+"/fore";
//准备字符串数组 noNeedAuthPage,存放哪些不需要登录也能访问的路径
String[] noNeedAuthPage = new String[]{
"Index", //首页
"DetailUI", //商品详情页
"RegisterUI", //注册页
"Register", //注册
"LoginUI", //登陆页
"Login", //登陆
"IsLogin", //判断是否登陆
"MtLogin", //模态登陆验证
"CstLoginOut", //退出
"DelOrderItem",//删除购物车项
"CreateOrder", //提交订单
"Payed", //支付成功
"NameLike", //模糊搜索商品
"FindCategory", //查看分类下的商品
"Zixunadd", //添加资讯
"LoginMsg", //登陆返回信息
};
//获取uri
String uri = request.getRequestURI(); //访问首页 /fore/foreIndex
//去掉前缀/fore
uri = uri.substring(5,uri.length()); //去掉前缀后 /foreIndex
//如果访问的地址是/fore开头
if(uri.startsWith("/fore")){
//判断是否是在noNeedAuthPage
String method = StringUtils.substringAfterLast(uri,"/fore" ); // 取出/fore后的字符串:Index
//如果不在,那么就需要进行是否登录验证
if(!Arrays.asList(noNeedAuthPage).contains(method)){
Customer customer =(Customer) session.getAttribute("cst");
if(null==customer){
response.sendRedirect("foreLoginUI");
return false;
}
}
}
return true;
//当前管理员拥有的权限
List<Permission> currentPermissions = permissionService.list(role);
model.addAttribute("currentPermissions", currentPermissions);
return "syspage/admin-role-edit";
}
@RequestMapping("/updateRole")
public String update(Role role,long[] permissionIds){
rolePermissionService.setPermissions(role, permissionIds);
roleService.update(role);
return "redirect:listRole";
}
@RequestMapping("/addRole")
public String list(Model model, Role role){
roleService.add(role);
return "redirect:listRole";
}
@RequestMapping("/deleteRole")
public String delete(Model model, long id){
roleService.delete(id);
return "redirect:listRole";
}
}
* 模态窗口登陆 验证
* @param customer
* @param session
* @return
*/
@RequestMapping("/foreMtLogin")
@ResponseBody
public String foreIsLogin(Customer customer, HttpSession session){
Customer cst = customerService.foreLogin(customer);
if(null==cst){
return "false";
}
session.setAttribute("cst", cst);
return "true";
}
/**
* ajax判断客户是否登陆
* @param session
* @return
*/
@RequestMapping("/foreIsLogin")
@ResponseBody
public String isLogin(HttpSession session){
Customer cst = (Customer) session.getAttribute("cst");
return cst==null?"false":"true";
}
/**
* 注册
* @param customer
* @return
*/
@RequestMapping("/foreRegister")
public String register(Customer customer){
customer.setStatus(0);
customerService.save(customer);
return "forepage/registerSuccess";
}
/**
* 客户登陆
* @param customer
* @param session
* @return
*/
@RequestMapping("/foreLogin")
}
model.addAttribute("products",ps1);
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
model.addAttribute("products",ps);
model.addAttribute("proSize",ps.size());
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
@RequestMapping("/faq")
public String faq(){
return "forepage/faq";
}
/**
* 商品评价
* @param pid
* @param model
* @return
*/
@RequestMapping("/forePingjia")
public String forePingjia(int pid, Model model){
return "forePage/pingjia";
}
/**
* 商品评论
* @param session
* @param pid
* @param content
* @return
*/
@RequestMapping("/cstPinglun")
@ResponseBody
public String cstPinglun(HttpSession session, int pid, String content){
Customer cst = (Customer) session.getAttribute("cst");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String format = sdf.format(new Date());
//string转date
ParsePosition pos = new ParsePosition(0);
Date strtodate = sdf.parse(format, pos);
Review review = new Review();
review.setCstid(cst.getId());
review.setCustomer(cst);
review.setPid(pid);
productService.save(product);
if (upload != null) {
String imageName = product.getId()+".jpg";
File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
System.out.println(session.getServletContext().getRealPath("/images/product"));
file.getParentFile().mkdirs();
upload.getImage().transferTo(file);
System.out.println("["+product.getId()+","+"images/product/"+imageName+"]");
ProductVO vo = new ProductVO();
vo.setId(product.getId());
vo.setImageUrl("images/product/"+imageName);
productService.setImageURL(vo);
System.out.println(productService.get(product.getId()));
}
return "redirect:list";
}
@RequestMapping("/deleteProduct")
public String del(@RequestParam(value = "id")int id, HttpSession session){
productService.del(id);
String imageName = id+".jpg";
File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
file.delete();
return "redirect:list";
}
@RequestMapping("/editProduct")
public String editUI(@RequestParam(value = "id")int id, Model model){
//获得要修改商品的信息
Product product = productService.get(id);
model.addAttribute("product",product);
System.out.println(product);
List<Category> categoryList = categoryService.list();
List<User> userList = userService.list();
//通过商品id 返回所属分类
* @return
*/
@RequestMapping("/foreNameLike")
public String foreNameLike(Model model, String pName, Page page){
PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
if(pName!=null) PNAME = pName;
List<Product> products = productService.findByName(PNAME);
int total = (int) new PageInfo<Product>(products).getTotal();//总条数
page.setTotal(total);
model.addAttribute("products",products);
model.addAttribute("total",total);
model.addAttribute("page", page);
model.addAttribute("proSize",products.size());
return "forepage/proSeach";
}
/**
* 显示分类下的商品
* @param model
* @param cid
* @return
*/
@RequestMapping("/foreFindCategory")
public String foreFindCategory(Model model, @RequestParam(value = "id") int cid){
List<Product> ps = productService.findByCid(cid);
Category category = categoryService.get(cid);
if(ps.size()>8){
List<Product> ps1 = new ArrayList<Product>();
for(int i=0;i<8;i++){
ps1.add(ps.get(i));
}
model.addAttribute("products",ps1);
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
model.addAttribute("products",ps);
model.addAttribute("proSize",ps.size());
model.addAttribute("category",category);
return "forepage/proCategorySeach";
}
List<OrderItem> ois = new ArrayList<OrderItem>();
Customer cst = (Customer)session.getAttribute("cst");
float total = 0;
int number = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi= orderItemService.get(id);
if (cst.getStatus()==1){
total +=oi.getProduct().getPrice()*0.8*oi.getNumber();
}else{
total +=oi.getProduct().getPrice()*oi.getNumber();
}
number += oi.getNumber();
ois.add(oi);
}
/*
累计这些ois的价格总数,赋值在total上
把订单项集合放在session的属性 "ois" 上,方便下订单时候直接获取
把总价格放在 model的属性 "total" 上
服务端跳转到buy.jsp
*/
session.setAttribute("ois", ois);
model.addAttribute("total", total);
model.addAttribute("number", number);
return "forepage/foreBuy";
}
/**
* 添加购物车
* @param pid 商品id
* @param number 购买数量
* @param model
* @param session
* @return boolean
*/
@RequestMapping("/foreAddCart")
@ResponseBody
return "forepage/proDetail";
}
/**
* 注册页面
* @return
*/
@RequestMapping("/foreRegisterUI")
public String registerUI(){
return "forepage/foreRegister";
}
/**
* 登陆页面
* @return
*/
@RequestMapping("/foreLoginUI")
public String foreLoginUI(){
return "forepage/forelogin";
}
/**
* 模态窗口登陆 验证
* @param customer
* @param session
* @return
*/
@RequestMapping("/foreMtLogin")
@ResponseBody
public String foreIsLogin(Customer customer, HttpSession session){
Customer cst = customerService.foreLogin(customer);
if(null==cst){
return "false";
}
session.setAttribute("cst", cst);
return "true";
}
/**
* ajax判断客户是否登陆
* @param session
* @return
@ResponseBody
public String stopStatus(@RequestParam(value = "name") String name){
return productService.stopStatus(name);
}
@RequestMapping("/productAddUI")
public String addUI(Model model){
List<Category> categoryList = categoryService.list();
List<User> userList = userService.list();
model.addAttribute("categoryList",categoryList);
model.addAttribute("userList",userList);
return "productmodule/product-add";
}
@RequestMapping("/addProduct")
public String add(Product product, HttpSession session, UploadUtil upload) throws IOException {
productService.save(product);
if (upload != null) {
String imageName = product.getId()+".jpg";
File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
System.out.println(session.getServletContext().getRealPath("/images/product"));
file.getParentFile().mkdirs();
upload.getImage().transferTo(file);
System.out.println("["+product.getId()+","+"images/product/"+imageName+"]");
ProductVO vo = new ProductVO();
vo.setId(product.getId());
vo.setImageUrl("images/product/"+imageName);
productService.setImageURL(vo);
System.out.println(productService.get(product.getId()));
}
/**
* 查看购物车购物车
* @param model
* @param session
* @return
*/
@RequestMapping("/forecart")
public String cart(Model model, HttpSession session) {
Customer customer =(Customer) session.getAttribute("cst");
//cstid等于当前登陆用户id 并且oid为null的订单项
List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
//购物车没有商品
if(ois==null || ois.size()==0){
return "forepage/cart_noPro";
}
int totalProductNumber = 0;
for (OrderItem oi:ois){
totalProductNumber += oi.getNumber();
}
model.addAttribute("ois", ois);
model.addAttribute("size", totalProductNumber);
return "forepage/foreCart";
}
/**
* 删除订单项
* @param oiid 订单项id
* @param session
* @return
*/
@RequestMapping("/foreDelOrderItem")
@ResponseBody
public String foreDelOrderItem(int oiid, HttpSession session){
Customer customer = (Customer) session.getAttribute("cst");
if(customer==null){
return "noSuccess";
}
orderItemService.del(oiid);
return "success";
}
/*
点击提交订单
1. 从session中获取cst对象
2. 通过参数Order接受收货人
3. 根据当前时间加上一个4位随机数生成订单号
4. 根据上述参数,创建订单对象
5. 把订单状态设置为未支付
2. 通过参数Order接受收货人
3. 根据当前时间加上一个4位随机数生成订单号
4. 根据上述参数,创建订单对象
5. 把订单状态设置为未支付
6. 从session中获取订单项集合 ( 在结算功能的ForeController.buy() 13行,订单项集合被放到了session中 )
7. 把订单加入到数据库,并且遍历订单项集合,设置每个订单项的order,更新到数据库
8. 统计本次订单的总金额
9. 客户端跳转到确认支付页forePayed,并带上订单id和总金额
*/
@RequestMapping("/foreCreateOrder")
public String createOrder(Model model, String address, HttpSession session){
/*
提交订单后,设置code,客户id,支付状态,地址
*/
Order order = new Order();
Customer customer =(Customer) session.getAttribute("cst");
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setCode(orderCode);
order.setAddress(address);
order.setCstid(customer.getId());
order.setStatus(0);//未支付
List<OrderItem> ois= (List<OrderItem>) session.getAttribute("ois");
//给每个订单项设置订单id 并且算出订单总价
float total =orderService.add(order,ois);
return "redirect:forePayed?oid="+order.getId() +"&total="+total;
}
/**
* 支付成功跳转
* @param oid 订单id
* @param total 总价
* @param model
* @return
*/
@RequestMapping("/forePayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(1);
orderService.update(order);
model.addAttribute("total", total);
return "forepage/forePayed";
}
/**
* 我的订单 根据session查看当前用户的订单
* @param model
* @param session