基于javaweb+mysql的ssm电商平台网上超市在线购物商城系统(java+ssm+javascript+jsp+mysql+tomcat)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM电商平台网上超市在线购物商城系统(java+ssm+javascript+jsp+mysql+tomcat)
前台用户:
[email protected] 123456
[email protected] 123456
后台管理员:
admin 123456
adminGoodsDao.deleteGoods(list);
model.addAttribute("msg", "成功删除商品!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
/**
* 删除一个商品
*/
@Override
public String deleteAGoods(Integer id, Model model) {
//商品有关联
if(adminGoodsDao.selectCartGoods(id).size() > 0 ||
adminGoodsDao.selectFocusGoods(id).size() > 0 ||
adminGoodsDao.selectOrderdetailGoods(id).size() > 0) {
model.addAttribute("msg", "商品有关联,不允许删除!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
adminGoodsDao.deleteAGoods(id);
model.addAttribute("msg", "成功删除商品!");
return "forward:/adminGoods/selectGoods?act=deleteSelect";
}
}
/**
* 验证码
*/
@Controller
public class ValidateCodeController {
private char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',
'3', '4', '5', '6', '7', '8', '9' };
private static final int WIDTH = 50;
private static final int HEIGHT = 20;
private static final int LENGTH = 4;
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
public String selectGoods(Model model, Integer pageCur, String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
/**
* add页面初始化
*/
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model){
model.addAttribute("goods", new Goods());
return "admin/addGoods";
}
/**
* 添加与修改
*/
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods, HttpServletRequest request, String updateAct){
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
/**
* 查询一个名片
*/
@RequestMapping("/selectAGoods")
public String selectAGoods(Model model, Integer id, String act){
return adminGoodsService.selectAGoods(model, id, act);
}
/**
* 删除多个商品
*/
@RequestMapping("/deleteGoods")
public String deleteGoods(Integer ids[], Model model) {
return adminGoodsService.deleteGoods(ids, model);
}
@Controller
@RequestMapping("/adminGoods")
public class AdminGoodsController extends BaseController{
@Autowired
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
public String selectGoods(Model model, Integer pageCur, String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
/**
* add页面初始化
*/
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model){
model.addAttribute("goods", new Goods());
return "admin/addGoods";
}
/**
* 添加与修改
*/
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods, HttpServletRequest request, String updateAct){
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
/**
* 查询一个名片
*/
@RequestMapping("/selectAGoods")
public String selectAGoods(Model model, Integer id, String act){
return adminGoodsService.selectAGoods(model, id, act);
}
/**
* 删除多个商品
*/
@RequestMapping("/deleteGoods")
public String deleteGoods(Integer ids[], Model model) {
return adminGoodsService.deleteGoods(ids, model);
}
/**
* 删除单个商品
*/
@RequestMapping("/deleteAGoods")
public String deleteAGoods(Integer id, Model model) {
return adminGoodsService.deleteAGoods(id, model);
}
}
model.addAttribute("myOrderDetail", userCenterDao.orderDetail(ordersn));
return "before/userOrderDetail";
}
}
@Controller
public class AdminController {
@Autowired
private AdminService adminService;
@RequestMapping("/admin")
public String toLogin(@ModelAttribute Auser auser) {
return "admin/login";
}
@RequestMapping("/admin/login")
public String login(@ModelAttribute Auser auser, Model model, HttpSession session) {
return adminService.login(auser, model, session);
}
@RequestMapping("/exit")
public String exit(@ModelAttribute Auser auser,HttpSession session) {
session.invalidate();
return "admin/login";
}
}
@Controller
@Service("indexService")
@Transactional
public class IndexServiceImpl implements IndexService{
@Autowired
private IndexDao indexDao;
@Autowired
private AdminTypeDao adminTypeDao;
@Autowired
private AdminNoticeDao adminNoticeDao;
@Override
public String before(Model model, HttpSession session, Goods goods) {
session.setAttribute("goodsType", adminTypeDao.selectGoodsType());
model.addAttribute("salelist", indexDao.getSaleOrder());
model.addAttribute("focuslist", indexDao.getFocusOrder());
model.addAttribute("noticelist", indexDao.selectNotice());
if(goods.getId() == null)
goods.setId(0);
model.addAttribute("lastedlist", indexDao.getLastedGoods(goods));
return "before/index";
}
@Override
public String toRegister(Model model) {
model.addAttribute("rbuser", new Buser());
return "before/register";
}
@Override
public String toLogin(Model model) {
model.addAttribute("lbuser", new Buser());
return "before/login";
}
@Override
public String goodsDetail(Model model, Integer id) {
Goods goods = indexDao.selectGoodsById(id);
model.addAttribute("goods", goods);
return "before/goodsdetail";
}
@Override
public String selectANotice(Model model, Integer id) {
Notice notice = adminNoticeDao.selectANotice(id);
model.addAttribute("notice", notice);
return "admin/noticeDetail";
}
private AdminGoodsService adminGoodsService;
@RequestMapping("/selectGoods")
public String selectGoods(Model model, Integer pageCur, String act) {
return adminGoodsService.selectGoods(model, pageCur, act);
}
/**
* add页面初始化
*/
@RequestMapping("/toAddGoods")
public String toAddGoods(Model model){
model.addAttribute("goods", new Goods());
return "admin/addGoods";
}
/**
* 添加与修改
*/
@RequestMapping("/addGoods")
public String addGoods(@ModelAttribute Goods goods, HttpServletRequest request, String updateAct){
return adminGoodsService.addOrUpdateGoods(goods, request, updateAct);
}
/**
* 查询一个名片
*/
@RequestMapping("/selectAGoods")
public String selectAGoods(Model model, Integer id, String act){
return adminGoodsService.selectAGoods(model, id, act);
}
/**
* 删除多个商品
*/
@RequestMapping("/deleteGoods")
public String deleteGoods(Integer ids[], Model model) {
return adminGoodsService.deleteGoods(ids, model);
}
/**
* 删除单个商品
*/
@RequestMapping("/deleteAGoods")
public String deleteAGoods(Integer id, Model model) {
return adminGoodsService.deleteAGoods(id, model);
}
}
/**
* 验证码
*/
@Controller
public class ValidateCodeController {
private char code[] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M',
'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2',
'3', '4', '5', '6', '7', '8', '9' };
private static final int WIDTH = 50;
private static final int HEIGHT = 20;
private static final int LENGTH = 4;
@RequestMapping("/validateCode")
public void validateCode(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// 设置响应报头信息
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 设置响应的MIME类型
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18);
Graphics g = image.getGraphics();
Random rd = new Random();
// 设置背景颜色
g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd
.nextInt(55) + 200));
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
}else{
return "card/addCard";
}
}
}
/**
* 查询商品
*/
@Override
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
int temp = allGoods.size();
model.addAttribute("totalCount", temp);
int totalPage = 0;
if (temp == 0) {
totalPage = 0;//总页数
} else {
//返回大于或者等于指定表达式的最小整数
totalPage = (int) Math.ceil((double) temp / 10);
}
if (pageCur == null) {
pageCur = 1;
}
if ((pageCur - 1) * 10 > temp) {
pageCur = pageCur - 1;
}
//分页查询
Map<String, Object> map = new HashMap<String, Object>();
map.put("startIndex", (pageCur - 1) * 10);//起始位置
map.put("perPageSize", 10);//每页10个
allGoods = adminGoodsDao.selectAllGoodsByPage(map);
model.addAttribute("allGoods", allGoods);
model.addAttribute("totalPage", totalPage);
// 设置响应的MIME类型
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18);
Graphics g = image.getGraphics();
Random rd = new Random();
// 设置背景颜色
g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd
.nextInt(55) + 200));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 设置字体
g.setFont(mFont);
// 画边框
g.setColor(Color.black);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
// 随机产生的验证码
String result = "";
for (int i = 0; i < LENGTH; ++i) {
result += code[rd.nextInt(code.length)];
}
HttpSession se = request.getSession();
se.setAttribute("code", result);
// 画验证码
for (int i = 0; i < result.length(); i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
g.drawString(result.charAt(i) + "", 12 * i + 1, 16);
}
// 随机产生2个干扰线
for (int i = 0; i < 2; i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
int x1 = rd.nextInt(WIDTH);
int x2 = rd.nextInt(WIDTH);
public class CartServiceImpl implements CartService{
@Autowired
private CartDao cartDao;
@Override
public String focus(Model model, Integer id, HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uid", MyUtil.getUserId(session));
map.put("gid", id);
List<Map<String, Object>> list = cartDao.isFocus(map);
if(list.size() > 0) {
model.addAttribute("msg", "已关注该商品!");
}else {
int n = cartDao.focus(map);
if(n > 0)
model.addAttribute("msg", "成功关注该商品!");
else
model.addAttribute("msg", "关注失败!");
}
return "forward:/goodsDetail?id=" + id;
}
@Override
public String putCart(Model model, Integer shoppingnum, Integer id, HttpSession session) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uid", MyUtil.getUserId(session));
map.put("gid", id);
map.put("shoppingnum", shoppingnum);
//是否已添加购物车
List<Map<String, Object>> list = cartDao.isPutCart(map);
if(list.size() > 0)
cartDao.updateCart(map);
else
cartDao.putCart(map);
return "forward:/cart/selectCart";
}
@Override
public String selectCart(Model model, HttpSession session) {
List<Map<String, Object>> list = cartDao.selectCart(MyUtil.getUserId(session));
double sum = 0;
for (Map<String, Object> map : list) {
sum = sum + (Double)map.get("smallsum");
}
model.addAttribute("total", sum);
// 随机产生2个干扰线
for (int i = 0; i < 2; i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
int x1 = rd.nextInt(WIDTH);
int x2 = rd.nextInt(WIDTH);
int y1 = rd.nextInt(HEIGHT);
int y2 = rd.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
// 释放图形资源
g.dispose();
try {
OutputStream os = response.getOutputStream();
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Service("cartService")
Font mFont = new Font("Arial", Font.TRUETYPE_FONT, 18);
Graphics g = image.getGraphics();
Random rd = new Random();
// 设置背景颜色
g.setColor(new Color(rd.nextInt(55) + 200, rd.nextInt(55) + 200, rd
.nextInt(55) + 200));
g.fillRect(0, 0, WIDTH, HEIGHT);
// 设置字体
g.setFont(mFont);
// 画边框
g.setColor(Color.black);
g.drawRect(0, 0, WIDTH - 1, HEIGHT - 1);
// 随机产生的验证码
String result = "";
for (int i = 0; i < LENGTH; ++i) {
result += code[rd.nextInt(code.length)];
}
HttpSession se = request.getSession();
se.setAttribute("code", result);
// 画验证码
for (int i = 0; i < result.length(); i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
g.drawString(result.charAt(i) + "", 12 * i + 1, 16);
}
// 随机产生2个干扰线
for (int i = 0; i < 2; i++) {
g.setColor(new Color(rd.nextInt(200), rd.nextInt(200), rd
.nextInt(200)));
int x1 = rd.nextInt(WIDTH);
int x2 = rd.nextInt(WIDTH);
int y1 = rd.nextInt(HEIGHT);
int y2 = rd.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
// 释放图形资源
g.dispose();
try {
OutputStream os = response.getOutputStream();
// 输出图像到页面
ImageIO.write(image, "JPEG", os);
} catch (IOException e) {
e.printStackTrace();
}
}
}
*/
@RequestMapping("/deleteAGoods")
public String deleteAGoods(Integer id, Model model) {
return adminGoodsService.deleteAGoods(id, model);
}
}
@Controller
@RequestMapping("/cart")
public class CartController extends BaseBeforeController{
@Autowired
private CartService cartService;
/**
* 关注商品
*/
@RequestMapping("/focus")
public String focus(Model model,Integer id, HttpSession session) {
return cartService.focus(model, id, session);
}
/**
* 添加购物车
*/
@RequestMapping("/putCart")
public String putCart(Model model,Integer shoppingnum, Integer id, HttpSession session) {
return cartService.putCart(model, shoppingnum, id, session);
}
/**
* 查询购物车
*/
@RequestMapping("/selectCart")
public String selectCart(Model model, HttpSession session) {
return cartService.selectCart(model, session);
}
/**
* 删除购物车
*/
@RequestMapping("/deleteAgoods")
public String deleteAgoods(Integer id,HttpSession session) {
return cartService.deleteAgoods(id, session);
}
/**
model.addAttribute("notice", new Notice());
return "admin/addNotice";
}
@RequestMapping("/addNotice")
public String addNotice(@ModelAttribute Notice notice) {
return adminNoticeService.addNotice(notice);
}
@RequestMapping("/deleteNoticeSelect")
public String deleteNoticeSelect(Model model) {
return adminNoticeService.deleteNoticeSelect(model);
}
@RequestMapping("/selectANotice")
public String selectANotice(Model model, Integer id) {
return adminNoticeService.selectANotice(model, id);
}
@RequestMapping("/deleteNotice")
public String deleteNotice(Integer id) {
return adminNoticeService.deleteNotice(id);
}
}
@Controller
@RequestMapping("/adminType")
public class AdminTypeController extends BaseController{
@Autowired
private AdminTypeService adminTypeService;
/**
* 到添加类型页面
*/
@RequestMapping("/toAddType")
public String toAddType(Model model) {
return adminTypeService.toAddType(model);
}
/**
* 添加类型
*/
@RequestMapping("/addType")
public String addType(String typename,Model model,HttpSession session) {
return adminTypeService.addType(typename, model, session);
}
@Override
public String selectANotice(Model model, Integer id) {
Notice notice = adminNoticeDao.selectANotice(id);
model.addAttribute("notice", notice);
return "admin/noticeDetail";
}
@Override
public String search(Model model, String mykey) {
List<Goods> list = indexDao.search(mykey);
model.addAttribute("searchlist", list);
return "before/searchResult";
}
}
@Service("orderService")
@Transactional
/**
* 订单生成有连串的更新操作,此处必须使用事务管理
*/
public class OrderServiceImpl implements OrderService{
@Autowired
private OrderDao orderDao;
/**
* 订单提交,连续的事务处理
*/
@Override
public String orderSubmit(Model model, HttpSession session, Double amount) {
Order order = new Order();
order.setAmount(amount);
order.setBusertable_id(MyUtil.getUserId(session));
//生成订单,并将主键返回order
orderDao.addOrder(order);
//生成订单详情
Map<String, Object> map = new HashMap<String, Object>();
map.put("ordersn", order.getId());
goods.setGpicture(newFileName);
File targetFile = new File(realpath, newFileName);
if(!targetFile.exists()){
targetFile.mkdirs();
}
//上传
try {
goods.getLogoImage().transferTo(targetFile);
} catch (Exception e) {
e.printStackTrace();
}
}
//修改
if("update".equals(updateAct)){//updateAct不能与act重名,因为使用了转发
//修改到数据库
if(adminGoodsDao.updateGoodsById(goods) > 0){
return "forward:/adminGoods/selectGoods?act=updateSelect";
}else{
return "/adminGoods/updateAgoods";
}
}else{//添
//保存到数据库
if(adminGoodsDao.addGoods(goods) > 0){
//转发到查询的controller
return "forward:/adminGoods/selectGoods";
}else{
return "card/addCard";
}
}
}
/**
* 查询商品
*/
@Override
public String selectGoods(Model model, Integer pageCur, String act) {
List<Goods> allGoods = adminGoodsDao.selectGoods();
int temp = allGoods.size();
model.addAttribute("totalCount", temp);
int totalPage = 0;
if (temp == 0) {
totalPage = 0;//总页数
} else {
//返回大于或者等于指定表达式的最小整数
totalPage = (int) Math.ceil((double) temp / 10);
}
if (pageCur == null) {
pageCur = 1;
}
if ((pageCur - 1) * 10 > temp) {
pageCur = pageCur - 1;
}
/**
* 转到商品详情页
*/
@RequestMapping("/goodsDetail")
public String goodsDetail(Model model,Integer id) {
return indexService.goodsDetail(model, id);
}
/**
* 转到公告页面
*/
@RequestMapping("/selectANotice")
public String selectANotice(Model model,Integer id) {
return indexService.selectANotice(model, id);
}
/**
* 首页搜索
*/
@RequestMapping("/search")
public String search(Model model,String mykey) {
return indexService.search(model, mykey);
}
}
public class MyExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
Exception arg3) {
Map<String, Object> model = new HashMap<String, Object>();
model.put("ex", arg3);
// 根据不同错误转向不同页面
if(arg3 instanceof AdminLoginNoException){
//登录页面需要auser对象
arg0.setAttribute("auser", new Auser());
arg0.setAttribute("msg", "没有登录,请登录!");
return new ModelAndView("/admin/login", model);
} else if(arg3 instanceof UserLoginNoException){
arg0.setAttribute("buser", new Buser());