从首页点击注册进入注册register-1.
在index.html 的topbar加入超链接引入注册的方法</i><a th:href="initGuestRegister" class="link">注册</a>,
在guestcontroller.java中写入initGuesReigster方法返回注册页面初始化
@RequestMapping(value = "initGuestRegister", method = RequestMethod.GET) public String initGuestRegister(Model model, Device device) { log.info("客户注册界面初始化"); GoodsForm goodsForm = new GoodsForm(); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm); model.addAttribute("commodityType", commodityType); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); GuestForm guestForm = new GuestForm(); model.addAttribute("guestForm", guestForm); if(device.isNormal()) { return "shop/register-1"; } else { return "mobile/register"; } }
在注册页面register-1.html中 要利用validation来判断输入的格式,(关键代码)
<span th:if="${#fields.hasErrors('${guestForm.*}')}"><span th:errors="${guestForm.*}"></span></span>
<span th:text="${message}"></span>
在页面上的每一个字段的name都要和guestform.java中的一致(例如在前台页面有个input name是 guestId ,那么在gusetForm.java中也一定有个gusetId),,并且需要th:value来传入值(th:value="${guestForm.guestId}" )
输入成功后通过form表单action提交利用Gugestcontroller.java中的addguest方法,成功后返回到register-2.html
在register-1.html
<body> <div> <div th:replace="shop/topBar :: page-top-bar"></div> </div> <!-- top over --> <div> <div> <a href="@{/}"><img src="images1/logo.jpg" alt=""/></a> <span th:if="${#fields.hasErrors('${guestForm.*}')}"><span th:errors="${guestForm.*}"></span></span> <span th:text="${message}"></span> <span class="tit yh">新用户注册</span> </div> </div> <!-- header over --> <div class="container mt20 regBox"> <form action="addGuest" th:object="${guestForm}" method="post"> <div> <span><label class="col-sm-4 control-label">用户名:<span style="display:inline;color:red;">*</span></label></span> <div> <span><input name="guestId" type="text" th:value="${guestForm.guestId}"/></span> </div> </div> <div> <span><label class="col-sm-4 control-label">密码:<span style="display:inline;color:red;">*</span></label></span> <div> <span><input name="password" type="password"/></span> </div> </div> <div> <span><label class="col-sm-4 control-label">重复密码:</label></span> <div> <span><input name="passwordConfirm" type="password"/></span> </div> </div> <div> <span><label class="col-sm-4 control-label">姓名<span style="display:inline;color:red;">*</span></label></span> <div> <span><input name="guestName" type="text" th:value="${guestForm.guestName}"/></span> </div> </div> <div> <span><label class="col-sm-4 control-label">性别</label></span> <div> <span><input name="gender" type="text" th:value="${guestForm.gender}"/></span> </div> </div> <div> <span><label class="col-sm-4 control-label">收货地址<span style="display:inline;color:red;">*</span></label></span> <div> <span><input name="address" type="text" th:value="${guestForm.address}"/></span> </div> </div> <div> <div class="col-sm-offset-4 col-sm-8"> <span><input type="submit" value="提交" /></span> </div> </div> </form> <!-- 注册 --> </div>
在guestController.java
@RequestMapping(value = "addGuest", method = RequestMethod.POST) public String executeAddGuest(Model model,HttpSession session, @Valid @ModelAttribute("guestForm") GuestForm guestForm, BindingResult results, Device device) throws SQLException { model.addAttribute("guestForm", guestForm); GoodsForm goodsForm = new GoodsForm(); // goodsForm.setType("粮食"); // model.addAttribute("commodityType", goodsService.getType(goodsForm)); // model.addAttribute("goodsForm", goodsForm); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm); model.addAttribute("commodityType", commodityType); if (results.hasErrors()) { log.info("内容验证出错"); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if(device.isNormal()) { return "shop/register-1"; } else { return "mobile/register"; } } if(guestForm.getGuestId().length() > 4 && "Guest".equals(guestForm.getGuestId().substring(0, 5))) { log.info("ID验证出错"); model.addAttribute("message", "Guest是系统预留关键字,请避免使用!"); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if(device.isNormal()) { return "shop/register-1"; } else { return "mobile/register"; } } if (!guestForm.getPassword().equals(guestForm.getPasswordConfirm())) { log.info("密码验证出错"); model.addAttribute("message", "密码和密码确认必须一致!"); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if(device.isNormal()) { return "shop/register-1"; } else { return "mobile/register"; } } log.info("添加客户信息"); guestForm.setUpdateUser(guestForm.getGuestId()); Date date = new Date(); SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); guestForm.setUpdateTime(dateformat.format(date)); boolean result = guestService.addGuest(guestForm); if(!result) { //throw new SQLException("客户信息添加失败!"); model.addAttribute("message", "该用户ID已被占用,请更换用户ID!"); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if(device.isNormal()) { return "shop/register-1"; } else { return "mobile/register"; } } UVO uvo = new UVO(); uvo.setGuestId(guestForm.getGuestId()); uvo.setGuestName(guestForm.getGuestName()); uvo.setPassword(guestForm.getPassword()); uvo.setGender(guestForm.getGender()); uvo.setAddress(guestForm.getAddress()); uvo.setEmail(guestForm.getEmail()); uvo.setMobile(guestForm.getMobile()); uvo.setQq(guestForm.getQq()); uvo.setPhone(guestForm.getPhone()); uvo.setZip(guestForm.getZip()); session.setAttribute("UVO", uvo); // GoodsForm goodsForm = new GoodsForm(); // goodsForm.setType("粮食"); // model.addAttribute("commodityType", goodsService.getType(goodsForm)); // model.addAttribute("goodsForm", goodsForm); model.addAttribute("list", goodsService.searchGoodsList(goodsForm)); CartForm cartForm = new CartForm(); cartForm.setGuestId(uvo.getGuestId()); model.addAttribute("cartList", cartService.searchCartList(cartForm)); if(device.isNormal()) { return "shop/register-2"; } else { return "mobile/index"; } }
注册成功后进入register-2.html页面可以点击登陆超链接<a href="@{initGuestLogin}" class="link">去登录</a>调用initGuestLogin方法进入登陆页面。登陆成功后返回首页
@RequestMapping(value = "initGuestLogin", method = RequestMethod.GET) public String initGuestLogin(Model model, Device device) { log.info("客户登录界面初始化"); GoodsForm goodsForm = new GoodsForm(); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm); model.addAttribute("commodityType",commodityType); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); GuestForm guestForm = new GuestForm(); model.addAttribute("guestForm", guestForm); if(device.isNormal()) { return "shop/login"; } else { return "mobile/login"; } }
进入登陆页面后输入正确用户名和密码,通过 form表单 action(guestLogin)方法进行提交。
@RequestMapping(value = "guestLogin", method = RequestMethod.POST) public String guestLogin(Model model, HttpSession session, GuestForm guestForm, Device device) { log.info("客户登录,验证客户信息,成功后进入系统"); GuestForm result = guestService.searchGuest(guestForm); GoodsForm goodsForm = new GoodsForm(); List<GoodsForm> commodityType = goodsService.getType(); goodsForm.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm); model.addAttribute("commodityType", commodityType); if(result != null) { UVO uvo = new UVO(); uvo.setGuestId(result.getGuestId()); uvo.setGuestName(result.getGuestName()); uvo.setPassword(guestForm.getPassword()); uvo.setGender(result.getGender()); uvo.setAddress(result.getAddress()); uvo.setEmail(result.getEmail()); uvo.setMobile(result.getMobile()); uvo.setQq(result.getQq()); uvo.setPhone(result.getPhone()); uvo.setZip(result.getZip()); session.setAttribute("UVO", uvo); GoodsForm goodsForm1 = new GoodsForm(); List<GoodsForm> commodityType1 = goodsService.getType(); goodsForm1.setCommodityTypeId(commodityType.get(0).getCommodityTypeId()); model.addAttribute("goodsForm", goodsForm1); model.addAttribute("commodityType",commodityType1); model.addAttribute("list", goodsService.searchGoodsList(goodsForm1)); GoodsForm goodsFormForId = new GoodsForm(); for(int i=0;i<commodityType1.size();i++){ goodsFormForId.setCommodityTypeId(commodityType1.get(i).getCommodityTypeId()); commodityType1.get(i).setList(goodsService.searchGoodsListLimit(goodsFormForId)); } CartForm cartForm = new CartForm(); cartForm.setGuestId(uvo.getGuestId()); model.addAttribute("cartList", cartService.searchCartList(cartForm)); if(device.isNormal()) { return "shop/index"; } else { return "mobile/index"; } } else { model.addAttribute("message", "用户名或密码错误!"); List<CartForm> cartList = new ArrayList<>(); model.addAttribute("cartList", cartList); if(device.isNormal()) { return "shop/login"; } else { return "mobile/login"; } } }
在login.html页面
<form action="guestLogin" th:object="${guestForm}" method="post" class="form-horizontal"> <div class="form-group"> <label for="inputEmail3" class="col-sm-4 control-label">用户名:</label> <div class="col-sm-8"> <input type="text" name="guestId" class="form-control" /> </div></div> <div class="form-group"> <label for="inputPassword3" class="col-sm-4 control-label">密 码:</label> <div class="col-sm-8"> <input type="password" name="password" class="form-control"/> <span th:if="${#fields.hasErrors('${guestForm.*}')}"> <span th:errors="${guestForm.*}"></span></span> <span th:text="${message}"></span> </div> </div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-8"> <div class="checkbox w300 cf"><label class="fl"><input id="saveCookie" type="checkbox" value=""/> 记住密码</label><a href="#" class="fr link">忘记密码?</a></div> </div></div> <div class="form-group"> <div class="col-sm-offset-4 col-sm-8"> <button type="submit" class="btnYellow yh">登 录</button></div></div></form><!-- 登录 --></div>