zoukankan      html  css  js  c++  java
  • SpringMVC存取Session的两种方法

    方法一:使用servlet-api

    @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

    @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";
        }
    }
  • 相关阅读:
    如何保证service不被系统杀死
    查找算法
    java多线程学习
    设计模式-单例
    Python2.7-内置类型
    Python2.7-内置函数
    准备要学的东西
    Python-2.7 : 编码问题及encode与decode
    【JZOJ4637】大鱼海棠【博弈论】
    【JZOJ4637】大鱼海棠【博弈论】
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5026125.html
Copyright © 2011-2022 走看看