Java项目:零食商城系统(java+SSM+jsp+MySQL+EasyUI)

2021/12/28 19:10:02

本文主要是介绍Java项目:零食商城系统(java+SSM+jsp+MySQL+EasyUI),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

源码获取:博客首页 "资源" 里下载!

系统主要实现的功能有:用户浏览商品、加入商品到购物车、登录注册、提交订单,会员中心修改个人信息、查看订单等。

后台管理员登录后可以分角色添加管理员,不同角色有不同权限,可以管理用户信息、商品信息、订单信息,订单信息可以导出到excel。其中支付是模拟在线支付,从账户余额中扣费的,用户注册后自动设置余额为指定值。

 

 

后台相关接口:

/**
 * 后台相关接口
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

	private static final int rows = 10;

	@Autowired
	private AdminService adminService;
	@Autowired
	private OrderService orderService;
	@Autowired
	private UserService userService;
	@Autowired
	private GoodService goodService;
	@Autowired
	private TopService topService;
	@Autowired
	private TypeService typeService;
	@Autowired
	private SkuService skuService;

	/**
	 * 管理员登录
	 * @return
	 */
	@GetMapping("/login")
	public String log() {
		return "/admin/login.jsp";
	}
	
	/**
	 * 管理员登录
	 * @return
	 */
	@PostMapping("/login")
	public String login(Admins admin, HttpServletRequest request, HttpSession session) {
		if (adminService.checkUser(admin.getUsername(), admin.getPassword())) {
			session.setAttribute("username", admin.getUsername());
			return "redirect:typeList";
		}
		request.setAttribute("msg", "用户名或密码错误!");
		return "/admin/login.jsp";
	}

	/**
	 * 退出
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("admin");
		return "/admin/login.jsp";
	}
	
	/**
	 * 后台首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request) {
		request.setAttribute("msg", "恭喜你! 登录成功了");
		return "/admin/index.jsp";
	}

	/**
	 * 类目列表
	 * 
	 * @return
	 */
	@RequestMapping("/typeList")
	public String typeList(HttpServletRequest request) {
		request.setAttribute("flag", 1);
		request.setAttribute("typeList", typeService.getList());
		return "/admin/type_list.jsp";
	}

	/**
	 * 类目添加
	 * 
	 * @return
	 */
	@RequestMapping("/typeAdd")
	public String typeAdd(HttpServletRequest request) {
		request.setAttribute("flag", 1);
		return "/admin/type_add.jsp";
	}
	
	/**
	 * 类目添加
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/typeSave")
	public String typeSave(Types type, MultipartFile file,
			@RequestParam(required=false, defaultValue="1") int page) throws Exception {
		type.setCover(UploadUtil.upload(file));
		typeService.add(type);
		return "redirect:typeList?flag=1&page="+page;
	}

	/**
	 * 类目更新
	 * 
	 * @return
	 */
	@RequestMapping("/typeEdit")
	public String typeEdit(int id, HttpServletRequest request) {
		request.setAttribute("flag", 1);
		request.setAttribute("type", typeService.get(id));
		return "/admin/type_edit.jsp";
	}

	/**
	 * 类目更新
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/typeUpdate")
	public String typeUpdate(Types type, MultipartFile file,
			@RequestParam(required=false, defaultValue="1") int page) throws Exception {
		if (Objects.nonNull(file) && !file.isEmpty()) {
			type.setCover(UploadUtil.upload(file));
		}
		typeService.update(type);
		return "redirect:typeList?flag=1&page="+page;
	}

	/**
	 * 类目删除
	 * 
	 * @return
	 */
	@RequestMapping("/typeDelete")
	public String typeDelete(Types type, 
			@RequestParam(required=false, defaultValue="1") int page) {
		typeService.delete(type);
		return "redirect:typeList?flag=1&page="+page;
	}

	/**
	 * sku列表
	 * 
	 * @return
	 */
	@RequestMapping("/skuList")
	public String skuList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request) {
		request.setAttribute("flag", 2);
		request.setAttribute("status", status);
		request.setAttribute("skuList", status>0 ? skuService.getSizeList() : skuService.getColorList());
		return "/admin/sku_list.jsp";
	}
	
	/**
	 * sku添加
	 * 
	 * @return
	 */
	@RequestMapping("/skuSave")
	public String skuSave(String name, int status) {
		if(status > 0) {
			skuService.addSize(name);
		}else {
			skuService.addColor(name);
		}
		return "redirect:skuList?status="+status;
	}
	
	/**
	 * sku删除
	 * 
	 * @return
	 */
	@RequestMapping("/skuDelete")
	public String skuDelete(int id, int status) {
		if(status > 0) {
			skuService.deleteSize(id);
		}else {
			skuService.deleteColor(id);
		}
		return "redirect:skuList?status="+status;
	}
	
	
	/**
	 * 产品列表
	 * 
	 * @return
	 */
	@RequestMapping("/goodList")
	public String goodList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request, 
			@RequestParam(required=false, defaultValue="1") int page) {
		request.setAttribute("flag", 3);
		request.setAttribute("page", page);
		request.setAttribute("status", status);
		request.setAttribute("goodList", goodService.getList(status, page, rows));
		request.setAttribute("pageTool", PageUtil.getPageTool(request, goodService.getTotal(status), page, rows));
		return "/admin/good_list.jsp";
	}

	/**
	 * 产品添加
	 * 
	 * @return
	 */
	@RequestMapping("/goodAdd")
	public String goodAdd(HttpServletRequest request) {
		request.setAttribute("flag", 3);
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("colorList", skuService.getColorList());
		request.setAttribute("sizeList", skuService.getSizeList());
		return "/admin/good_add.jsp";
	}

	/**
	 * 产品添加
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/goodSave")
	public String goodSave(Goods good, MultipartFile file,
			@RequestParam(required=false, defaultValue="1") int page) throws Exception {
		good.setCover(UploadUtil.upload(file));
		goodService.add(good);
		return "redirect:goodList?flag=3&page="+page;
	}

	/**
	 * 产品更新
	 * 
	 * @return
	 */
	@RequestMapping("/goodEdit")
	public String goodEdit(int id, HttpServletRequest request) {
		request.setAttribute("flag", 3);
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("colorList", skuService.getColorList());
		request.setAttribute("sizeList", skuService.getSizeList());
		request.setAttribute("good", goodService.get(id));
		return "/admin/good_edit.jsp";
	}

	/**
	 * 产品更新
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/goodUpdate")
	public String goodUpdate(Goods good, MultipartFile file, 
			@RequestParam(required=false, defaultValue="1") int page) throws Exception {
		if (Objects.nonNull(file) && !file.isEmpty()) {
			good.setCover(UploadUtil.upload(file));
		}
		goodService.update(good);
		return "redirect:goodList?flag=3&page="+page;
	}

	/**
	 * 产品删除
	 * 
	 * @return
	 */
	@RequestMapping("/goodDelete")
	public String goodDelete(int id, 
			@RequestParam(required=false, defaultValue="1") int page) {
		goodService.delete(id);
		return "redirect:goodList?flag=3&page="+page;
	}
	
	/**
	 * 添加推荐
	 * @return
	 */
	@RequestMapping("/topSave")
	public @ResponseBody String topSave(Tops tops, 
			@RequestParam(required=false, defaultValue="0")byte status,
			@RequestParam(required=false, defaultValue="1") int page) {
		int id = topService.add(tops);
		return id > 0 ? "ok" : null;
	}
	
	/**
	 * 删除推荐
	 * @return
	 */
	@RequestMapping("/topDelete")
	public @ResponseBody String topDelete(Tops tops, 
			@RequestParam(required=false, defaultValue="0")byte status,
			@RequestParam(required=false, defaultValue="1") int page) {
		boolean flag = topService.delete(tops);
		return flag ? "ok" : null;
	}

	/**
	 * 订单列表
	 * 
	 * @return
	 */
	@RequestMapping("/orderList")
	public String orderList(@RequestParam(required=false, defaultValue="0")byte status, HttpServletRequest request,
			@RequestParam(required=false, defaultValue="1") int page) {
		request.setAttribute("flag", 4);
		request.setAttribute("status", status);
		request.setAttribute("orderList", orderService.getList(status, page, rows));
		request.setAttribute("pageTool", PageUtil.getPageTool(request, orderService.getTotal(status), page, rows));
		return "/admin/order_list.jsp";
	}

	/**
	 * 订单发货
	 * 
	 * @return
	 */
	@RequestMapping("/orderDispose")
	public String orderDispose(int id, byte status,
			@RequestParam(required=false, defaultValue="1") int page) {
		orderService.dispose(id);
		return "redirect:orderList?flag=4&status="+status+"&page="+page;
	}
	
	/**
	 * 订单完成
	 * 
	 * @return
	 */
	@RequestMapping("/orderFinish")
	public String orderFinish(int id, byte status,
			@RequestParam(required=false, defaultValue="1") int page) {
		orderService.finish(id);
		return "redirect:orderList?flag=4&status="+status+"&page="+page;
	}

	/**
	 * 订单删除
	 * 
	 * @return
	 */
	@RequestMapping("/orderDelete")
	public String orderDelete(int id, byte status,
			@RequestParam(required=false, defaultValue="1") int page) {
		orderService.delete(id);
		return "redirect:orderList?flag=4&status="+status+"&page="+page;
	}

	/**
	 * 顾客管理
	 * 
	 * @return
	 */
	@RequestMapping("/userList")
	public String userList(HttpServletRequest request,
			@RequestParam(required=false, defaultValue="1") int page) {
		request.setAttribute("flag", 5);
		request.setAttribute("userList", userService.getList(page, rows));
		request.setAttribute("pageTool", PageUtil.getPageTool(request, userService.getTotal(), page, rows));
		return "/admin/user_list.jsp";
	}

	/**
	 * 顾客添加
	 * 
	 * @return
	 */
	@RequestMapping("/userAdd")
	public String userAdd(HttpServletRequest request) {
		request.setAttribute("flag", 5);
		return "/admin/user_add.jsp";
	}

	/**
	 * 顾客添加
	 * 
	 * @return
	 */
	@RequestMapping("/userSave")
	public String userSave(Users user, HttpServletRequest request, 
			@RequestParam(required=false, defaultValue="1") int page) {
		if (userService.isExist(user.getUsername())) {
			request.setAttribute("msg", "用户名已存在!");
			return "/admin/user_add.jsp";
		}
		userService.add(user);
		return "redirect:userList?flag=5&page="+page;
	}

	/**
	 * 顾客密码重置页面
	 * 
	 * @return
	 */
	@RequestMapping("/userRe")
	public String userRe(int id, HttpServletRequest request) {
		request.setAttribute("flag", 5);
		request.setAttribute("user", userService.get(id));
		return "/admin/user_reset.jsp";
	}

	/**
	 * 顾客密码重置
	 * 
	 * @return
	 */
	@RequestMapping("/userReset")
	public String userReset(Users user, 
			@RequestParam(required=false, defaultValue="1") int page) {
		String password = SafeUtil.encode(user.getPassword());
		user = userService.get(user.getId());
		user.setPassword(password);
		userService.update(user);
		return "redirect:userList?page="+page;
	}

	/**
	 * 顾客更新
	 * 
	 * @return
	 */
	@RequestMapping("/userEdit")
	public String userEdit(int id, HttpServletRequest request) {
		request.setAttribute("flag", 5);
		request.setAttribute("user", userService.get(id));
		return "/admin/user_edit.jsp";
	}

	/**
	 * 顾客更新
	 * 
	 * @return
	 */
	@RequestMapping("/userUpdate")
	public String userUpdate(Users user, 
			@RequestParam(required=false, defaultValue="1") int page) {
		userService.update(user);
		return "redirect:userList?flag=5&page="+page;
	}

	/**
	 * 顾客删除
	 * 
	 * @return
	 */
	@RequestMapping("/userDelete")
	public String userDelete(Users user, 
			@RequestParam(required=false, defaultValue="1") int page) {
		userService.delete(user);
		return "redirect:userList?flag=5&page="+page;
	}

	/**
	 * 管理员列表
	 * 
	 * @return
	 */
	@RequestMapping("/adminList")
	public String adminList(HttpServletRequest request, 
			@RequestParam(required=false, defaultValue="1") int page) {
		request.setAttribute("flag", 6);
		request.setAttribute("adminList", adminService.getList(page, rows));
		request.setAttribute("pageTool", PageUtil.getPageTool(request, adminService.getTotal(), page, rows));
		return "/admin/admin_list.jsp";
	}

	/**
	 * 管理员添加
	 * 
	 * @return
	 */
	@RequestMapping("/adminAdd")
	public String adminAdd(HttpServletRequest request) {
		request.setAttribute("flag", 6);
		return "/admin/admin_add.jsp";
	}
	
	/**
	 * 管理员密码重置
	 * 
	 * @return
	 */
	@RequestMapping("/adminRe")
	public String adminRe(int id, HttpServletRequest request) {
		request.setAttribute("flag", 6);
		request.setAttribute("admin", adminService.get(id));
		return "/admin/admin_reset.jsp";
	}

	/**
	 * 管理员密码重置
	 * 
	 * @return
	 */
	@RequestMapping("/adminReset")
	public String adminReset(Admins admin, HttpServletRequest request,
			@RequestParam(required=false, defaultValue="1") int page) {
		String password = SafeUtil.encode(admin.getPassword());
		admin = adminService.get(admin.getId());
		admin.setPassword(password);
		adminService.update(admin);
		return "redirect:adminList?page="+page;
	}

	/**
	 * 管理员添加
	 * 
	 * @return
	 */
	@RequestMapping("/adminSave")
	public String adminSave(Admins admin, HttpServletRequest request, 
			@RequestParam(required=false, defaultValue="1") int page) {
		if (adminService.isExist(admin.getUsername())) {
			request.setAttribute("msg", "用户名已存在!");
			return "/admin/admin_add.jsp";
		}
		adminService.add(admin);
		return "redirect:adminList?flag=6&page="+page;
	}

	/**
	 * 管理员修改
	 * 
	 * @return
	 */
	@RequestMapping("/adminEdit")
	public String adminEdit(int id, HttpServletRequest request) {
		request.setAttribute("flag", 6);
		request.setAttribute("admin", adminService.get(id));
		return "/admin/admin_edit.jsp";
	}

	/**
	 * 管理员更新
	 * 
	 * @return
	 */
	@RequestMapping("/adminUpdate")
	public String adminUpdate(Admins admin, 
			@RequestParam(required=false, defaultValue="1") int page) {
		admin.setPassword(SafeUtil.encode(admin.getPassword()));
		adminService.update(admin);
		return "redirect:adminList?flag=6&page="+page;
	}

	/**
	 * 管理员删除
	 * 
	 * @return
	 */
	@RequestMapping("/adminDelete")
	public String adminDelete(Admins admin, 
			@RequestParam(required=false, defaultValue="1") int page) {
		adminService.delete(admin);
		return "redirect:adminList?flag=6&page="+page;
	}

}

 

用户相关接口:

/**
 * 用户相关接口
 */
@Controller
@RequestMapping("/index")
public class UserController{
	
	@Resource
	private UserService userService;
	@Resource
	private OrderService orderService;
	@Resource
	private GoodService goodService;
	@Resource
	private TypeService typeService;
	@Resource
	private ShopcartService shopcartService;
	@Resource
	private SkuService skuService;

	
	/**
	 * 用户注册
	 * @return
	 */
	@GetMapping("/register")
	public String reg(Model model) {
		model.addAttribute("flag", -1); // 注册页面
		return "/index/register.jsp";
	}
	
	/**
	 * 用户注册
	 * @return
	 */
	@PostMapping("/register")
	public String register(Users user, Model model){
		if (user.getUsername().isEmpty()) {
			model.addAttribute("msg", "用户名不能为空!");
			return "/index/register.jsp";
		}else if (userService.isExist(user.getUsername())) {
			model.addAttribute("msg", "用户名已存在!");
			return "/index/register.jsp";
		}else {
			String password = user.getPassword();
			userService.add(user);
			user.setPassword(password);
			return "/index/index"; // 注册成功后转去登录
		}
	}
	
	/**
	 * 用户登录
	 * @return
	 */
	@GetMapping("/login")
	public String log() {
		return "/index/index";
	}
	
	/**
	 * 用户登录
	 * @return
	 */
	@PostMapping("/login")
	public String login(@RequestParam(required=false, defaultValue="0")int flag, Users user, HttpSession session, Model model) {
		model.addAttribute("typeList", typeService.getList());
		if(flag==-1) {
			flag = 6; // 登录页面
			return "/index/index";
		}
		if(userService.checkUser(user.getUsername(), user.getPassword())){
			Users loginUser = userService.get(user.getUsername());
			session.setAttribute("user", loginUser);
			// 还原购物车数量
			session.setAttribute("total", shopcartService.getTotal(loginUser.getId()));
			return "redirect:index";
		} else {
			model.addAttribute("msg", "用户名或密码错误!");
			return "/index/index";
		}
	}

	/**
	 * 注销登录
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("user");
		session.removeAttribute("order");
		return "/index/index";
	}
	
	/**
	 * 查看购物车
	 * @return
	 */
	@RequestMapping("/shopcart")
	public String shopcart(Model model, HttpSession session) {
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			model.addAttribute("msg", "请先登录!");
			return "/index/index";
		}
		model.addAttribute("typeList", typeService.getList());
		model.addAttribute("shopcartList", shopcartService.getList(user.getId()));
		model.addAttribute("totalPrice", shopcartService.getTotalPrice(user.getId()));
		return "/index/shopcart.jsp";
	}
	
	/**
	 * 购买
	 * @return
	 */
	@RequestMapping("/buy")
	public @ResponseBody int buy(Shopcart shopcart, HttpSession session, Model model){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return -111;
		}
		shopcart.setUserId(user.getId());
		shopcart.setGood(goodService.get(shopcart.getGoodId()));
		// 验证库存
//		int stock = skuService.getStock(shopcart.getGoodId(), shopcart.getColorId(), shopcart.getSizeId());
//		if(shopcart.getAmount() > stock) {
//			model.addAttribute("msg", "商品 [ " + shopcart.getGood().getName() + " ] 库存不足! 当前库存只有: " + stock);
//		}
		return orderService.save(Arrays.asList(shopcart), user);
	}
	
	/**
	 * 购买
	 * @return
	 */
	@RequestMapping("/cart")
	public @ResponseBody int cart(Shopcart shopcart, HttpSession session, Model model){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return -111;
		}
		shopcart.setUserId(user.getId());
		shopcartService.save(shopcart);
		int total = shopcartService.getTotal(user.getId());
		session.setAttribute("total", total);
		return total;
	}
	
	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public @ResponseBody boolean add(int skuid, HttpSession session){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return false;
		}
		return shopcartService.add(skuid);
	}
	
	/**
	 * 减少
	 */
	@RequestMapping("/less")
	public @ResponseBody boolean less(int skuid, HttpSession session){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return false;
		}
		return shopcartService.less(skuid);
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public @ResponseBody boolean delete(int skuid, HttpSession session){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return false;
		}
		shopcartService.delete(skuid);
		session.setAttribute("total", shopcartService.getTotal(user.getId()));
		return true;
	}
	
	/**
	 * 总金额
	 * @return
	 */
	@RequestMapping("/total")
	public @ResponseBody int total(HttpSession session){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			return -111;
		}
		return shopcartService.getTotalPrice(user.getId());
	}
	
	
	/**
	 * 提交订单
	 * @return
	 */
	@RequestMapping("/save")
	public String save(ServletRequest request, HttpSession session, Model model){
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			model.addAttribute("msg", "请先登录!");
			return "/index/index";
		}
		List<Shopcart> shopcartList = shopcartService.getList(user.getId());
		if(Objects.isNull(shopcartList) || shopcartList.isEmpty()) {
			model.addAttribute("msg", "购物车没有商品");
			return shopcart(model, session);
		}
		// 验证库存
		for(Shopcart cart : shopcartList) {
			int stock = skuService.getStock(cart.getGoodId(), cart.getColorId(), cart.getSizeId());
			if(cart.getAmount() > stock) {
				model.addAttribute("msg", "商品 [ " + cart.getGood().getName() + " ] 库存不足! 当前库存只有: " + stock);
				return shopcart(model, session);
			}
		}
		int orderid = orderService.save(shopcartList, user);
		if(orderid > 0) {
			// 清空购物车
			session.setAttribute("total", shopcartService.getTotal(user.getId()));
			// 跳转支付
			return "redirect:topay?orderid="+orderid;
		} 

		model.addAttribute("msg", "出了点问题");
		return shopcart(model, session);
	}
	
	/**
	 * 支付页面
	 * @return
	 */
	@RequestMapping("/topay")
	public String topay(int orderid, ServletRequest request, HttpSession session) {
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			request.setAttribute("msg", "请先登录!");
			return "/index/index";
		}
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("order", orderService.get(orderid));
		return "/index/pay.jsp";
	}
	
	/**
	 * 支付(模拟)
	 * @return
	 */
	@RequestMapping("/pay")
	public String pay(Orders order, ServletRequest request, HttpSession session) {
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			request.setAttribute("msg", "请先登录!");
			return "/index/index";
		}
		// 模拟支付
		orderService.pay(order);
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("order", orderService.get(order.getId()));
		request.setAttribute("msg", "支付成功! 即将跳转到订单列表");
		return "/index/pay.jsp";
	}
	
	/**
	 * 查看订单
	 * @return
	 */
	@RequestMapping("/order")
	public String order(HttpSession session, Model model){
		model.addAttribute("flag", 12);
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			model.addAttribute("msg", "请登录后查看订单!");
			return "/index/index";
		}
		model.addAttribute("typeList", typeService.getList());
		model.addAttribute("orderList", orderService.getListByUserid(user.getId()));
		return "/index/order.jsp";
	}
	
	
	/**
	 * 个人信息
	 * @return
	 */
	@RequestMapping("/my")
	public String my(HttpSession session, Model model){
		model.addAttribute("flag", 11);
		model.addAttribute("typeList", typeService.getList());
		Users user = (Users) session.getAttribute("user");
		if (user == null) {
			model.addAttribute("msg", "请先登录!");
			return "/index/index";
		}
		model.addAttribute("user", user);
		return "/index/my.jsp";
	}
	
	
	/**
	 * 修改信息
	 * @return
	 */
	@RequestMapping("/updateUser")
	public String updateUser(Users user, HttpSession session, Model model){
		model.addAttribute("flag", 11);
		model.addAttribute("typeList", typeService.getList());
		Users userLogin = (Users) session.getAttribute("user");
		if (userLogin == null) {
			model.addAttribute("msg", "请先登录!");
			return "/index/index";
		}
		// 修改资料
		Users u = userService.get(userLogin.getId());
		u.setName(user.getName());
		u.setPhone(user.getPhone());
		u.setAddress(user.getAddress());
		userService.update(u);  // 更新数据库
		session.setAttribute("user", u); // 更新session
		model.addAttribute("msg", "信息修改成功!");
		return "/index/my.jsp";
	}
	
	
	/**
	 * 修改信息
	 * @return
	 */
	@RequestMapping("/updatePassword")
	public String updatePassword(Users user, HttpSession session, Model model){
		model.addAttribute("flag", 11);
		model.addAttribute("typeList", typeService.getList());
		Users userLogin = (Users) session.getAttribute("user");
		if (userLogin == null) {
			model.addAttribute("msg", "请先登录!");
			return "/index/index";
		}
		// 修改密码
		Users u = userService.get(userLogin.getId());
		if(user.getPasswordNew()!=null && !user.getPasswordNew().trim().isEmpty()) {
			if (user.getPassword()!=null && !user.getPassword().trim().isEmpty() 
					&& SafeUtil.encode(user.getPassword()).equals(u.getPassword())) {
				if (user.getPasswordNew()!=null && !user.getPasswordNew().trim().isEmpty()) {
					u.setPassword(SafeUtil.encode(user.getPasswordNew()));
				}
				userService.update(u);  // 更新数据库
				session.setAttribute("user", u); // 更新session
				model.addAttribute("msg", "密码修改成功!");
				return "redirect:logout";
			}else {
				model.addAttribute("msg", "原密码错误!");
			}
		}
		return "/index/index";
	}
	
}

前台相关接口:

/**
 * 前台相关接口
 */
@Controller
@RequestMapping("/index")
public class IndexController{
	
	private static final int rows = 8; // 默认每页数量

	@Autowired
	private TopService topService;
	@Autowired
	private GoodService goodService;
	@Autowired
	private TypeService typeService;
	@Autowired
	private SkuService skuService;
	

	/**
	 * 首页
	 * @return
	 */
	@RequestMapping("/index")
	public String index(HttpServletRequest request){
		request.setAttribute("flag", -1);
		List<Types> typeList = typeService.getList();
		request.setAttribute("typeList", typeList);
		List<Map<String, Object>> dataList = new ArrayList<>();
		for(Types type : typeList) {
			Map<String, Object> map = new HashMap<>();
			map.put("type", type);
			map.put("goodList", goodService.getListByType(type.getId(), 1, 8)); // 取前8个
			dataList.add(map);
		}
		request.setAttribute("dataList", dataList);
		return "/index/index.jsp";
	}
	
	/**
	 * 推荐列表
	 * @return
	 */
	@RequestMapping("/top")
	public String tops(int typeid, @RequestParam(required=false, defaultValue="1")int page, HttpServletRequest request) {
		request.setAttribute("flag", typeid==2 ? 7 : 8);
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("goodList", goodService.getList(typeid, page, rows));
		request.setAttribute("pageHtml", PageUtil.getPageHtml(request, goodService.getTotal(typeid), page, rows));
		return "/index/goods.jsp";
	}
	
	/**
	 * 商品列表
	 * @return
	 */
	@RequestMapping("/goods")
	public String goods(int typeid, @RequestParam(required=false, defaultValue="1")int page, HttpServletRequest request){
		request.setAttribute("flag", typeid);
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("topList", topService.getList(Tops.TYPE_SUPPER, 1, 4));
		if (typeid > 0) {
			request.setAttribute("type", typeService.get(typeid));
		}
		request.setAttribute("goodList", goodService.getListByType(typeid, page, rows));
		request.setAttribute("pageHtml", PageUtil.getPageHtml(request, goodService.getTotalByType(typeid), page, rows));
		return "/index/goods.jsp";
	}
	
	/**
	 * 商品详情
	 * @return
	 */
	@RequestMapping("/detail")
	public String detail(int goodid, HttpServletRequest request){
		request.setAttribute("typeList", typeService.getList());
		request.setAttribute("topList", topService.getList(Tops.TYPE_SUPPER, 1, 4));
		Goods good = goodService.get(goodid);
		request.setAttribute("good", good);
		request.setAttribute("type", typeService.get(good.getTypeId()));
		request.setAttribute("colorList", skuService.getColorList(goodid));
		request.setAttribute("sizeList", skuService.getSizeList(goodid));
		return "/index/detail.jsp";
	}
	
	/**
	 * 搜索
	 * @return
	 */
	@RequestMapping("/search")
	public String search(String name, @RequestParam(required=false, defaultValue="1")int page, HttpServletRequest request) {
		if (Objects.nonNull(name) && !name.trim().isEmpty()) {
			request.setAttribute("goodList", goodService.getListByName(name, page, rows));
			request.setAttribute("pageHtml", PageUtil.getPageHtml(request, goodService.getTotalByName(name), page, rows));
		}
		request.setAttribute("typeList", typeService.getList());
		return "/index/goods.jsp";
	}
	
	/**
	 * 查询库存
	 * @param goodid
	 * @param colorid
	 * @param sizeid
	 * @return
	 */
	@GetMapping("/stock")
	public @ResponseBody int stock(int goodid, int colorid, int sizeid) {
		return skuService.getStock(goodid, colorid, sizeid);
	}

}

源码获取:博客首页 "资源" 里下载!



这篇关于Java项目:零食商城系统(java+SSM+jsp+MySQL+EasyUI)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程