一、使用 ServletAPI 向 request 域对象共享数据
可以使用这种原生的 ServletAPI 向 request 域对象共享数据,示例如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testServletAPI}">测试ServletAPI向request域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testServletAPI")
public String testServletAPI(HttpServletRequest request){
request.setAttribute("testScope", "hello,servletAPI");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${testScope}"></p>
</body>
</html>
二、使用 ModelAndView 向 request 域对象共享数据
ModelAndView 有 Model 和 View 的功能,Model 主要用于向请求域共享数据,View 主要用于设置视图,实现页面跳转。如下:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testModelAndView}">测试ModelAndView向request域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView(){
ModelAndView mav=new ModelAndView();
// 向请求域共享数据
mav.addObject("testScope","hello,ModelAndView");
// 设置视图,实现页面跳转
mav.setViewName("success");
return mav;
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${testScope}"></p>
</body>
</html>
三、使用 Model 向 request 域对象共享数据
这里面的这个 Model 就是 ModelAndView 里面的 Model,
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testModel}">测试Model向request域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("testScope", "hello,Model");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${testScope}"></p>
</body>
</html>
四、使用 map 向 request 域对象共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testMap}">测试map向request域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("testScope", "hello,Map");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${testScope}"></p>
</body>
</html>
五、使用 ModelMap 向 request 域对象共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testModelMap}">测试ModelMap向request域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("testScope", "hello,ModelMap");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${testScope}"></p>
</body>
</html>
六、Model、ModelMap、Map 的关系
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 {}
七、向 session 域共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testSession}">测试向session域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("testSessionScope", "hello,session");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${session.testSessionScope}"></p>
</body>
</html>
八、向 application 域共享数据
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testApplication}">测试向Application域对象共享数据</a><br>
</body>
</html>
@Controller
public class ShareDataController {
@RequestMapping("/")
public String index() {
return "index";
}
@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplicationScope", "hello,application");
return "success";
}
}
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>success</h1><br>
<p th:text="${application.testApplicationScope}"></p>
</body>
</html>