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";
        }
    }
  • 相关阅读:
    深入了解 JavaScript 中的 for 循环
    JavaScript 字符串常用操作
    10个习惯助你成为一名优秀的程序员
    Ubuntu日常问题搜集和解决办法
    提升你的开发效率,10 个 NPM 使用技巧
    Redux教程3:添加倒计时
    Redux教程2:链接React
    Redux教程1:环境搭建,初写Redux
    React + Redux 入坑指南
    12个非常实用的JavaScript小技巧
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/5026125.html
Copyright © 2011-2022 走看看