基于javaweb+mysql的ssm网上商城系统(java+ssm+jsp+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM网上商城系统(java+ssm+jsp+mysql)
一、项目简述功能
javaweb 网上商城系统,前台+后台管理,用户注册,登录,上哦展示,分组展示,搜索,收货地址管理,购物车管理,添加,购买,个人信息修改。订单查询等等,后台商品管理,分类管理,库存管理,订单管理,用户管理,信息修改等等.
二、项目运行
环境配置: Jdk1.8 + Tomcats . 5 + mysql + Eclispe ( IntelliJ IDEA ,Eclispe , MyEclispe , sts 都支持)
项目技术: JSP + Spring + SpringMVC + MyBatis + html + cSS + Javascript + JQuery + Ajax 等等。
*/
@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
*/
@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);
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());
*/
public static String getPageHtml(HttpServletRequest request, long total, int page, int size){
if (total <= 0) {
return null;
}
// 计算总页数
int pages = (int) (total % size ==0 ? total/size : total /size + 1);
pages = pages == 0 ? 1 : pages;
// 请求地址
String url = request.getRequestURL().toString();
// 请求参数
StringBuilder paramBuilder = new StringBuilder();
Enumeration<String> params = request.getParameterNames();
while (params.hasMoreElements()) {
String param = params.nextElement();
if(param.indexOf("page") > -1) {
continue;
}
paramBuilder.append("&").append(param).append("=").append(request.getParameter(param));
}
// 分页字符串
StringBuilder pageBuilder = new StringBuilder();
pageBuilder.append("<div class='page text-center clearfix'><span class='pro_link'>");
// 上一页
if (page <= 1) { // 如果已经是第一页, 上一页按钮disabled
pageBuilder.append("<a href='javascript:;' title='已是首页'><<</a>");
pageBuilder.append("<a href='javascript:;' title='已是首页'><</a>");
}else{
pageBuilder.append("<a title='首页' href='").append(url).append("?").append("page=").append(page-1)
.append(paramBuilder).append("'><<</a>");
pageBuilder.append("<a title='上一页' href='").append(url).append("?").append("page=").append(page-1)
.append(paramBuilder).append("'><</a>");
}
// 中间数字页码
if (pages <= 7) { // 全部显示
for (int i = 1; i <= pages; i++) {
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, i));
}
}else{ // 显示部分
if (page<4 || page>pages-3) { // 1 2 3 ... pages-2 pages-1 pages
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, 1));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, 2));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, 3));
pageBuilder.append(" ... ");
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, pages-2));
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);
}
}
/**
* 后台登录验证拦截器
*/
public class AdminInterceptor extends HandlerInterceptorAdapter{
/**
* 检测登录状态
*/
@Override
public boolean preHandle(HttpServletRequest request,
/**
* 类目列表
*
* @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;
}
/**
* 类目更新
* 用户相关接口
*/
@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
*/
@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";
@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";
}
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) {
} 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) {
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";
}
/**
* 类目添加
@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;
}
/**
* 用户相关接口
*/
@Controller
@RequestMapping("/index")
public class UserController{
@Resource
private UserService userService;
@Resource
private OrderService orderService;
@Resource
private GoodService goodService;
/**
* 产品列表
*
* @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);
buf.append("<a class='btn btn-info' disabled >尾页</a>\n");
}else {
buf.append("<a class='btn btn-info' href='").append(url).append("?page=").append(pages).append(queryString).append("'>尾页</a>\n");
}
buf.append("<input type='text' class='form-control' style='display:inline;width:60px;' value=''/>");
buf.append("<a class='btn btn-info' href='javascript:void(0);' onclick='location.href=\"").append(url).append("?page=").append("\"+(this.previousSibling.value)+\"").append(queryString).append("\"'>GO</a>\n");
buf.append("</div>\n");
return buf.toString();
}
}
/**
* 前台相关接口
*/
@Controller
@RequestMapping("/index")
public class IndexController{
private static final int rows = 8; // 默认每页数量
@Autowired
private TopService topService;
@Autowired
@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");
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")