首页 > 代码库 > SpringMVC存取Session的两种方法 转
SpringMVC存取Session的两种方法 转
方法一:使用servlet-api
[java] view plain copy print?
- @Controller
- public class ManagerController {
- @Resource
- private ManagerService managerServiceImpl;
- @RequestMapping(value = "manager/login.do",method = RequestMethod.GET)
- public ModelAndView login(ManagerModel managerModel,HttpSession httpSession){
- ManagerModel manager = managerServiceImpl.getManager(managerModel);
- if(manager!=null){
- manager.setPassword("");
- httpSession.setAttribute("manager", manager);
- return new ModelAndView(new RedirectView("../admin/main.jsp"));
- }else{
- return new ModelAndView(new RedirectView("../admin/login.jsp"));
- }
- }
- @RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)
- public String logout(HttpSession httpSession){
- httpSession.getAttribute("manager");
- return "success";
- }
- }
方法二:使用SessionAttributes
[java] view plain copy print?
- @Controller
- @SessionAttributes("manager")
- public class ManagerController {
- @Resource
- private ManagerService managerServiceImpl;
- @RequestMapping(value = "manager/login.do",method = RequestMethod.GET)
- public ModelAndView login(ManagerModel managerModel,ModelMap model){
- ManagerModel manager = managerServiceImpl.getManager(managerModel);
- if(manager!=null){
- manager.setPassword("");
- model.addAttribute("manager", manager);
- return new ModelAndView(new RedirectView("../admin/main.jsp"));
- }else{
- return new ModelAndView(new RedirectView("../admin/login.jsp"));
- }
- }
- @RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)
- public String logout(@ModelAttribute("manager")ManagerModel managerModel){
- return "success";
- }
- }
SpringMVC存取Session的两种方法 转
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。