作者主页:源码空间站2022
简介:Java领域优质创作者、Java项目、学习资料、技术互助
文末获取源码
项目介绍
该项目为前后台项目,分为普通用户与管理员两种角色,前台普通用户登录,后台管理员登录;
普通用户主要功能包括:
登录注册,查看商品,提交订单,然后留言,查看购物车,购买商品等等。
管理员主要功能包括:
添加商品,管理用户,添加类目,管理留言等等。
环境需要
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项目:否;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+jQuery+Ajax
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,输入localhost:8080/ 登录
用户账号/密码: user/123456
管理员账号/密码:admin/admin
运行截图
前台界面
后台界面
相关代码
登录控制器
package com.shop.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shop.exception.zdyException;
import com.shop.po.Adminuser;
import com.shop.po.User;
import com.shop.service.UserService;
@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";
}
// 用户退出
@RequestMapping("userLogout")
public String userLogout(HttpServletRequest request,
HttpServletResponse response, Model model) {
request.getSession().removeAttribute("loginUser");
response.setHeader("content-type", "text/html;charset=UTF-8");
response.setHeader("refresh", "5;url=/shop/index.action");
model.addAttribute("message", "退出成功,系统将在5秒之后跳转到首页......");
return "msg";
}
//进入管理员页面
@RequestMapping("/admin")
public String AdminIndex(){
return "admin/index";
}
//admin/adminUser_login.action
//adminUser_login管理员登录
@RequestMapping("/admin/adminUser_login")
public String adminUser_login(HttpServletRequest request,Model model,@RequestParam String username,@RequestParam String password) throws Exception{
Adminuser adminuserLogin = userService.adminUser_login(username,password);
System.out.println(username);
System.out.println(password);
if(adminuserLogin == null){
model.addAttribute("message", "改用户未被授予管理员身份!");
return "admin/index";
}
request.getSession().setAttribute("adminuserLogin", adminuserLogin);
// model.addAttribute("adminuserLogin", adminuserLogin);
return "admin/home";
}
}
产品模块
package com.shop.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shop.po.Product;
import com.shop.service.ProductService;
@Controller
public class ProductController {
@Autowired
private ProductService prodcutService;
@RequestMapping("/productFindByPid")
public String productFindByPid(@RequestParam int pid,Model model) throws Exception {
Product product = prodcutService.productFindByPid(pid);
model.addAttribute("product", product);
return "product";
}
}
注册控制器
package com.shop.controller;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shop.exception.zdyException;
import com.shop.po.User;
import com.shop.service.UserService;
@Controller
public class registController {
@Autowired
private UserService userService;
@RequestMapping("regist")
public String regist() {
return "regist";
}
@RequestMapping("/userRegist")
public String userRegist(Model model, HttpServletRequest request,
@Validated User user, BindingResult bindingResult,
@RequestParam String checkImg) throws Exception {
if (bindingResult.hasErrors()) {
List<ObjectError> errors =bindingResult.getAllErrors();
List<String> list = new ArrayList<>();
for (ObjectError objectError : errors) {
String str = new String(objectError.getDefaultMessage()
.getBytes("ISO-8859-1"), "gbk");
list.add(str);
}
model.addAttribute("errors", list);
return "regist";
}
// 查看验证码
String sessionCode = (String) request.getSession().getAttribute(
"checkcode");
// System.out.println("adadadad" + sessionCode);
if (!sessionCode.equalsIgnoreCase(checkImg)) {
// model.addAttribute("message", "验证码错误请重新注册");
throw new zdyException("验证码错误请重新注册");
}
// 开始写入数据库
userService.saveUser(user);
//model.addAttribute("message", "注册成功请去邮箱激活");
model.addAttribute("message", "注册成功请登录");
return "msg";
}
@RequestMapping("/activeUser")
public String activeUser(@RequestParam String code,Model model) throws zdyException,Exception {
/*
* 根据传递激活码进行用户查询. 如果用户不为空: 修改用户状态 改为1 如果用户为空: 激活码被篡改了.
*/
User activeUser = userService.findByCode(code);
if(activeUser==null){
// model.addAttribute("message", "激活码被篡改了,请重新注册");
throw new zdyException("激活码被篡改了,请重新注册");
}
activeUser.setState(1);
activeUser.setCode(null);
userService.activeUser(activeUser);
model.addAttribute("message", "激活码成功");
return "msg";
}
}
购物车
package com.shop.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shop.po.Cart;
import com.shop.po.CartItem;
import com.shop.po.Product;
import com.shop.po.User;
import com.shop.service.ProductService;
@Controller
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;
}
}
二级分类
package com.shop.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import com.shop.Utils.PageBean;
import com.shop.po.Product;
import com.shop.service.ProductService;
@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";
}
}
如果也想学习本系统,下面领取。关注并回复:112ssm