基于javaweb+mysql的ssm+maven校园鲜花销售商城系统(java+ssm+jsp+layui+mysql)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM+Maven校园鲜花销售商城系统(java+ssm+jsp+layui+mysql)
一、项目简述 环境配置:
Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,MyEclispe,Sts都支持)
项目技术:
JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + layui+ maven等等。
功能介绍:用户管理、商品管理、评论管理、订单管理、管理员管理、资讯管理、登录注册 等等。
* @param model
* @return
*/
@RequestMapping("/forePayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(1);
orderService.update(order);
model.addAttribute("total", total);
return "forepage/forePayed";
}
/**
* 我的订单 根据session查看当前用户的订单
* @param model
* @param session
* @return
*/
@RequestMapping("/forebought")
public String forebought(Model model, HttpSession session){
Customer customer = (Customer) session.getAttribute("cst");
List<Order> os= orderService.list(customer.getId());
//给每个订单的订单项设置属性值,如orderitem、product
orderItemService.fill(os);
model.addAttribute("os", os);
return "forepage/foreBought";
}
/**
* 搜索商品
* @param model
* @param pName
* @return
*/
@RequestMapping("/foreNameLike")
public String foreNameLike(Model model, String pName, Page page){
PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
}else {
model.addAttribute("error", "账号已被停用!");
return "/login";
}
} catch (AuthenticationException e) {
model.addAttribute("error", "验证失败!");
return "/login";
}
}
}
/**
* 权限模块controller
*/
@Controller
@RequestMapping("/config")
public class PermissionController {
@Autowired
PermissionService permissionService;
/**
* 添加权限页面
* @return
*/
@RequestMapping("/adminPerAddUI")
public String addUI(){
return "syspage/admin-permission-add";
}
/**
* 权限列表
* @param model
* @param page
* @return
*/
@RequestMapping("/listPermission")
public String list(Model model, Page page){
/**
* 登陆页面
* @return
*/
@RequestMapping("/foreLoginUI")
public String foreLoginUI(){
return "forepage/forelogin";
}
/**
* 模态窗口登陆 验证
* @param customer
* @param session
* @return
*/
@RequestMapping("/foreMtLogin")
@ResponseBody
public String foreIsLogin(Customer customer, HttpSession session){
Customer cst = customerService.foreLogin(customer);
if(null==cst){
return "false";
}
session.setAttribute("cst", cst);
return "true";
}
/**
* ajax判断客户是否登陆
* @param session
* @return
*/
@RequestMapping("/foreIsLogin")
@ResponseBody
public String isLogin(HttpSession session){
Customer cst = (Customer) session.getAttribute("cst");
return cst==null?"false":"true";
}
/**
* 注册
* @param customer
* @return
*/
@RequestMapping("/foreRegister")
public String register(Customer customer){
customer.setStatus(0);
customerService.save(customer);
return "forepage/registerSuccess";
}
String uri = request.getRequestURI(); //访问首页 /fore/foreIndex
//去掉前缀/fore
uri = uri.substring(5,uri.length()); //去掉前缀后 /foreIndex
//如果访问的地址是/fore开头
if(uri.startsWith("/fore")){
//判断是否是在noNeedAuthPage
String method = StringUtils.substringAfterLast(uri,"/fore" ); // 取出/fore后的字符串:Index
//如果不在,那么就需要进行是否登录验证
if(!Arrays.asList(noNeedAuthPage).contains(method)){
Customer customer =(Customer) session.getAttribute("cst");
if(null==customer){
response.sendRedirect("foreLoginUI");
return false;
}
}
}
return true;
}
/**
* 在业务处理器处理请求执行完成后,生成视图之前执行的动作
* 可在modelAndView中加入数据,比如当前时间
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
// super.postHandle(request, response, handler, modelAndView);
}
/**
* 在DispatcherServlet完全处理完请求后被调用,可用于清理资源等
*
* 当有拦截器抛出异常时,会从当前拦截器往回执行所有的拦截器的afterCompletion()
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// super.afterCompletion(request, response, handler, ex);
}
}
private String rootPath = null;
private String contextPath = null;
private String actionType = null;
private ConfigManager configManager = null;
public ActionEnter ( HttpServletRequest request, String rootPath ) {
this.request = request;
this.rootPath = rootPath;
this.actionType = request.getParameter( "action" );
this.contextPath = request.getContextPath();
this.configManager = ConfigManager.getInstance( this.rootPath, this.contextPath, request.getRequestURI() );
}
public String exec () {
String callbackName = this.request.getParameter("callback");
if ( callbackName != null ) {
if ( !validCallbackName( callbackName ) ) {
return new BaseState( false, AppInfo.ILLEGAL ).toJSONString();
}
return callbackName+"("+this.invoke()+");";
} else {
return this.invoke();
}
}
public String invoke() {
if ( actionType == null || !ActionMap.mapping.containsKey( actionType ) ) {
return new BaseState( false, AppInfo.INVALID_ACTION ).toJSONString();
}
/**
* 添加购物车
* @param pid 商品id
* @param number 购买数量
* @param model
* @param session
* @return boolean
*/
@RequestMapping("/foreAddCart")
@ResponseBody
public String addCart(int pid, int number, Model model, float totalPrice, HttpSession session) {
Customer customer =(Customer) session.getAttribute("cst");
if(customer==null){
return "false";
}
Product p = productService.get(pid);
boolean found = false;
//获得订单项表中该用户的所有订单id为空的订单项
List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
for (OrderItem oi : ois) {
//基于用户对象customer,查询没有生成订单的订单项集合
// 如果产品是一样的话,就进行数量追加
if(oi.getProduct().getId().intValue()==p.getId().intValue()){
//如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
oi.setNumber(oi.getNumber()+number);
orderItemService.update(oi);
found = true;
break;
}
}
//如果不存在对应的OrderItem,那么就新增一个订单项OrderItem
if(!found){
OrderItem oi = new OrderItem();
oi.setCstid(customer.getId());
oi.setNumber(number);
oi.setPid(pid);
orderItemService.save(oi);
@Autowired
private ForeService foreService;
@Autowired
private ProductService productService;
@Autowired
private ReviewService reviewService;
@Autowired
private CategoryService categoryService;
@Autowired
private CustomerService customerService;
@Autowired
private OrderItemService orderItemService;
@Autowired
private OrderService orderService;
@Autowired
private ZiXunService ziXunService;
public String PNAME=null;
/**
* 前台首页
* @param model
* @return
*/
@RequestMapping("/foreIndex")
public String index(Model model, HttpSession session){
//传入3个分类
List<Category> categories = foreService.listToThree();
List<Category> categories1 = categoryService.list();
//给每个分类设置商品
for (Category c:categories){
List<Product> products = productService.getProductsByCid(c.getId());
//如果分类下的商品超过4个,则只显示4个给前端
if(products.size()>5){
List<Product> products1 = new ArrayList<Product>();
for(int i=0;i<=4;i++){
products1.add(products.get(i));
}
c.setProducts(products1);
}else{
c.setProducts(products);
}
}
model.addAttribute("categories",categories);
session.setAttribute("categories",categories1); //保存在session 使其他页面也能获取到分类列表 而不用每次都去查询
return "forepage/index2";
}
return "forepage/forelogin";
}
/**
* 客户注销
* @param session
* @return
*/
@RequestMapping("/foreCstLoginOut")
public String cstLoginOut(HttpSession session){
session.setAttribute("cst",null);
return "redirect:foreIndex";
}
/**
* 立即购买
* @param session
* @param pid 商品id
* @param number 商品数量
* @return 重定向到支付 , 传入订单项id
*/
@RequestMapping("/forebuyone")
public String forebuyone(HttpSession session, int pid, int number, float totalPrice){
Customer cst = (Customer) session.getAttribute("cst");
Product product = productService.get(pid);
int oiid = 0;
boolean find = false;
List<OrderItem> orderItems = orderItemService.listByCustomer(cst.getId());//获得订单项表中该用户的所有订单id为空的订单项
for (OrderItem oi : orderItems) {
//基于用户对象customer,查询没有生成订单的订单项集合
// 如果产品是一样的话,就进行数量追加
if(oi.getProduct().getId().intValue()==product.getId().intValue()){
//如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
oi.setNumber(oi.getNumber()+number);
orderItemService.update(oi);
find = true;
//获取这个订单项的 id
oiid = oi.getId();
break;
float total = 0;
int number = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi= orderItemService.get(id);
if (cst.getStatus()==1){
total +=oi.getProduct().getPrice()*0.8*oi.getNumber();
}else{
total +=oi.getProduct().getPrice()*oi.getNumber();
}
number += oi.getNumber();
ois.add(oi);
}
/*
累计这些ois的价格总数,赋值在total上
把订单项集合放在session的属性 "ois" 上,方便下订单时候直接获取
把总价格放在 model的属性 "total" 上
服务端跳转到buy.jsp
*/
session.setAttribute("ois", ois);
model.addAttribute("total", total);
model.addAttribute("number", number);
return "forepage/foreBuy";
}
/**
* 添加购物车
* @param pid 商品id
* @param number 购买数量
* @param model
* @param session
* @return boolean
*/
@RequestMapping("/foreAddCart")
@ResponseBody
public String addCart(int pid, int number, Model model, float totalPrice, HttpSession session) {
Customer customer =(Customer) session.getAttribute("cst");
if(customer==null){
return "false";
}
Product p = productService.get(pid);
boolean found = false;
//获得订单项表中该用户的所有订单id为空的订单项
List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
for (OrderItem oi : ois) {
//基于用户对象customer,查询没有生成订单的订单项集合
// 如果产品是一样的话,就进行数量追加
if(oi.getProduct().getId().intValue()==p.getId().intValue()){
//如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
oi.setNumber(oi.getNumber()+number);
public String createOrder(Model model, String address, HttpSession session){
/*
提交订单后,设置code,客户id,支付状态,地址
*/
Order order = new Order();
Customer customer =(Customer) session.getAttribute("cst");
String orderCode = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()) + RandomUtils.nextInt(10000);
order.setCode(orderCode);
order.setAddress(address);
order.setCstid(customer.getId());
order.setStatus(0);//未支付
List<OrderItem> ois= (List<OrderItem>) session.getAttribute("ois");
//给每个订单项设置订单id 并且算出订单总价
float total =orderService.add(order,ois);
return "redirect:forePayed?oid="+order.getId() +"&total="+total;
}
/**
* 支付成功跳转
* @param oid 订单id
* @param total 总价
* @param model
* @return
*/
@RequestMapping("/forePayed")
public String payed(int oid, float total, Model model) {
Order order = orderService.get(oid);
order.setStatus(1);
orderService.update(order);
model.addAttribute("total", total);
return "forepage/forePayed";
}
/**
* 我的订单 根据session查看当前用户的订单
* @param model
* @param session
* @return
*/
@RequestMapping("/forebought")
public String forebought(Model model, HttpSession session){
Customer customer = (Customer) session.getAttribute("cst");
List<Order> os= orderService.list(customer.getId());
//给每个订单的订单项设置属性值,如orderitem、product
orderItemService.fill(os);
model.addAttribute("os", os);
return "forepage/foreBought";
model.addAttribute("product",product);
System.out.println(product);
List<Category> categoryList = categoryService.list();
List<User> userList = userService.list();
//通过商品id 返回所属分类
Category categoryByid = productService.getCategoryByCid(id);
model.addAttribute("crrentCategory",categoryByid);
//通过id返回所属商家
User userById = userService.getUserByPid(id);
model.addAttribute("crrentUser",userById);
model.addAttribute("categoryList",categoryList);
model.addAttribute("userList",userList);
return "productmodule/product-edit";
}
@RequestMapping("/updateProduct")
public String update(Product product, HttpSession session, UploadUtil upload) throws IOException {
productService.update(product);
if(upload!=null){
String imageName = product.getId()+".jpg";
File file = new File(session.getServletContext().getRealPath("/images/product"),imageName);
file.getParentFile().mkdirs();
upload.getImage().transferTo(file);
ProductVO vo = new ProductVO();
vo.setId(product.getId());
vo.setImageUrl("images/product/"+imageName);
productService.setImageURL(vo);
}
return "redirect:list";
}
}
@RequestMapping("/editRole")
public String list(Model model, long id){
Role role =roleService.get(id);
model.addAttribute("role", role);
//所有权限
List<Permission> ps = permissionService.list();
model.addAttribute("ps", ps);
//当前管理员拥有的权限
List<Permission> currentPermissions = permissionService.list(role);
model.addAttribute("currentPermissions", currentPermissions);
return "syspage/admin-role-edit";
}
@RequestMapping("/updateRole")
public String update(Role role,long[] permissionIds){
rolePermissionService.setPermissions(role, permissionIds);
roleService.update(role);
return "redirect:listRole";
}
@RequestMapping("/addRole")
public String list(Model model, Role role){
roleService.add(role);
return "redirect:listRole";
}
@RequestMapping("/deleteRole")
public String delete(Model model, long id){
roleService.delete(id);
return "redirect:listRole";
}
}
/**
}
@RequestMapping("/foreZixunadd")
@ResponseBody
public String zixunadd(String content, HttpSession session){
Customer c = (Customer) session.getAttribute("cst");
ZiXun z = new ZiXun();
z.setCstid(c.getId());
z.setContent(content);
z.setFabudate(new Date());
z.setStatus(0);
ziXunService.save(z);
return "success";
}
}
/**
* 商品模块controller
*/
@Controller
@RequestMapping("/product")
public class ProductController {
@Autowired
private ProductService productService;
@Autowired
* 修改管理员角色
* @param model
* @param id
* @return
*/
@RequestMapping("/editUser")
public String edit(Model model, Long id){
List<Role> rs = roleService.list();
model.addAttribute("rs", rs);
User user =userService.get(id);
model.addAttribute("user", user);
//当前拥有的角色
List<Role> roles =roleService.listRoles(user);
model.addAttribute("currentRoles", roles);
return "syspage/admin-edit";
}
@RequestMapping("deleteUser")
public String delete(Model model, long id){
userService.delete(id);
return "redirect:listUser";
}
@RequestMapping("updateUser")
public String update(User user, long[] roleIds){
userRoleService.setRoles(user,roleIds);
String password=user.getPassword();
//如果在修改的时候没有设置密码,就表示不改动密码
if(user.getPassword().length()!=0) {
String salt = new SecureRandomNumberGenerator().nextBytes().toString();
int times = 2;
String algorithmName = "md5";
String encodedPassword = new SimpleHash(algorithmName,password,salt,times).toString();
user.setSalt(salt);
user.setPassword(encodedPassword);
user.setPassword(password);
}
else
user.setPassword(null);
userService.update(user);
return "redirect:listUser";
}
@RequestMapping("addUser")
ois.add(oi);
}
/*
累计这些ois的价格总数,赋值在total上
把订单项集合放在session的属性 "ois" 上,方便下订单时候直接获取
把总价格放在 model的属性 "total" 上
服务端跳转到buy.jsp
*/
session.setAttribute("ois", ois);
model.addAttribute("total", total);
model.addAttribute("number", number);
return "forepage/foreBuy";
}
/**
* 添加购物车
* @param pid 商品id
* @param number 购买数量
* @param model
* @param session
* @return boolean
*/
@RequestMapping("/foreAddCart")
@ResponseBody
public String addCart(int pid, int number, Model model, float totalPrice, HttpSession session) {
Customer customer =(Customer) session.getAttribute("cst");
if(customer==null){
return "false";
}
Product p = productService.get(pid);
boolean found = false;
//获得订单项表中该用户的所有订单id为空的订单项
List<OrderItem> ois = orderItemService.listByCustomer(customer.getId());
for (OrderItem oi : ois) {
//基于用户对象customer,查询没有生成订单的订单项集合
// 如果产品是一样的话,就进行数量追加
if(oi.getProduct().getId().intValue()==p.getId().intValue()){
//如果已经存在这个产品对应的OrderItem,并且还没有生成订单,即还在购物车中。 那么就应该在对应的OrderItem基础上,调整数量
oi.setNumber(oi.getNumber()+number);
orderItemService.update(oi);
found = true;
break;
}
}
//如果不存在对应的OrderItem,那么就新增一个订单项OrderItem
/**
* 管理员角色controler
*/
@Controller
@RequestMapping("/config")
public class RoleController {
@Autowired
RoleService roleService;
@Autowired
RolePermissionService rolePermissionService;
@Autowired
PermissionService permissionService;
@RequestMapping("/addRoleUI")
public String addRole(){
return "syspage/admin-role-add";
}
@RequestMapping("/listRole")
public String list(Model model, Page page){
PageHelper.offsetPage(page.getStart(),page.getCount());//分页查询
List<Role> rs= roleService.list();
int total = (int) new PageInfo<>(rs).getTotal();//总条数
page.setTotal(total);
model.addAttribute("rs", rs);
model.addAttribute("roleSize",total);
Map<Role,List<Permission>> role_permissions = new HashMap<>();
for (Role role : rs) {
List<Permission> ps = permissionService.list(role);
role_permissions.put(role, ps);
}
model.addAttribute("role_permissions", role_permissions);
return "syspage/admin-role";
}
@RequestMapping("/editRole")
public String list(Model model, long id){
Role role =roleService.get(id);
model.addAttribute("role", role);
//所有权限
if(!find){
OrderItem oi = new OrderItem();
oi.setCstid(cst.getId());
oi.setNumber(number);
oi.setPid(pid);
orderItemService.save(oi);
//获取这个刚添加的订单项的 id
oiid = oi.getId();
}
return "redirect:forebuy?oiid="+oiid;
}
/**
* 立即购买、购物车提交到订单页面调用 根据oiid计算订单项的总价、购买数量 , 订单项放session
* 订单-支付 上一次的购物信息会被下次单个挤掉 根据oiid获得订单项
* @param model
* @param oiid 立即购买生成的订单项id
* @param session
* @return 返回订单项集合 | 返回所有订单项加起来的总价
*/
@RequestMapping("/forebuy")
public String forebuy(Model model, String[] oiid, HttpSession session){
System.out.println(oiid);
List<OrderItem> ois = new ArrayList<OrderItem>();
Customer cst = (Customer)session.getAttribute("cst");
float total = 0;
int number = 0;
for (String strid : oiid) {
int id = Integer.parseInt(strid);
OrderItem oi= orderItemService.get(id);
if (cst.getStatus()==1){
total +=oi.getProduct().getPrice()*0.8*oi.getNumber();
}else{
total +=oi.getProduct().getPrice()*oi.getNumber();
}
number += oi.getNumber();
ois.add(oi);
}