使用ModelAndView向request域对象共享数据:
使用ModelAndView时,可以使用其Model功能向请求域共享数据
使用View功能设置逻辑视图,但是控制器方法一定要将ModelAndView作为方法的返回值
举例如下所示:
创建控制器方法:
java
package Controller.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class modelAndViewController {
@RequestMapping(value = "/param/pojo")
public String modelAndViewTesting(HttpServletRequest request){
//request.setAttribute:是用来设置请求对象testScope的属性,该属性的值为hello,ModelAndView,此方法允许将数据从一个servlet传递到另一个servlet,或者从servlet传递到JSP页面,
request.setAttribute("testScope","hello,ModelAndView");
//获取请求对象testScope的属性
System.out.println(request.getAttribute("testSCope"));
return "success";
}
}
index.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{test/mdv}">通过modelandview向请求域共享数据</a>
</body>
</html>
success.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>success.html</h1>
<!--显示存储在请求作用域中的名为"${}"的变量的值 -->
<p th:text="${testRequestScope}"></p>
</body>
</html>
项目重新部署后,浏览器显示如下:
点击该超链接后跳转:
成功将请求对象名称为testScope的属性值显示出来!
使用Model向请求域共享数据:
创建控制器方法:
java
package Controller.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class modelAndViewController {
@RequestMapping("/test/model")
public String modelTesting(Model model){
model.addAttribute("testRequestScope","hello,Model");
return "success";
}
}
index.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{test/model}">通过model向请求域共享数据</a>
</body>
</html>
success.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testRequestScope}"></p>
</body>
</html>
重新部署项目,浏览器显示如下:
点击超链接:
显示如下:
使用ModelMap向请求域共享数据:
创建控制器方法:
java
package Controller.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class modelAndViewController {
@RequestMapping("/test/modelMap")
public String modelTesting(ModelMap modelMap){
modelMap.addAttribute("testRequestScope","hello,ModelMap");
return "success";
}
}
index.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{test/modelMap}">通过modelMap向请求域共享数据</a>
</body>
</html>
success.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testRequestScope}"></p>
</body>
</html>
重新部署项目,浏览器显示如下:
点击超链接:
显示如下:
使用Map向请求域共享数据:
创建控制器方法:
java
package Controller.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
@Controller
public class modelAndViewController {
@RequestMapping("/test/Map")
public String modelTesting(Map<String,Object> map){
map.put("testRequestScope","hello,map");
return "success";
}
}
index.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{test/Map}">通过Map向请求域共享数据</a>
</body>
</html>
success.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testRequestScope}"></p>
</body>
</html>
重新部署项目,浏览器显示如下:
点击超链接:
显示如下:
model,modelMap以及Map三者之间的关系:
三者通过getClass().getName()调用方法,其结果都是org.springframework.validation.support.BindingAwareModelMap
,由此我们可以得知在底层中,这三者的形参最终都是通过BindingAwareModelMap
创建的,其三者的关系如下所示:
java
public class BindingAwareModelMap extends ExtendedModelMap {
public class ExtendedModelMap extends ModelMap implements Model {
public class ModelMap extends LinkedHashMap<String, Object> {
向会话域和应用域 共享数据:
控制器方法:
java
package Controller.pojo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
@Controller
public class shareDataController {
@RequestMapping("/test/Session")
public String sessionTesting(HttpSession httpSession){
httpSession.setAttribute("testRequestScope","hello,Session");
return "success";
}
@RequestMapping("/test/Application")
public String applicationTesting(HttpSession httpSession){
ServletContext servletContext=httpSession.getServletContext();
servletContext.setAttribute("testRequestScope","hello,Application");
return "success";
}
}
index.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>首页</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{test/Session}">测试向会话域中共享数据</a><br>
<a th:href="@{test/Application}">测试向应用域中共享数据</a>
</body>
</html>
success.html:
html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>成功页面</title>
</head>
<body>
<h1>success.html</h1>
<p th:text="${testRequestScope}"></p>
<!--获取session和application中的共享数据 -->
<p th:text="${session.testRequestScope}"></p>
<p th:text="${application.testRequestScope}"></p>
</body>
</html>
项目
将当前的地址复制,关闭浏览器后,重新打开,如下所示,我们会发现session共享的数据已经不存在了
不关闭浏览器,选择model/modelMap/Map中的任意一种向请求域共享数据,再重新部署项目(重新部署项目相当于重启服务器),如下所示:
上面我们说到session是在一次会话中有效,只要会话没有关闭,那么共享的数据就不会消失,但是这里为什么消失了呢?
原因是与IDE有关,如下所示:
勾选该选项的作用是:当服务器关闭不清空session