@RequestMapping(value = "/login", method = RequestMethod.POST) public String handleLogin( @ModelAttribute("user") User user, BindingResult errors, Model model, RedirectAttributes redirectAttributes, HttpServletRequest request) { if (errors.hasErrors()) { return "login"; } try { User loginUser = userBO.login(user.getUserName(), user.getPassword()); if (loginUser != null) { request.getSession().setAttribute("LOGIN_USER", loginUser); return "redirect:welcome"; } else { // model.addAttribute("error", "Wrong UserName and Password."); // return "login"; redirectAttributes.addFlashAttribute("error", "Wrong UserName and Password."); return "redirect:login"; } } catch (Exception e) { e.printStackTrace(); redirectAttributes.addFlashAttribute("error", e.getMessage()); return "redirect:login"; } }
@ResponseBody @RequestMapping(value = "/checkEmailExists", produces = "application/json") public AjaxResponse checkEmailExists(@RequestParam("email") String email) { boolean exists = userBO.checkEmailExists(email); if (exists) { return new AjaxResponseBuilder().notOk().error("Email [" + email + "] already exist").build(); } else { return new AjaxResponseBuilder().ok().build(); } }
@ResponseBody @RequestMapping(value = "/checkUserNameExists", produces = "application/json") public AjaxResponse checkUserNameExists(@RequestParam("userName") String userName) { boolean exists = userBO.checkUserNameExists(userName); if (exists) { return new AjaxResponseBuilder() .notOk() .error("UserName [" + userName + "] already exist") .build(); } else { return new AjaxResponseBuilder().ok().build(); } }
@RequestMapping(value = "/register", method = RequestMethod.POST) public String handleRegistration( @ModelAttribute("user") User user, BindingResult errors, Model model, RedirectAttributes redirectAttributes) { if (errors.hasErrors()) { return "register"; } try { userBO.createUser(user); redirectAttributes.addFlashAttribute("msg", "Registered successfully"); return "redirect:login"; } catch (Exception e) { e.printStackTrace(); // redirectAttributes.addFlashAttribute("error", e.getMessage()); // redirectAttributes.addFlashAttribute("user",user); model.addAttribute("error", e.getMessage()); return "register"; } }