域对象有4个:
①page(PageContextImpl):当前jsp页面范围内有效
②request(HttpServletRequest):一次请求响应范围有效,同一客户端的不同请求,无法获取域对象中的值
③session(HttpSession):一次会话范围有效,同一客户端在一次会话内的多个请求,都可以获取到session保存作用域内的值;可以看成一次浏览器关闭
④application(ServletContext):一次应用程序范围;可以看成一次服务器关闭
一. 向request域对象共享数据
1. 通过ServletAPI向request域对象共享数据
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/requestByServletAPI")
public String testRequestByServletAPI(HttpServletRequest request){
request.setAttribute("requestScope","hello");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/requestByServletAPI}">通过servletAPI向request域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${requestScope}"></p> <!-- 获取request域对象中的数据 -->
<!-- 获取到的结果是hello -->
</body>
</html>
2. 使用ModelAndView向request域对象共享数据(建议使用)
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/requestByModelAndView")
public ModelAndView testRequestByModelAndView(){
ModelAndView mav = new ModelAndView();
//向request域中存数据
mav.addObject("requestScope","hello,ModelAndView");
//设置视图名称
mav.setViewName("test");
return mav;
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/requestByModelAndView}">通过ModelAndView向request域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${requestScope}"></p> <!-- 获取request域对象中的数据 -->
<!-- 获取到的结果是hello,ModelAndView -->
</body>
</html>
3. 使用Model向request域对象共享数据
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/requestByModel")
public String testRequestByModel(Model model){
model.addAttribute("requestScope", "hello,Model");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/requestByModel}">通过Model向request域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${requestScope}"></p> <!-- 获取request域对象中的数据 -->
<!-- 获取到的结果是hello,Model -->
</body>
</html>
4. 使用map向request域对象共享数据
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/requestByMap")
public String testRequestByMap(Map<String,Object> map){
map.put("requestScope", "hello,Map");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/requestByMap}">通过Map向request域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${requestScope}"></p> <!-- 获取request域对象中的数据 -->
<!-- 获取到的结果是hello,Map -->
</body>
</html>
5. 使用ModelMap向request域对象共享数据
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/requestByModelMap")
public String testRequestByModelMap(ModelMap modelMap){
modelMap.addAttribute("requestScope", "hello,ModelMap");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/requestByModelMap}">通过ModelMap向request域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${requestScope}"></p> <!-- 获取request域对象中的数据 -->
<!-- 获取到的结果是hello,ModelMap -->
</body>
</html>
二. 向session域对象共享数据
1. 通过ServletAPI向session域对象共享数据(建议使用)
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("sessionScope", "hello,session");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/testSession}">p向session域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${session.sessionScope}"></p> <!-- 获取session域对象中的数据,需要加上session. -->
<!-- hello,session -->
</body>
</html>
三. 向application域对象共享数据
1. 通过ServletAPI向application域对象共享数据(建议使用)
package com.mvc.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
@Controller
public class ScopeController {
@RequestMapping("/") //必须要有该映射路径,这样启动Tomcat服务器时才能找到index.html页面
public String index(){
return "index";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("applicationScope", "hello,application");
return "test";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>index页面</h1>
<a th:href="@{/testApplication}">向application域对象共享数据</a>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>test页面</h1>
<p th:text="${application.applicationScope}"></p> <!-- 获取application域对象中的数据,需要加上application. -->
<!-- hello,application -->
</body>
</html>