SpringMVC如何设置、获取及清除Session

 时间:2024-10-13 10:31:20

1、在Controller上加上注解@SessionAttributes(value 屏顿幂垂= {"student1","student2"}),表明将student1和student2对象保存到session中

SpringMVC如何设置、获取及清除Session

2、setSession函数实现了向Session中添加student1和student2的功能

SpringMVC如何设置、获取及清除Session

3、以下两个函数clearSession、getSession分别实现了清除session和获取session中的student1及student2的功能

SpringMVC如何设置、获取及清除Session

4、输入url映射/testSession/setSession进入setSession方法设置student1,student2,并将其置入session中

SpringMVC如何设置、获取及清除Session

5、输入url映射/testSession/getSession进入getSession方法获取session中的属性student1,student2

SpringMVC如何设置、获取及清除Session

6、分别输入url映射/testSession/clearSession,/testSession/getSession清空session和获取session

SpringMVC如何设置、获取及清除Session

7、完成测试代码如下:@Controller@RequestMapping(value = "/testSession")刺胳挤萧@SessionAttributes(value = {"student1","student2"})public class SessionController{ @RequestMapping("setSession") public void setSession(Model model, HttpSession session) { Student student1 = new Student(); student1.setName("lisa"); student1.setAge(18); student1.setCountry("America"); Student student2 = new Student(); student2.setName("yc"); student2.setAge(20); student2.setCountry("China"); model.addAttribute("student1",student1); model.addAttribute("student2",student2); System.out.println("set session ok."); } @RequestMapping("clearSession") public void clearSession(SessionStatus sessionStatus, HttpSession session){ sessionStatus.setComplete(); System.out.println("clear session ok."); } @RequestMapping(value = "getSession") public void getSession(HttpSession session) { System.out.println("get session: student1 = " + session.getAttribute("student1")); System.out.println("get session: student2 = " + session.getAttribute("student2")); }}

SpringMVC如何设置、获取及清除Session
  • 如何使用系统信息工具查看网络适配器详细信息
  • 远程操作PLC上下载的方法
  • 发生火灾时如何逃生
  • WAP贴吧怎么玩
  • PLC维修的技巧
  • 热门搜索
    手抄报板块 三爱手抄报图片 法律进校园手抄报 小学科技手抄报内容 阅读手抄报的内容 教师节手抄报大全 英语手抄报国庆节 手抄报的版面设计 感恩节的手抄报 三年级数学手抄报内容