基于javaweb+mysql的ssm在线手机品牌商城(java+ssm+jsp+jquery+ajax+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM在线手机品牌商城(java+ssm+jsp+jquery+ajax+mysql)
项目介绍
该项目为前后台项目,分为普通用户与管理员两种角色,前台普通用户登录,后台管理员登录; 管理员角色包含以下功能: 管理员登录,用户管理,品牌管理,子品牌管理,商品管理,订单管理,留言板管理等功能。 用户角色包含以下功能: 用户首页,用户登录,加入购物车,提交订单,付款页面,我的订单,留言板等功能。
环境需要
1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。 2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA; 3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可 4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS; 5.数据库:MySql 5.7版本; 6.是否Maven项目:否;
技术栈
- 后端:Spring+SpringMVC+Mybatis 2. 前端:JSP+jQuery+Ajax
使用说明
- 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件; 2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven; 若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行; 3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置; 4. 运行项目,在浏览器中输入http://localhost:8080/ 登录 用户账号/密码: user/123456 管理员账号/密码:admin/admin
@Controller
public class checkImgController {
@RequestMapping("checkImg")
public void checkImg(HttpServletRequest request ,HttpServletResponse response) throws IOException {
int width = 120;
int height = 30;
// 步骤一 绘制一张内存中图片
BufferedImage bufferedImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 步骤二 图片绘制背景颜色 ---通过绘图对象
Graphics graphics = bufferedImage.getGraphics();// 得到画图对象 --- 画笔
// 绘制任何图形之前 都必须指定一个颜色
graphics.setColor(getRandColor(200, 250));
graphics.fillRect(0, 0, width, height);
// 步骤三 绘制边框
graphics.setColor(Color.WHITE);
graphics.drawRect(0, 0, width - 1, height - 1);
// 步骤四 四个随机数字
Graphics2D graphics2d = (Graphics2D) graphics;
// 设置输出字体
graphics2d.setFont(new Font("宋体", Font.BOLD, 18));
// String words
// ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
String words = "\u7684\u4e00\u4e86\u662f\u6211\u4e0d\u5728\u4eba\u4eec\u6709\u6765\u4ed6\u8fd9\u4e0a\u7740\u4e2a\u5730\u5230\u5927\u91cc\u8bf4\u5c31\u53bb\u5b50\u5f97\u4e5f\u548c\u90a3\u8981\u4e0b\u770b\u5929\u65f6\u8fc7\u51fa\u5c0f\u4e48\u8d77\u4f60\u90fd\u628a\u597d\u8fd8";
Random random = new Random();// 生成随机数
// 定义StringBuffer
StringBuffer sb = new StringBuffer();
// 定义x坐标
int x = 10;
for (int i = 0; i < 4; i++) {
// 随机颜色
graphics2d.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 旋转 -30 --- 30度
int jiaodu = random.nextInt(60) - 30;
{
model.addAttribute("sList", hList);
}
return "searchProduct";
}
}
@Controller
public class registController {
@Autowired
private UserService userService;
@RequestMapping("regist")
public String regist() {
return "regist";
}
@RequestMapping("/userRegist")
public String userRegist(Model model, HttpServletRequest request,
@Controller
public class CategorySecondController {
@Autowired
private ProductService productService;
//根据一级目录查找二级目录下面的商品(使用延迟加载但是这里不知为啥使用起来出错了)
@RequestMapping("/findCategorySecond")
public String findCategorySecond(HttpServletRequest request,@RequestParam int cid,Model model,@RequestParam int page) throws Exception {
request.getSession().setAttribute("cid",cid);
PageBean<Product> proPageBean = productService.findProductyBycid(cid,page);
model.addAttribute("pageBean",proPageBean);
return "category";
}
// 根据csid来分页查询商品
@RequestMapping("/findCategorySecond1")
public String findCategorySecond1(@RequestParam int csid,Model model,@RequestParam int page){
PageBean<Product> proPageBean = productService.finbProductByCsid(csid,page);
model.addAttribute("pageBean",proPageBean);
return "category";
}
}
public class EncodingInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2) throws Exception {
response.setContentType("text/html;charset=UTF-8");
/*String method = request.getMethod();
if (method.equalsIgnoreCase("post")) {
request.setCharacterEncoding("utf-8");
} else {
// new String(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
Iterator iter = request.getParameterMap().values().iterator();
while (iter.hasNext()) {
String[] parames = (String[]) iter.next();
for (int i = 0; i < parames.length; i++) {
parames[i] = new String(parames[i].getBytes("iso8859-1"),
"utf-8");// 此处utf-8与页面编码一样
}
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
PageBean<Product> allProPageBean = productService.findAllProduct(page);
model.addAttribute("allProPageBean", allProPageBean);
return "admin/product/list";
}
@RequestMapping("/admin/adminProduct_addPage")
public String adminProduct_addPage(Model model,HttpServletRequest request) throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
//查询出所有的二级分类
List<Categorysecond> cslist = categorySecondService.findAll();
/*for (Categorysecond categorysecond : cslist) {
System.out.println(categorysecond.getCsname());
}*/
model.addAttribute("cslist", cslist);
return "admin/product/add";
}
@RequestMapping("/admin/adminProduct_save")
public String adminProduct_save(Product product,HttpServletRequest request,MultipartFile file) throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
product.setPdate(new Date());
// 上传图片
if (file != null) {
String path = request.getServletContext().getRealPath(
"/products");
String uploadFileName = file.getOriginalFilename();
String fileName = UUIDUtiils.getUUID()+uploadFileName;
File diskFile = new File(path + "//" + fileName);
file.transferTo(diskFile);
product.setImage("products/" + fileName);
}
System.out.println(product.getImage());
Object arg2, ModelAndView arg3) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
}
public class golbalException implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setHeader("refresh", "5;url=/shop/index.action");
zdyException exception =null;
if(ex instanceof zdyException){
exception=(zdyException)ex;
}else{
exception.setMessage("发生了未知异常");
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("errorMessage",exception.getMessage()+"系统将在5秒之后跳转到首页......");
modelAndView.setViewName("error");
return modelAndView;
}
}
@Controller
public class ProductController {
@Autowired
private ProductService prodcutService;
public class cartController {
@Autowired
private ProductService productService;
// 添加到购物车
@RequestMapping("/addCart")
public String addCart(HttpServletRequest request,@RequestParam int pid,@RequestParam int count ,Model model){
Product product = productService.finbProductByPid(pid);
// 存进一个购物项
CartItem cartItem = new CartItem();
cartItem.setCount(count);
cartItem.setProduct(product);
cartItem.setSubtotle(count*product.getShopPrice());
// 存进购物车
Cart cart=getCart(request);
cart.addCart(cartItem);
return "cart";
}
// 查看我的购物车
@RequestMapping("/myCart")
public String myCart(HttpServletRequest request,Model model){
User loginUser = (User) request.getSession().getAttribute("loginUser");
if (loginUser == null) {
model.addAttribute("message","您还没有登录!");
return "msg";
}
return "cart";
}
// 清空购物车
@RequestMapping("/clearCart")
public String clearCart(HttpServletRequest request){
Cart cart=getCart(request);
cart.clearCart();
return "redirect:myCart.action";
}
// 删除某个购物项
@RequestMapping("/delProFromCart")
public String delProFromCart(@RequestParam int pid,HttpServletRequest request){
Cart cart=getCart(request);
cart.delProFromCart(pid);
return "redirect:myCart.action";
}
public Cart getCart(HttpServletRequest request) {
Cart cart = (Cart) request.getSession().getAttribute("cart");
if (cart == null) {
cart = new Cart();
request.getSession().setAttribute("cart", cart);
}
return cart;
}
}
public void registFindByid(HttpServletRequest request,
HttpServletResponse response, @RequestParam String username) {
response.setContentType("text/html;charset=UTF-8");
try {
User user = userService.loginFindByid(username);
if (user!=null) {
response.getWriter().write("已经被注册");
}else{
response.getWriter().write("可以注册");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
@Controller
public class CategorySecondController {
@Autowired
private ProductService productService;
//根据一级目录查找二级目录下面的商品(使用延迟加载但是这里不知为啥使用起来出错了)
@RequestMapping("/findCategorySecond")
public String findCategorySecond(HttpServletRequest request,@RequestParam int cid,Model model,@RequestParam int page) throws Exception {
request.getSession().setAttribute("cid",cid);
PageBean<Product> proPageBean = productService.findProductyBycid(cid,page);
model.addAttribute("pageBean",proPageBean);
return "category";
}
// 根据csid来分页查询商品
@RequestMapping("/findCategorySecond1")
public String findCategorySecond1(@RequestParam int csid,Model model,@RequestParam int page){
PageBean<Product> proPageBean = productService.finbProductByCsid(csid,page);
model.addAttribute("pageBean",proPageBean);
return "category";
}
}
int x1;
int x2;
int y1;
int y2;
for (int i = 0; i < 30; i++) {
x1 = random.nextInt(width);
x2 = random.nextInt(12);
y1 = random.nextInt(height);
y2 = random.nextInt(12);
graphics.drawLine(x1, y1, x1 + x2, x2 + y2);
}
// 将上面图片输出到浏览器 ImageIO
graphics.dispose();// 释放资源
ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
}
private Color getRandColor(int fc, int bc) {
// 取其随机颜色
Random random = new Random();
if (fc > 255) {
fc = 255;
}
if (bc > 255) {
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
}
public class golbalException implements HandlerExceptionResolver {
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex) {
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setHeader("refresh", "5;url=/shop/index.action");
zdyException exception =null;
if(ex instanceof zdyException){
exception=(zdyException)ex;
}else{
exception.setMessage("发生了未知异常");
}
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("errorMessage",exception.getMessage()+"系统将在5秒之后跳转到首页......");
modelAndView.setViewName("error");
return modelAndView;
}
}
@Controller
public class ProductController {
}
@RequestMapping("admin/adminCategorySecond_edit")
public String adminCategorySecond_edit(@RequestParam int csid,Model model,HttpServletRequest request) throws Exception{
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
Categorysecond findByCsid = categorySecondService.findByCsid(csid);
model.addAttribute("findByCsid", findByCsid);
return "admin/categorysecond/edit";
}
@RequestMapping("admin/adminCategorySecond_update")
public String adminCategorySecond_update(@RequestParam String csname,@RequestParam int csid,HttpServletRequest request){
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
Categorysecond categorysecond = new Categorysecond();
categorysecond.setCsname(csname);
categorysecond.setCsid(csid);
categorySecondService.adminCategorySecond_update(categorysecond);
return "redirect:/admin/adminCategorySecond_findAllByPage.action?page=1";
}
@RequestMapping("admin/adminCategorySecond_delete")
public String adminCategorySecond_delete(@RequestParam int csid,HttpServletRequest request) throws Exception{
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
categorySecondService.adminCategorySecond_delete(csid);
return "redirect:/admin/adminCategorySecond_findAllByPage.action?page=1";
}
//admin的商品管理
@RequestMapping("/admin/adminProduct_findAllByPage")
public String adminProduct_findAllByPage(@RequestParam int page, Model model,HttpServletRequest request)
throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
}
@Controller
public class PrivilageInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object object) throws Exception {
String uri = request.getRequestURI();
if(uri.indexOf("admin.action")>=0){
return true;
}
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin!=null){
return true ;
}
request.getRequestDispatcher("/WEB-INF/jsp/admin/index.jsp").forward(request, response);
return true;
}
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1,
Object arg2, ModelAndView arg3) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest arg0,
HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
}
}
@Controller
public class loginController {
@Autowired
private UserService userService;
@RequestMapping("login")
public String login() {
return "login";
}
// 用户登录
@RequestMapping("/userLogin")
public String userLogin(HttpServletRequest request,
@RequestParam String checkImg, User user, Model model)
throws zdyException, Exception {
String sessionCode = (String) request.getSession().getAttribute(
"checkcode");
if (!sessionCode.equalsIgnoreCase(checkImg)) {
//model.addAttribute("message", "验证码输入错误请重新注册");
//throw new zdyException("验证码输入错误请重新注册");
request.getSession().setAttribute("message", "验证码输入错误请重新注册");
return "redirect:login.action";
}
User loginUser = userService.loginFindByUnameAndPwd(user.getUsername(),
user.getPassword());
if (loginUser == null) {
//model.addAttribute("message", "密码输入错误请重新登陆");
request.getSession().setAttribute("message", "密码输入错误请重新登陆");
//throw new zdyException("密码输入错误请重新登陆");
return "redirect:login.action";
}
request.getSession().setAttribute("loginUser", loginUser);
request.getSession().removeAttribute("message");
return "redirect:index.action";
return "redirect:/admin/adminMessage_findAllByPage.action?page=1";
}
}
@Controller
public class orderController {
@Autowired
private OrderService orderService;
@RequestMapping("/toOrder")
public String toOrder(HttpServletRequest request,Model model) throws Exception {
Orders orders = new Orders();
Cart cart = (Cart) request.getSession().getAttribute("cart");
User loginUser = (User) request.getSession().getAttribute("loginUser");
if(loginUser==null){
model.addAttribute("message", "对不起您还没有登录");
return "msg";
}
// 0表示没有付款、1表示已付款即将发货 2表示确认收货 3表示交易成功
public String searchProduct(@RequestParam String condition,Model model,HttpServletRequest request) throws Exception {
//查询一级分类
List<Category> cList = categoryService.findCategory();
request.getSession().getServletContext().setAttribute("cList",cList);
//查询商品
List<Product> hList= productService.findHotProduct();
model.addAttribute("hList", hList);
//搜索商品
List<Product> srList = productService.searchProduct(condition);
model.addAttribute("srList", srList);
//滚动图片专用,最多显示3个商品
List<Product> sList;
if(hList.size()>3)
{
sList = new ArrayList<Product>();
sList.add(hList.get(0));
sList.add(hList.get(1));
sList.add(hList.get(2));
model.addAttribute("sList", sList);
}
else
{
model.addAttribute("sList", hList);
}
return "searchProduct";
}
}
return "redirect:/admin/adminMessage_findAllByPage.action?page=1";
}
}
@Controller
public class orderController {
@Autowired
private OrderService orderService;
@RequestMapping("/toOrder")
public String toOrder(HttpServletRequest request,Model model) throws Exception {
Orders orders = new Orders();
Cart cart = (Cart) request.getSession().getAttribute("cart");
User loginUser = (User) request.getSession().getAttribute("loginUser");
if(loginUser==null){
@Controller
public class messageController {
@Autowired
private MessageService messageService;
@RequestMapping("/saveMessage")
public String saveMessage(@RequestParam String messageinfo,HttpServletRequest request,Model model) throws Exception {
Message Message = new Message();
User loginUser = (User) request.getSession().getAttribute("loginUser");
if(loginUser==null){
model.addAttribute("message", "对不起您还没有登录");
return "msg";
}
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Message.setMessage(messageinfo);
Message.setUid(loginUser.getUid());
Message.setMessagedate(sdf.format(new Date()));
messageService.insertMessage(Message);
request.getSession().setAttribute("Message", Message);
return "redirect:/messageList.action?page=1";
}
// 显示留言板全部留言
@RequestMapping("/messageList")
public String messageList(@RequestParam int page, Model model,
HttpServletRequest request) throws Exception {
PageBean<Message> pageBean = messageService.findAllMessageByPage(page);
model.addAttribute("pageBean", pageBean);
return "messageList";
}
}
return "redirect:/admin/adminCategorySecond_findAllByPage.action?page=1";
}
@RequestMapping("admin/adminCategorySecond_delete")
public String adminCategorySecond_delete(@RequestParam int csid,HttpServletRequest request) throws Exception{
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
categorySecondService.adminCategorySecond_delete(csid);
return "redirect:/admin/adminCategorySecond_findAllByPage.action?page=1";
}
//admin的商品管理
@RequestMapping("/admin/adminProduct_findAllByPage")
public String adminProduct_findAllByPage(@RequestParam int page, Model model,HttpServletRequest request)
throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
PageBean<Product> allProPageBean = productService.findAllProduct(page);
model.addAttribute("allProPageBean", allProPageBean);
return "admin/product/list";
}
@RequestMapping("/admin/adminProduct_addPage")
public String adminProduct_addPage(Model model,HttpServletRequest request) throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
//查询出所有的二级分类
List<Categorysecond> cslist = categorySecondService.findAll();
/*for (Categorysecond categorysecond : cslist) {
System.out.println(categorysecond.getCsname());
}*/
model.addAttribute("cslist", cslist);
return "admin/product/add";
}
@RequestMapping("/admin/adminProduct_save")
public String adminProduct_save(Product product,HttpServletRequest request,MultipartFile file) throws Exception {
Adminuser adminuserLogin = (Adminuser) request.getSession().getAttribute("adminuserLogin");
if(adminuserLogin==null){
request.getSession().setAttribute("message","对不起您还没有登录");
return "admin/index";
}
product.setPdate(new Date());
// 上传图片
if (file != null) {
String path = request.getServletContext().getRealPath(
"/products");
{
sList = new ArrayList<Product>();
sList.add(hList.get(0));
sList.add(hList.get(1));
sList.add(hList.get(2));
model.addAttribute("sList", sList);
}
else
{
model.addAttribute("sList", hList);
}
return "index";
}
@RequestMapping("/about")
public String about(Model model,HttpServletRequest request) throws Exception {
return "about";
}
@RequestMapping("/contact")
public String contact(Model model,HttpServletRequest request) throws Exception {
return "contact";
}
@RequestMapping("/delivery")
public String delivery(Model model,HttpServletRequest request) throws Exception {
return "delivery";
}
//
@RequestMapping("/searchProduct")
public String searchProduct(@RequestParam String condition,Model model,HttpServletRequest request) throws Exception {
//查询一级分类
List<Category> cList = categoryService.findCategory();
request.getSession().getServletContext().setAttribute("cList",cList);
//查询商品
List<Product> hList= productService.findHotProduct();
model.addAttribute("hList", hList);
//搜索商品
List<Product> srList = productService.searchProduct(condition);
model.addAttribute("srList", srList);
//滚动图片专用,最多显示3个商品
List<Product> sList;
if(hList.size()>3)
{