基于javaweb+springboot的网上商城系统设计和实现(java+Springboot+ssm+mysql+jsp+maven)
一、项目简述
本系统主要实现的功能有: OnlineMall项目是一套电商系统,包括OnlineMall商城系 统及OnlineMall商城后台管理^统,基于Spring Boot 2.X 及相关技术栈开发。前台商城系统包含首页门户、商品分 类、新品上线、首页轮播、商品推荐、商品搜索、商品展 示、购物车、订单结算、订单流程、个人订单管理、会员 中心、帮助中心等模块。后台管理系统包含数据面板、轮 播图管理、商品管理、订单管理、会员管理、分类管理、 设置等模块。
二、项目运行
环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)
项目技术: Springboot+ SpringMVC + MyBatis + ThymeLeaf + JavaScript + JQuery + Ajax + maven等等
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
String acceptHeader = req.getHeader("Accept");
String xRequestedWith = req.getHeader("X-Requested-With");
if ((contentTypeHeader != null && contentTypeHeader.contains("application/json"))
|| (acceptHeader != null && acceptHeader.contains("application/json"))
|| "XMLHttpRequest".equalsIgnoreCase(xRequestedWith)) {
return result;
} else {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", e.getMessage());
modelAndView.addObject("url", req.getRequestURL());
modelAndView.addObject("stackTrace", e.getStackTrace());
modelAndView.addObject("author", "十三");
modelAndView.addObject("ltd", "新峰商城");
modelAndView.setViewName("error/error");
return modelAndView;
}
}
}
管理员业务控制层:
/**
properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
properties.put("kaptcha.textproducer.char.space", "2");
properties.put("kaptcha.textproducer.char.length", "6");
Config config = new Config(properties);
newBeeMallLoginKaptcha.setConfig(config);
byte[] captchaOutputStream = null;
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String verifyCode = newBeeMallLoginKaptcha.createText();
@Autowired
private DefaultKaptcha captchaProducer;
@GetMapping("/common/kaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
byte[] captchaOutputStream = null;
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String verifyCode = captchaProducer.createText();
httpServletRequest.getSession().setAttribute("verifyCode", verifyCode);
BufferedImage challenge = captchaProducer.createImage(verifyCode);
ImageIO.write(challenge, "jpg", imgOutputStream);
} catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
if (e instanceof NewBeeMallException) {
result.setMessage(e.getMessage());
} else {
e.printStackTrace();
result.setMessage("未知异常,请联系管理员");
}
//检查请求是否为ajax, 如果是 ajax 请求则返回 Result json串, 如果不是 ajax 请求则返回 error 视图
String contentTypeHeader = req.getHeader("Content-Type");
String acceptHeader = req.getHeader("Accept");
String xRequestedWith = req.getHeader("X-Requested-With");
if ((contentTypeHeader != null && contentTypeHeader.contains("application/json"))
|| (acceptHeader != null && acceptHeader.contains("application/json"))
|| "XMLHttpRequest".equalsIgnoreCase(xRequestedWith)) {
return result;
} else {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", e.getMessage());
modelAndView.addObject("url", req.getRequestURL());
modelAndView.addObject("stackTrace", e.getStackTrace());
modelAndView.addObject("author", "十三");
modelAndView.addObject("ltd", "新峰商城");
modelAndView.setViewName("error/error");
return "admin/login";
}
@GetMapping({"/test"})
public String test() {
return "admin/test";
}
@GetMapping({"", "/", "/index", "/index.html"})
public String index(HttpServletRequest request) {
request.setAttribute("path", "index");
request.setAttribute("categoryCount", 0);
return "admin/index";
}
@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
return "admin/login";
}
}
@GetMapping("/profile")
public String profile(HttpServletRequest request) {
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
if (adminUser == null) {
return "admin/login";
}
request.setAttribute("path", "profile");
request.setAttribute("loginUserName", adminUser.getLoginUserName());
*/
@RestControllerAdvice
public class NewBeeMallExceptionHandler {
@ExceptionHandler(Exception.class)
public Object handleException(Exception e, HttpServletRequest req) {
Result result = new Result();
result.setResultCode(500);
//区分是否为自定义异常
if (e instanceof NewBeeMallException) {
result.setMessage(e.getMessage());
} else {
e.printStackTrace();
result.setMessage("未知异常,请联系管理员");
}
//检查请求是否为ajax, 如果是 ajax 请求则返回 Result json串, 如果不是 ajax 请求则返回 error 视图
String contentTypeHeader = req.getHeader("Content-Type");
String acceptHeader = req.getHeader("Accept");
String xRequestedWith = req.getHeader("X-Requested-With");
request.setAttribute("linkCount", 0);
request.setAttribute("tagCount", 0);
request.setAttribute("commentCount", 0);
request.setAttribute("path", "index");
return "admin/index";
}
@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
*/
@Controller
@RequestMapping("/admin")
public class AdminController {
@Resource
private AdminUserService adminUserService;
@GetMapping({"/login"})
public String login() {
return "admin/login";
}
@GetMapping({"/test"})
public String test() {
return "admin/test";
responseOutputStream.flush();
responseOutputStream.close();
}
@GetMapping("/common/mall/kaptcha")
public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
com.google.code.kaptcha.impl.DefaultKaptcha newBeeMallLoginKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "27,174,171");
properties.put("kaptcha.noise.color", "20,33,42");
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.image.width", "110");
properties.put("kaptcha.image.height", "40");
properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
properties.put("kaptcha.textproducer.char.space", "2");
properties.put("kaptcha.textproducer.char.length", "6");
Config config = new Config(properties);
newBeeMallLoginKaptcha.setConfig(config);
byte[] captchaOutputStream = null;
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
captchaOutputStream = imgOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
public String login() {
return "admin/login";
}
@GetMapping({"/test"})
public String test() {
return "admin/test";
}
@GetMapping({"", "/", "/index", "/index.html"})
public String index(HttpServletRequest request) {
request.setAttribute("path", "index");
request.setAttribute("categoryCount", 0);
request.setAttribute("blogCount", 0);
request.setAttribute("linkCount", 0);
request.setAttribute("tagCount", 0);
request.setAttribute("commentCount", 0);
request.setAttribute("path", "index");
return "admin/index";
}
}
}
全局异常处理控制层:
/**
* 全局异常处理
*/
@RestControllerAdvice
public class NewBeeMallExceptionHandler {
@ExceptionHandler(Exception.class)
public Object handleException(Exception e, HttpServletRequest req) {
Result result = new Result();
result.setResultCode(500);
//区分是否为自定义异常
if (e instanceof NewBeeMallException) {
result.setMessage(e.getMessage());
} else {
e.printStackTrace();
result.setMessage("未知异常,请联系管理员");
}
return "admin/index";
}
@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
@RequestParam("nickName") String nickName) {
if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
return "参数不能为空";
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
return ServiceResultEnum.SUCCESS.getResult();
} else {
return "修改失败";
}
}
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "admin/login";
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "admin/login";
}
}
验证码校验控制层:
/**
* 验证码校验控制层
*/
@Controller
public class CommonController {
@Autowired
private DefaultKaptcha captchaProducer;
@GetMapping("/common/kaptcha")
public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
byte[] captchaOutputStream = null;
ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
try {
//生产验证码字符串并保存到session中
String verifyCode = captchaProducer.createText();
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
return "admin/login";
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", "登陆失败,请联系作者获得测试账号");
}
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
if (adminUserService.updateName(loginUserId, loginUserName, nickName)) {
return ServiceResultEnum.SUCCESS.getResult();
} else {
return "修改失败";
}
}
@GetMapping("/logout")
public String logout(HttpServletRequest request) {
request.getSession().removeAttribute("loginUserId");
request.getSession().removeAttribute("loginUser");
request.getSession().removeAttribute("errorMsg");
return "admin/login";
}
}
验证码校验控制层:
/**
* 验证码校验控制层
*/
@Controller
public class CommonController {
@Autowired
private DefaultKaptcha captchaProducer;
String contentTypeHeader = req.getHeader("Content-Type");
String acceptHeader = req.getHeader("Accept");
String xRequestedWith = req.getHeader("X-Requested-With");
if ((contentTypeHeader != null && contentTypeHeader.contains("application/json"))
|| (acceptHeader != null && acceptHeader.contains("application/json"))
|| "XMLHttpRequest".equalsIgnoreCase(xRequestedWith)) {
return result;
} else {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", e.getMessage());
modelAndView.addObject("url", req.getRequestURL());
modelAndView.addObject("stackTrace", e.getStackTrace());
modelAndView.addObject("author", "十三");
modelAndView.addObject("ltd", "新峰商城");
modelAndView.setViewName("error/error");
return modelAndView;
}
}
}
管理员业务控制层:
/**
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", "登陆失败,请联系作者获得测试账号");
return "admin/login";
}
}
@GetMapping("/profile")
public String profile(HttpServletRequest request) {
Integer loginUserId = (int) request.getSession().getAttribute("loginUserId");
AdminUser adminUser = adminUserService.getUserDetailById(loginUserId);
if (adminUser == null) {
return "admin/login";
}
captchaOutputStream = imgOutputStream.toByteArray();
httpServletResponse.setHeader("Cache-Control", "no-store");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}
@RequestParam("password") String password,
@RequestParam("verifyCode") String verifyCode,
HttpSession session) {
if (StringUtils.isEmpty(verifyCode)) {
session.setAttribute("errorMsg", "验证码不能为空");
return "admin/login";
}
if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
session.setAttribute("errorMsg", "用户名或密码不能为空");
return "admin/login";
}
String kaptchaCode = session.getAttribute("verifyCode") + "";
if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
session.setAttribute("errorMsg", "验证码错误");
}
AdminUser adminUser = adminUserService.login(userName, password);
if (adminUser != null) {
session.setAttribute("loginUser", adminUser.getNickName());
session.setAttribute("loginUserId", adminUser.getAdminUserId());
//session过期时间设置为7200秒 即两小时
//session.setMaxInactiveInterval(60 * 60 * 2);
return "redirect:/admin/index";
} else {
session.setAttribute("errorMsg", "登陆失败,请联系作者获得测试账号");
return "admin/login";
properties.put("kaptcha.noise.color", "20,33,42");
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.image.width", "110");
properties.put("kaptcha.image.height", "40");
properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
properties.put("kaptcha.textproducer.char.space", "2");
properties.put("kaptcha.textproducer.char.length", "6");
Config config = new Config(properties);
newBeeMallLoginKaptcha.setConfig(config);
byte[] captchaOutputStream = null;
request.setAttribute("categoryCount", 0);
request.setAttribute("blogCount", 0);
request.setAttribute("linkCount", 0);
request.setAttribute("tagCount", 0);
request.setAttribute("commentCount", 0);
request.setAttribute("path", "index");
return "admin/index";
}
@PostMapping(value = "/login")
public String login(@RequestParam("userName") String userName,
@RequestParam("password") String password,
httpServletResponse.setDateHeader("Expires", 0);
httpServletResponse.setContentType("image/jpeg");
ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
responseOutputStream.write(captchaOutputStream);
responseOutputStream.flush();
responseOutputStream.close();
}
@GetMapping("/common/mall/kaptcha")
public void mallKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
com.google.code.kaptcha.impl.DefaultKaptcha newBeeMallLoginKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
properties.put("kaptcha.border", "no");
properties.put("kaptcha.textproducer.font.color", "27,174,171");
properties.put("kaptcha.noise.color", "20,33,42");
properties.put("kaptcha.textproducer.font.size", "30");
properties.put("kaptcha.image.width", "110");
properties.put("kaptcha.image.height", "40");
properties.put("kaptcha.session.key", Constants.MALL_VERIFY_CODE_KEY);
properties.put("kaptcha.textproducer.char.space", "2");
properties.put("kaptcha.textproducer.char.length", "6");
Config config = new Config(properties);
newBeeMallLoginKaptcha.setConfig(config);
byte[] captchaOutputStream = null;