Java项目:蔬菜网上商城+后台管理系统(java+SSM+mysql+maven+tomcat)

一、项目简述

功能: 功能:系统分管理员界面与用户界面 管理员:用户管理,商品类别管理,商品管理,订单管理,公 告管理留言里筲等 向户:房总主册功能,用户登录功能,商品浏览,商品留言评 论,商品购买,商品支付,订单查询等等

二、项目运行

环境配置: Jdk1.8 + Tomcat8.5 + mysql + Eclispe (IntelliJ IDEA,Eclispe,MyEclispe,Sts 都支持)

项目技术: JSP +Spring + SpringMVC + MyBatis + html+ css + JavaScript + JQuery + Ajax + Fileupload + maven等等。

购物车控制层:

/**
 * 购物车Controller
 *
 */
@Controller
@RequestMapping("/car")
public class CarController {
    @Autowired
    private CarService carService;
    @Autowired
    private ItemService itemService;

    /**
     * 加入购物车
     *
     * @param request
     * @param car
     * @return
     */
    @RequestMapping("/addcar")
    @ResponseBody
    public String addcar(HttpServletRequest request, Car car) {
        JSONObject json = new JSONObject();
        Object userId = request.getSession().getAttribute("userId");
        //如果未登录,返回0,提示先登录
        if (userId == null) {
            json.put(Consts.RES, 0);
            return json.toJSONString();
        }
        //保存到购物车
        Item item = itemService.getById(car.getItemId());
        car.setUserId(Integer.valueOf(userId.toString()));
        String price = item.getPrice();
        Double value = Double.valueOf(price);
        car.setPrice(value);
        // BigDecimal bigDecimal = new BigDecimal(value).setScale(2,BigDecimal.ROUND_UP);
        if (item.getZk() != null) {
            value = value * item.getZk() / 10;
            BigDecimal bigDecimal = new BigDecimal(value).setScale(2, RoundingMode.UP);
            car.setPrice(bigDecimal.doubleValue());
        }
        Integer num = car.getNum();
        Double t = value * num;
        BigDecimal bigDecimal = new BigDecimal(t).setScale(2, RoundingMode.UP);
        Double tDouble = bigDecimal.doubleValue();
        car.setTotal(tDouble + "");
        carService.insert(car);
        json.put(Consts.RES, 1);
        return json.toJSONString();
    }

    /**
     * 跳转到购物车页面
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/findBySql")
    public String findBySql(Model model, HttpServletRequest request) {
         Object userId = request.getSession().getAttribute("userId");
         if(userId==null){
             return "redirect:/login/uLogin";

         }
         Integer id = Integer.valueOf(userId.toString());
         String sql = "select * from car where user_id="+id+" order by id";
        List<Car> carList = carService.listBySqlReturnEntity(sql);
        model.addAttribute("list",carList);
        return "/car/carview";
    }

    /**
     * 删除购物车
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    @ResponseBody
    public String delete(Integer id){
        carService.deleteById(id);
        return "success";
    }
}

评论控制层:

/**
 * 评论Controller
 */
@Controller
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private ItemOrderService itemOrderService;
    @Autowired
    private OrderDetailService orderDetailService;
    @RequestMapping("/exAdd")
    public String commentAdd(Comment comment , HttpServletRequest request,Integer orderId){
            Object obj = request.getSession().getAttribute("userId");
            if(obj==null){
                return "redirect:/login/toULogin.action";
            }
            //把订单状态至为4(已评价)
            ItemOrder itemOrder = itemOrderService.getById(orderId);
            itemOrder.setStatus(4);
            itemOrderService.updateById(itemOrder);
            comment.setUserId(Integer.valueOf(obj.toString()));
            comment.setAddTime(new Date());
            commentService.insert(comment);
            return "redirect:/itemOrder/my.action";
    }
}

登录相关的控制器:

/**
 * 登录相关的控制器
 */
@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {

    @Autowired
    private ManageService manageService;
    @Autowired
    private ItemCategoryService itemCategoryService;
    @Autowired
    private ItemService itemService;
    @Autowired
    private UserService userService;
    /**
     * 管理员登录前
     * @return
     */
    @RequestMapping("login")
    public String login(){
        return "/login/mLogin";
    }

    /**
     * 登录验证
     * @return
     */
    @RequestMapping("toLogin")
    public String toLogin(Manage manage, HttpServletRequest request){
       Manage byEntity = manageService.getByEntity(manage);
       if(byEntity==null){
           return "redirect:/login/mtuichu";
       }
       request.getSession().setAttribute(Consts.MANAGE,byEntity);
       return "/login/mIndex";
    }

    /**
     * 管理员退出
     */
    @RequestMapping("mtuichu")
    public String mtuichu(HttpServletRequest request){
        request.getSession().setAttribute(Consts.MANAGE,null);
        return "/login/mLogin";
    }

    /**
     * 默认首页面
     * @param model
     * @param item
     * @param request
     * @return
     */
    @RequestMapping("uIndex")
    public String uIndex(Model model, Item item, HttpServletRequest request){
        //查询一级类目列表
        String sql1 = "select * from item_category where pid is null and isDelete=0 order by name ";
        List<ItemCategory> itemCategoryFatherList = itemCategoryService.listBySqlReturnEntity(sql1);
        List<CategoryDto> categoryDtoList = new ArrayList<>();
        for (ItemCategory itemCategory:itemCategoryFatherList){
            CategoryDto categoryDto = new CategoryDto();
            categoryDto.setFather(itemCategory);
            //查询一级目录对应的二级目录商品名
            String sql2 = "select * from item_Category where isDelete = 0 and pid = "+itemCategory.getId();
            List<ItemCategory> itemCategoryChildrenList = itemCategoryService.listBySqlReturnEntity(sql2);
            categoryDto.setChldren(itemCategoryChildrenList);
            categoryDtoList.add(categoryDto);
        }
        model.addAttribute("lbs",categoryDtoList);
        //查找折扣商品
        String sql3 = "select * from item where isDelete=0 and zk is not null order by zk desc limit 0,10";
        List<Item> itemListZK = itemService.listBySqlReturnEntity(sql3);
        model.addAttribute("zks",itemListZK);
        //查找热销商品
        String sql4 = "select * from item where isDelete=0 order by gmNum desc limit 0,10";
        List<Item> itemListGM = itemService.listBySqlReturnEntity(sql4);
        model.addAttribute("rxs",itemListGM);
        return "/login/uIndex";
    }

    /**
     * 跳转到注册页面
     * @return
     */
    @RequestMapping("/register")
    public String register(){
        return "/register/register";
    }

    @RequestMapping("/toRegister")
    public String toRegister(User user){
        if(user.getSex().equals("man")){
            user.setSex("男");
        }
        if(user.getSex().equals("woman")){
            user.setSex("女");
        }
        userService.insert(user);
        return "login/uIndex";
    }

    /**
     * 跳转到普通用户登录页面
     * @return
     */
    @RequestMapping("/uLogin")
    public String uLogin(){
        return "/login/uLogin";
    }

    /**
     * 验证普通用户登录
     * @param request
     * @param user
     * @return
     */
    @RequestMapping("/toULogin")
    public String toULogin(HttpServletRequest request,User user){
        User byEntity = userService.getByEntity(user);
        if (byEntity==null){
            //return "/register/register";
            return "redirect:/login/register.action";
        }else{
            request.getSession().setAttribute("userId",byEntity.getId());
            request.getSession().setAttribute("username",byEntity.getRealName());
            request.getSession().setAttribute(Consts.USER,byEntity);
            return "redirect:/login/uIndex.action";
        }
    }

    /**
     * 用户退出
     * @param request
     * @return
     */
    @RequestMapping("exit")
    public String exit(HttpServletRequest request){
        /*request.setAttribute("userId",null);*/
        request.getSession().setAttribute("userId",null);
        request.getSession().setAttribute("username",null);
        request.getSession().setAttribute(Consts.USER,null);
        return "redirect:/login/uIndex.action";
    }

    /**
     * 跳转到修改密码页面
     * @param request
     * @param model
     * @return
     */
    @RequestMapping("/updatePW")
    public String updatePW(HttpServletRequest request,Model model){
        Object userId = request.getSession().getAttribute("userId");

        if (userId==null){
            return "redirect:/login/toULogin.action";
        }
        User user = userService.load(Integer.valueOf(userId.toString()));
        model.addAttribute("obj",user);
        return "login/updatePW";
    }
    @RequestMapping("/exUpdate")
    @ResponseBody
    public String exUpdate(HttpServletRequest request,String passWord){
        Object userId = request.getSession().getAttribute("userId");
        JSONObject js = new JSONObject();
        if (userId==null){
            js.put(Consts.RES,0);
        }
        User user = userService.load(Integer.valueOf(userId.toString()));
        user.setPassWord(passWord);
        userService.updateById(user);
        js.put(Consts.RES,1);
        return js.toString();
    }
}

用户登录页面:

<%@page language="java" contentType="text/html; character=UTF-8" pageEncoding="UTF-8" %>
<%@include file="/common/taglibs.jsp"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>用户登录</title>
    <link type="text/css" rel="stylesheet" href="${ctx}/resource/user/css/style.css">
    <script src="${ctx}/resource/user/js/jquery-1.8.3.min.js"></script>
    <%--轮播图js--%>
    <script src="${ctx}/resource/user/js/jquery.luara.0.0.1.min.js"></script>
</head>
<body>
<div class="width100 hidden_yh" style="height: 573px;background: url('${ctx}/resource/user/images/bj.jpg') no-repeat center">
    <div class="width1200 hidden_yh center_yh" style="margin-top: 75px;margin-left: 290px">
        <form action="${ctx}/login/toULogin" method="post">
         <h3 class="tcenter font30 c_33" style="font-weight: 100;margin-top: 36px;margin-bottom: 36px">账户登录</h3>
            <div class="center_yh hidden_yh" style="width: 336px;">
                <div class="width100 box-sizing hidden_yh" style="height: 44px;border: 1px solid #c9c9c9;margin-bottom: 34px">
                    <img src="${ctx}/resource/user/images/rw.jpg" alt="" class="left_yh" width="42" height="">
                    <input type="text" placeholder="请输入用户名" name="userName" value="" style="border: 0;width: 292px;height: 42px;font-size: 16px;text-indent: 22px">
                </div>


                <div class="width100 box-sizing hidden_yh" style="height: 44px;border: 1px solid #c9c9c9;margin-bottom: 34px">
                    <img src="${ctx}/resource/user/images/pass.jpg" alt="" class="left_yh" width="42" height="">
                    <input type="password" placeholder="请输入密码" name="passWord" value="" style="border: 0;width: 292px;height: 42px;font-size: 16px;text-indent: 22px">
                </div>
                <input type="submit" value="登录" class="center_yh" style="width: 100%;height: 43px;font-size: 16px;background: #dd4545;outline: none;border: 0;color: #fff;cursor:pointer">
            </div>
        </form>
    </div>
</div>
<%@include file="/common/ufooter.jsp"%>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/m0_59687645/article/details/121586984