基于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 等等。
long pages = total % size ==0 ? total/size : total /size + 1;
pages = pages==0 ? 1 : pages;
String url = request.getRequestURL().toString();
StringBuilder queryString = new StringBuilder();
Enumeration<String> enumeration = request.getParameterNames();
try { // 拼装请求参数
while (enumeration.hasMoreElements()) {
String element = (String) enumeration.nextElement();
if(!element.contains("page")) { // 跳过page参数
queryString.append("&").append(element).append("=").append(java.net.URLEncoder.encode(request.getParameter(element),"UTF-8"));
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 拼装分页代码
StringBuilder buf = new StringBuilder();
buf.append("<div style='text-align:center;'>\n");
if (page <= 1) {
buf.append("<a class='btn btn-info' disabled >首页</a>\n");
}else{
buf.append("<a class='btn btn-info' href='").append(url).append("?page=").append(1).append(queryString).append("'>首页</a>\n");
}
if (page <= 1) {
buf.append("<a class='btn btn-info' disabled >上一页</a>\n");
}else {
buf.append("<a class='btn btn-info' href='").append(url).append("?page=").append(page>1 ? page-1 : 1).append(queryString).append("'>上一页</a>\n");
}
buf.append("<h2 style='display:inline;'>[").append(page).append("/").append(pages).append("]</h2>\n");
buf.append("<h2 style='display:inline;'>[").append(total).append("]</h2>\n");
if (page >= pages) {
buf.append("<a class='btn btn-info' disabled >下一页</a>\n");
}else {
buf.append("<a class='btn btn-info' href='").append(url).append("?page=").append(page<pages ? page+1 : pages).append(queryString).append("'>下一页</a>\n");
}
if (page >= pages) {
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();
/**
* 删除
*/
@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);
}
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);
/**
* 用户相关接口
*/
@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
*/
@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";
}
/**
* 类目列表
*
*/
@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
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 "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")
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) {
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";
}
}
/**
* 分页工具类
*/
public class PageUtil {
/**
* 获取分页代码
* @param total 总记录数
* @param page 当前页面
* @param size 每页数量
* @return
*/
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);
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 {
* @param i
* @return
*/
private static String packPageItem(String url, String params, int page, int i) {
StringBuilder pageBuilder = new StringBuilder();
if (i == page) {
pageBuilder.append("<a class='on'>").append(i).append("</a>");
}else{
pageBuilder.append("<a class='num' title='第").append(i).append("页' href='").append(url).append("?").append("page=").append(i)
.append(params).append("'>");pageBuilder.append(i).append("</a>");
}
return pageBuilder.toString();
}
/**
* 获取分页代码
* @param total 总记录数
* @param page 当前页面
* @param size 每页数量
* @return
*/
public static String getPageTool(HttpServletRequest request, long total, int page, int size){
long pages = total % size ==0 ? total/size : total /size + 1;
pages = pages==0 ? 1 : pages;
String url = request.getRequestURL().toString();
StringBuilder queryString = new StringBuilder();
Enumeration<String> enumeration = request.getParameterNames();
try { // 拼装请求参数
while (enumeration.hasMoreElements()) {
String element = (String) enumeration.nextElement();
if(!element.contains("page")) { // 跳过page参数
queryString.append("&").append(element).append("=").append(java.net.URLEncoder.encode(request.getParameter(element),"UTF-8"));
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 拼装分页代码
StringBuilder buf = new StringBuilder();
buf.append("<div style='text-align:center;'>\n");
if (page <= 1) {
buf.append("<a class='btn btn-info' disabled >首页</a>\n");
@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);
/**
* 封装分页项
* @param url
* @param params
* @param page
* @param i
* @return
*/
private static String packPageItem(String url, String params, int page, int i) {
StringBuilder pageBuilder = new StringBuilder();
if (i == page) {
pageBuilder.append("<a class='on'>").append(i).append("</a>");
}else{
pageBuilder.append("<a class='num' title='第").append(i).append("页' href='").append(url).append("?").append("page=").append(i)
.append(params).append("'>");pageBuilder.append(i).append("</a>");
}
return pageBuilder.toString();
}
/**
* 获取分页代码
* @param total 总记录数
* @param page 当前页面
* @param size 每页数量
* @return
*/
public static String getPageTool(HttpServletRequest request, long total, int page, int size){
long pages = total % size ==0 ? total/size : total /size + 1;
pages = pages==0 ? 1 : pages;
String url = request.getRequestURL().toString();
StringBuilder queryString = new StringBuilder();
Enumeration<String> enumeration = request.getParameterNames();
try { // 拼装请求参数
while (enumeration.hasMoreElements()) {
String element = (String) enumeration.nextElement();
if(!element.contains("page")) { // 跳过page参数
queryString.append("&").append(element).append("=").append(java.net.URLEncoder.encode(request.getParameter(element),"UTF-8"));
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
// 拼装分页代码
StringBuilder buf = new StringBuilder();
buf.append("<div style='text-align:center;'>\n");
* @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);
}
}
/**
* 后台登录验证拦截器
/**
* 提交订单
* @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";
}
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));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, pages-1));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, pages));
}else{ // 1 2 ... page-1 page page+1 ... pages-1 pages
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, 1));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, 2));
pageBuilder.append(" ... ");
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, page-1));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, page));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, page+1));
pageBuilder.append(" ... ");
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, pages-1));
pageBuilder.append(packPageItem(url, paramBuilder.toString(), page, pages));