SpringMVC存取Session的两种方法
WEB应用通常会引入Session,用来在服务端和客户端之间保存一系列动作/消息的状态,比如网上购物维护user登录信息直到user退出。SpringMVC存取Session的两种方法,具体如下:
方法一:使用servlet-api
@Controller publicclassManagerController{ @Resource privateManagerServicemanagerServiceImpl; @RequestMapping(value="manager/login.do",method=RequestMethod.GET) publicModelAndViewlogin(ManagerModelmanagerModel,HttpSessionhttpSession){ ManagerModelmanager=managerServiceImpl.getManager(managerModel); if(manager!=null){ manager.setPassword(""); httpSession.setAttribute("manager",manager); returnnewModelAndView(newRedirectView("../admin/main.jsp")); }else{ returnnewModelAndView(newRedirectView("../admin/login.jsp")); } } @RequestMapping(value="manager/logout.do",method=RequestMethod.GET) publicStringlogout(HttpSessionhttpSession){ httpSession.getAttribute("manager"); return"success"; } }
方法二:使用SessionAttributes
@Controller @SessionAttributes("manager") publicclassManagerController{ @Resource privateManagerServicemanagerServiceImpl; @RequestMapping(value="manager/login.do",method=RequestMethod.GET) publicModelAndViewlogin(ManagerModelmanagerModel,ModelMapmodel){ ManagerModelmanager=managerServiceImpl.getManager(managerModel); if(manager!=null){ manager.setPassword(""); model.addAttribute("manager",manager); returnnewModelAndView(newRedirectView("../admin/main.jsp")); }else{ returnnewModelAndView(newRedirectView("../admin/login.jsp")); } } @RequestMapping(value="manager/logout.do",method=RequestMethod.GET) publicStringlogout(@ModelAttribute("manager")ManagerModelmanagerModel){ return"success"; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。