Springmvc servlet api object as in the processing method using the reference

In springmvc, the controller does not rely on any servlet api object, servlet api object can be treated as the reference method used , is very easy, such as the need to use the HttpSession object, then it can be directly used as the reference HttpSession follows

@RequestMapping(value = "/dologin.html", method = RequestMethod.POST)
public String doLogin(@RequestParam String userCode, @RequestParam String userPassword,
                      HttpSession session, HttpServletRequest request){
    User user = userService.selectUserByUserCodeAndUserPassword(userCode, userPassword);
    if (null != user){
        session.setAttribute("userSession", user);
        return "redirect:/user/main.html";
    } The else {
         request.setAttribute ( "error", "user name or password is incorrect");
         return "Login" ;
    }
}

@RequestMapping("/main.html")
public String main(HttpSession session){
    if (session.getAttribute("userSession") == null){
        return "redirect:/user/login.html";
    }
    return "frame";
}

After the success of the current user login information into the HttpSession, for the same HttpServletRequest, the main user failed to log an error message.

the main method parameters into HttpSession also increased in vivo by the logic to determine whether there session currently logged in user, if not, the system proved not logged or session has expired

 

Guess you like

Origin www.cnblogs.com/yanguobin/p/11665378.html