@Test public void loginCheckByMock() throws Exception { request.setRequestURI("/loginCheck.html"); request.addParameter("userName", "tom"); request.addParameter("password", "123456"); ModelAndView mav = controller.loginCheck(request); User user = (User) request.getSession().getAttribute("user"); assertNotNull(mav); assertEquals(mav.getViewName(), "main"); assertNotNull(user); assertThat(user.getUserName(), equalTo("tom")); assertThat(user.getCredit(), greaterThan(5)); }
@RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request){ User user = new User(); user.setUserName(request.getParameter("userName")); user.setPassword(request.getParameter("password")); boolean isValidUser = userService.hasMatchUser(user.getUserName(), user.getPassword()); if (!isValidUser) { return new ModelAndView("login", "error", "用户名或密码错误。"); } else { user = userService.findUserByUserName(user .getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user", user); return new ModelAndView("main"); } }
LoginController { @RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request){ User user = new User(); user.setUserName(request.getParameter("userName")); user.setPassword(request.getParameter("password")); boolean isValidUser = userService.hasMatchUser(user.getUserName(), user.getPassword()); if (!isValidUser) { return new ModelAndView("login", "error", "用户名或密码错误。"); } else { user = userService.findUserByUserName(user .getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user", user); return new ModelAndView("main"); } } }
LoginController { @RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request){ User user = new User(); user.setUserName(request.getParameter("userName")); user.setPassword(request.getParameter("password")); boolean isValidUser = userService.hasMatchUser(user.getUserName(), user.getPassword()); if (!isValidUser) { return new ModelAndView("login", "error", "用户名或密码错误。"); } else { user = userService.findUserByUserName(user .getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user", user); return new ModelAndView("main"); } }  }
LoginController { @RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request){ User user = new User(); user.setUserName(request.getParameter("userName")); user.setPassword(request.getParameter("password")); boolean isValidUser = userService.hasMatchUser(user.getUserName(), user.getPassword()); if (!isValidUser) { return new ModelAndView("login", "error", "用户名或密码错误。"); } else { user = userService.findUserByUserName(user .getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user", user); return new ModelAndView("main"); } }  @RequestMapping(value = "/index.html") String loginPage(); @RequestMapping(value = "/loginCheck.html") ModelAndView loginCheck(HttpServletRequest request); @Autowired void setUserService(UserService userService); }
LoginController { @RequestMapping(value = "/loginCheck.html") public ModelAndView loginCheck(HttpServletRequest request){ User user = new User(); user.setUserName(request.getParameter("userName")); user.setPassword(request.getParameter("password")); boolean isValidUser = userService.hasMatchUser(user.getUserName(), user.getPassword()); if (!isValidUser) { return new ModelAndView("login", "error", "用户名或密码错误。"); } else { user = userService.findUserByUserName(user .getUserName()); user.setLastIp(request.getLocalAddr()); user.setLastVisit(new Date()); userService.loginSuccess(user); request.getSession().setAttribute("user", user); return new ModelAndView("main"); } }  @RequestMapping(value = "/index.html") String loginPage(); @RequestMapping(value = "/loginCheck.html") ModelAndView loginCheck(HttpServletRequest request); @Autowired void setUserService(UserService userService);  }