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

    SpringMVC存取Session的两种方法

    不推荐使用方法一,解释在02章

    方法一:使用servlet-api

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    1. @Controller  
    2. public class ManagerController {  
    3.   
    4.     @Resource  
    5.     private ManagerService managerServiceImpl;  
    6.       
    7.     @RequestMapping(value = "manager/login.do",method = RequestMethod.GET)    
    8.     public ModelAndView login(ManagerModel managerModel,HttpSession httpSession){  
    9.           
    10.         ManagerModel manager = managerServiceImpl.getManager(managerModel);  
    11.         if(manager!=null){  
    12.             manager.setPassword("");  
    13.             httpSession.setAttribute("manager", manager);  
    14.             return new ModelAndView(new RedirectView("../admin/main.jsp"));  
    15.         }else{  
    16.             return new ModelAndView(new RedirectView("../admin/login.jsp"));  
    17.         }  
    18.     }  
    19.       
    20.     @RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)  
    21.     public String logout(HttpSession httpSession){  
    22.         httpSession.getAttribute("manager");  
    23.         return "success";  
    24.     }  
    25. }  



    方法二:使用SessionAttributes

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
      1. @Controller  
      2. @SessionAttributes("manager")  
      3. public class ManagerController {  
      4.   
      5.     @Resource  
      6.     private ManagerService managerServiceImpl;  
      7.       
      8.     @RequestMapping(value = "manager/login.do",method = RequestMethod.GET)    
      9.     public ModelAndView login(ManagerModel managerModel,ModelMap model){  
      10.           
      11.         ManagerModel manager = managerServiceImpl.getManager(managerModel);  
      12.         if(manager!=null){  
      13.             manager.setPassword("");  
      14.             model.addAttribute("manager", manager);  
      15.             return new ModelAndView(new RedirectView("../admin/main.jsp"));  
      16.         }else{  
      17.             return new ModelAndView(new RedirectView("../admin/login.jsp"));  
      18.         }  
      19.     }  
      20.       
      21.     @RequestMapping(value = "manager/logout.do",method = RequestMethod.GET)  
      22.     public String logout(@ModelAttribute("manager")ManagerModel managerModel){  
      23.         return "success";  
      24.     }  
      25. }  
  • 相关阅读:
    python 类定义 继承
    BAYSY2 的LVDS引脚 笔记
    Fedora20-32bit cross-compiling arm-linux-gcc4.3.2
    以冒泡排序为例--malloc/free 重定向stdin stdout
    笔记:程序内存管理 .bss .data .rodata .text stack heap
    第一章 数值和码制
    《将博客搬至CSDN》
    Servlet 3.0 新特性
    java Servlet接口及应用
    C语言输出单个汉字字符
  • 原文地址:https://www.cnblogs.com/handsome1013/p/5300913.html
Copyright © 2011-2022 走看看