@RequestMapping("save1")
public String s1(HttpSession session){
session.setAttribute("users1", "admin1111");
System.out.println("wr32r13");
return "session";
}
1.1.2 完整代码
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/session/save1">保存session1</a>
<a href="/session/clear1">清除session1</a><br>
HttpSession存储的值:${users1} <br>
</body>
</html>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
//session请求(常用)
@RequestMapping("save1")
public String s1(HttpSession session){
/*session里面的值需要重启一下才行吗*/
session.setAttribute("users1", "admin1111");
System.out.println("wr32r13");
return "session";
}
//清除方法
@RequestMapping("clear1")
public String c1(HttpSession session){
session.invalidate();
return "session";
}
}
这个HttpSession的使用方法和之前servlet学习阶段的方法是一致的
只要在一个会话范围内,所有页面都是可以获取改数据的
视图解析器中跳转界面的形式就是转发,因为地址栏不变
@RequestMapping("clear1")
public String c1(HttpSession session){
session.invalidate();
return "session";
}
1.2.2 完整代码
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>java
<head>
<title>Title</title>
</head>
<body>
<a href="/session/save1">保存session1</a>
<a href="/session/clear1">清除session1</a><br>
HttpSession存储的值:${users1} <br>
</body>
</html>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
//session请求(常用)
@RequestMapping("save1")
public String s1(HttpSession session){
/*session里面的值需要重启一下才行吗*/
session.setAttribute("users1", "admin1111");
System.out.println("wr32r13");
return "session";
}
//清除方法
@RequestMapping("clear1")
public String c1(HttpSession session){
session.invalidate();
return "session";
}
}
session.invalidate()是清除该session对象所有保存好的信息
@RequestMapping("get1")
public String get(HttpSession session){
System.out.println("进入了获取数据的方法");
String users1 = (String) session.getAttribute("users1");
System.out.println(users1);
return "session";
}
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
保存session1
清除session1
HttpSession存储的值:${users1}
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
//session请求(常用)
@RequestMapping("save1")
public String s1(HttpSession session){
/*session里面的值需要重启一下才行吗*/
session.setAttribute("users1", "admin1111");
System.out.println("wr32r13");
return "session";
}
//清除方法
@RequestMapping("clear1")
public String c1(HttpSession session){
session.invalidate();
return "session";
}
@RequestMapping("get1")
public String get(HttpSession session){
System.out.println("进入了获取数据的方法");
String users1 = (String) session.getAttribute("users1");
System.out.println(users1);
return "session";
}
}
这个HttpSession的使用方法和之前servlet学习阶段的方法是一致的
可以通过getAttribute方法获取数据,因为默认返回的是Object类型,因此需要指定类型需要进行强转
@SessionAttributes({"users2"})
//在类上写上该注解,但是需要在@Component这类主键的下面
@RequestMapping("save2")
public String s2(Model m){
/*保存的规范 key和注解里面一样*/
m.addAttribute("users2","zhangsan");
return "session";
}
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/session/save2">保存session2</a>
<a href="/session/clear2">清除session2</a><br>
session注解${users2}
</body>
</html>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
@RequestMapping("save2")
public String s2(Model m){
/*保存的规范 key和注解里面一样*/
m.addAttribute("users2","zhangsan");
return "session";
}
@RequestMapping("clear2")
public String c2(SessionStatus ss){
ss.setComplete();
/*redirect:/session.jsp重定向的方式*/
return "redirect:/session.jsp";
}
}
a 要想Model保存的数据同时,也赋值一份到特定的session域中,就必须要加上@SessionAttributes注解
b Model里面保存数据的key值需要和注解里面的值一模一样
@RequestMapping("clear2")
public String c2(SessionStatus ss){
ss.setComplete();
return "redirect:/session.jsp";
}
2.2.2 完整代码
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<a href="/session/save2">保存session2</a>
<a href="/session/clear2">清除session2</a><br>
session注解${users2}
</body>
</html>
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
@RequestMapping("save2")
public String s2(Model m){
/*保存的规范 key和注解里面一样*/
m.addAttribute("users2","zhangsan");
return "session";
}
@RequestMapping("clear2")
public String c2(SessionStatus ss){
ss.setComplete();
/*redirect:/session.jsp重定向的方式*/
return "redirect:/session.jsp";
}
}
ss.setComplete()会清除sessionAttribute方式上保存的session域中的数据
需要注意的是,需要重定向该页面,该数据才能在页面上不展示
@RequestMapping("get2")
public String get(@SessionAttribute(value = "users1",required = false) String u1){
System.out.println("users1:"+u1);
return "session";
}
<%--
Created by IntelliJ IDEA.
User: SSS翱翔万里
Date: 2022/11/14
Time: 15:49
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
保存session2
清除session2
session注解${users2}
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Controller
@RequestMapping("session")
@SessionAttributes({"users2"})
public class SessionController {
@RequestMapping("save2")
public String s2(Model m){
/*保存的规范 key和注解里面一样*/
m.addAttribute("users2","zhangsan");
return "session";
}
@RequestMapping("clear2")
public String c2(SessionStatus ss){
ss.setComplete();
return "redirect:/session.jsp";
}
@RequestMapping("get2")
public String get(@SessionAttribute(value = "users2",required = false) String u1){
System.out.println("users1:"+u1);
return "session";
}
}
通过@SessionAttributes保存的session域中的值,一般是通过@SessionAttribute注解的方式进行获取值