1、使用ServletAPI向request域对象共享数据
Java代码:
@Controller
@RequestMapping("/test/scope")
public class TestScopeController {
@RequestMapping("/servlet")
public String testScopeByServletAPI(HttpServletRequest request) {
request.setAttribute("testRequestScope", "hello servlet");
return "target";
}
}
index.html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" xmlns:th="http://www.thymeleaf.org">
<title>首页</title>
</head>
<body>
<h1>首页</h1>
<a th:href="@{/test/scope/servlet}">测试ServletAPI向request域对象共享数据</a>
</body>
</html>
target.html 代码:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Target</title>
</head>
<body>
<h1>Hello World</h1>
<p th:text="${testRequestScope}"></p>
</body>
</html>
点击 index.html 中的超链接,结果为:
2、使用ModelAndView向request域对象共享数据
Java代码:
@RequestMapping("/modelAndView")
public ModelAndView testScopeByModelAndView() {
/**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
ModelAndView mav = new ModelAndView();
// 向请求域共享数据
mav.addObject("testScopeByModelAndView", "hello testScopeByModelAndView");
// 设置视图,实现页面跳转
mav.setViewName("target");
return mav;
}
index.html代码:
<a th:href="@{/test/scope/modelAndView}">测试 modelAndView 向 request 域对象共享数据</a><br/>
点击 index.html 中的目标超链接,结果为:
3、使用Model、ModelMap、Map向request域对象共享数据
Java代码:
@RequestMapping("/model")
public String testScopeByModel(Model model) {
System.out.println(model.getClass().getName());
// org.springframework.validation.support.BindingAwareModelMap
model.addAttribute("testRequestScope", "hello model");
return "target";
}
@RequestMapping("/modelMap")
public String testScopeByModel(ModelMap modelMap) {
System.out.println(modelMap.getClass().getName());
// org.springframework.validation.support.BindingAwareModelMap
modelMap.addAttribute("testRequestScope", "hello ModelMap");
return "target";
}
@RequestMapping("/map")
public String testScopeByMap(Map<String, Object> map) {
System.out.println(map.getClass().getName());
// org.springframework.validation.support.BindingAwareModelMap
map.put("testRequestScope", "hello map");
return "target";
}
index.html代码:
<a th:href="@{/test/scope/model}">测试 Model 向 request 域对象共享数据</a><br/>
<a th:href="@{/test/scope/modelMap}">测试 ModelMap 向 request 域对象共享数据</a><br/>
<a th:href="@{/test/scope/map}">测试 Map 向 request 域对象共享数据</a><br/>
Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的
public interface Model{}public class ModelMap extends LinkedHashMap<String, Object> {}public class ExtendedModelMap extends ModelMap implements Model {}public class BindingAwareModelMap extends ExtendedModelMap {}
4、向session域共享数据
Java代码:
@RequestMapping("/session")
public String testSession(HttpSession session) {
session.setAttribute("testSessionScope","hello session");
return "target";
}
index.html代码:
<a th:href="@{/test/scope/session}">测试向 session 域对象共享数据</a><br/>
target.html代码:
<body>
<h1>Hello World</h1>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSessionScope}"></p>
</body>
点击 index.html 中的对应的目标超链接,结果为:
5、向application域共享数据
Java代码:
@RequestMapping("/application")
public String testApplication(HttpSession session) {
ServletContext application = session.getServletContext();
application.setAttribute("testApplication", "hello application");
return "target";
}
index.html代码:
<a th:href="@{/test/scope/application}">测试向 application 域对象共享数据</a><br/>
target.html代码:
<body>
<h1>Hello World</h1>
<p th:text="${testRequestScope}"></p>
<p th:text="${session.testSessionScope}"></p>
<p th:text="${application.testApplication}"></p>
</body>
点击 index.html 中的对应的目标超链接,结果为: