目录
测试网页代码
测试网页的所有代码,使用了thymeleaf。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<a th:href="@{/testAPI}">测试原生API向request域对象共享数据</a><br>
<a th:href="@{/testModelAndView}">测试ModelAndView向request域对象共享数据</a><br>
<a th:href="@{/testMode1}">测试Model向request域对象共享数据</a><br>
<a th:href="@{/testMap}">测试Map向request域对象共享数据</a><br>
<a th:href="@{/testModelMap}">测试ModelMap向request域对象共享数据</a><br>
<a th:href="@{/testSession}">测试session域对象共享数据</a><br>
<a th:href="@{/testApplication}">测试testApplication域对象共享数据</a><br>
</body>
</html>
接收网页的所有代码,使用了thymeleaf。
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>测试</title>
</head>
<body>
<h1>测试成功</h1>
<p th:text="${key1}"></p>
<p th:text="${key2}"></p>
<p th:text="${key3}"></p>
<p th:text="${key4}"></p>
<p th:text="${key5}"></p>
<p th:text="${session.key6}"></p>
<p th:text="${application.key7}"></p>
</body>
</html>
request域对象
request域的各种共享数据的方法使用起来大同小异
原生Servlet共享数据
原生API作为基础不多解释,直接看代码
html代码
<a th:href="@{/testAPI}">测试原生API向request域对象共享数据</a><br>
Servlet代码
@RequestMapping(value = "/testAPI")
public String testAPI(HttpServletRequest request) {
request.setAttribute("key1", "value1");
return "test";
}
页面接收代码
<p th:text="${key1}"></p>
ModelAndView共享数据
html代码
<a th:href="@{/testModelAndView}">测试ModelAndView向request域对象共享数据</a><br>
接收代码
@RequestMapping("/testModelAndView")
public ModelAndView testModelAndView() {
ModelAndView mav = new ModelAndView();
mav.addObject("key2", "value2");//添加数据
mav.setViewName("test");//设置视图名称
return mav;
}
页面接收代码
<p th:text="${key2}"></p>
这种方式代码长度和原生API相比还更加长了,但在springMVC中推荐我们使用这一种方式。
Model共享数据
html代码
<a th:href="@{/testMode1}">测试Model向request域对象共享数据</a><br>
接收代码
@RequestMapping("/testMode1")
public String testMode1(Model model) {
model.addAttribute("key3", "value3");
System.out.println(model.getClass().getName());
return "test";
}
页面接收代码
<p th:text="${key3}"></p>
map共享数据
html代码
<a th:href="@{/testMap}">测试Map向request域对象共享数据</a><br>
接收代码
@RequestMapping("/testMap")
public String testMap(Map<String, Object> map) {
map.put("key4", "value4");
System.out.println(map.getClass().getName());
return "test";
}
页面接收代码
<p th:text="${key4}"></p>
ModelMap共享数据
html代码
<a th:href="@{/testModelMap}">测试ModelMap向request域对象共享数据</a><br>
接收代码
@RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap) {
modelMap.addAttribute("key5", "value5");
System.out.println(modelMap.getClass().getName());
return "test";
}
页面接收代码
<p th:text="${key5}"></p>
其他知识
(1)Model、ModelMap、Map类型的参数其实本质上都是 BindingAwareModelMap 类型的。上面的Model、ModelMap、Map方法中的输出内容都是BindingAwareModelMap。
(2)不管使用上面方法中的哪一种方法,最终返回的都是打包好的ModelAndView对象,也就是上面所写的第二种方法,这也是为什么springMVC推荐使用这一种方式来进行共享数据操作。
session域对象
html代码
<a th:href="@{/testSession}">测试session域对象共享数据</a><br>
接收代码
@RequestMapping("/testSession")
public String testSession(HttpSession session) {
session.setAttribute("key6", "value6");
return "test";
}
页面接收代码
session域接收数据时略有不同,要指定是session域下的键值
<p th:text="${session.key6}"></p>
application域对象
html代码
<a th:href="@{/testApplication}">测试testApplication域对象共享数据</a><br>
接收代码
@RequestMapping("/testApplication")
public String testApplication(HttpSession session) {
ServletContext application = session.getServletContext();
application.setAttribute("key7", "value7");
return "test";
}
页面接收代码
<p th:text="${application.key7}"></p>