使用ModelAndView向request域对象共享数据

index.html

<a th:href="@{/testModelAndView}">使用ModelAndView</a>



控制器

    /**
* ModelAndView有Model和View的功能
* Model主要用于向请求域共享数据
* View主要用于设置视图,实现页面跳转
*/
@RequestMapping("/testModelAndView")
public ModelAndView success(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("username","gonghr"); //向请求域共享数据
modelAndView.setViewName("success"); //设置视图名称,实现页面跳转
return modelAndView; //返回
}

success.html

sucess
<p th:text="${username}"></p>

使用Model向request域对象共享数据

index.html

<a th:href="@{/testModel}">使用Model</a> <br>

控制器

    @RequestMapping("/testModel")
public String testModel(Model model){
model.addAttribute("company","JLU");
return "success";
}

success.html

sucess
<p th:text="${company}"></p> <br>

使用map向request域对象共享数据

index.html

<a th:href="@{/testMap}">使用Map</a> <br>

控制器

    @RequestMapping("/testMap")
public String testMap(Map<String, Object> map){
map.put("age","Nineteen");
return "success";
}

sucess.html

sucess
<p th:text="${age}"></p> <br>

使用ModelMap向request域对象共享数据

index.html

<a th:href="@{/testModelMap}">使用ModelMap</a> <br>

控制器

    @RequestMapping("/testModelMap")
public String testModelMap(ModelMap modelMap){
modelMap.addAttribute("major","software engineering");
return "success";
}

success.html

<p th:text="${major}"></p> <br>

Model、ModelMap、Map的关系

经过测试发现:除了ModelAndView的实现类是ModelAndViewModelMapModelMap 的实现类都是BindingAwareModelMap

ModelModelMapMap类型的参数其实本质上都是 BindingAwareModelMap 类型的

class of ModelAndView:  class org.springframework.web.servlet.ModelAndView
class of Model: class org.springframework.validation.support.BindingAwareModelMap
class of Map: class org.springframework.validation.support.BindingAwareModelMap
class of ModelMap: class org.springframework.validation.support.BindingAwareModelMap

阅读ModeAndView的源码可以发现,ModeAndViewModelMap是组合关系。下面是ModeAndView的部分源码。

public class ModelAndView {

    @Nullable
private ModelMap model; public ModelAndView() {
} public ModelMap getModelMap() {
if (this.model == null) {
this.model = new ModelMap();
}
return this.model;
}
public ModelAndView addObject(String attributeName, @Nullable Object attributeValue) {
this.getModelMap().addAttribute(attributeName, attributeValue);
return this;
}

ModeAndView调用addObject()方法时其实是调用ModelMapaddAttribute()方法,本质上与ModelMap是一样的。

各个类之间的关系如下:

public interface Model{}
public class ModelMap extends LinkedHashMap<String, Object> {}
public class ExtendedModelMap extends ModelMap implements Model {}
public class BindingAwareModelMap extends ExtendedModelMap {}

四种方式本质上都是调用的Model接口中的addAttribute方法

向session域共享数据

index.html

<a th:href="@{/testSession}">使用Session</a> <br>

控制器

    @RequestMapping("/testSession")
public String testSession(HttpSession session){
session.setAttribute("message","session scope");
return "success";
}

success.html

<p th:text="${session.message}"></p> <br>

向application域共享数据

index.html

<a th:href="@{/testApplication}">使用Application</a> <br>

控制器

@RequestMapping("/testApplication")
public String testApplication(HttpSession session){
ServletContext application = session.getServletContext();
application.setAttribute("testApplication","hello,application");
return "success"; }

success.html

<p th:text="${application.testApplication}"></p> <br>

最新文章

  1. PHP 文件限速下载代码
  2. UWP开发之Mvvmlight实践三:简单MVVM实例开发(图文详解付代码)
  3. 如何让同局域网的同事访问我电脑上的PHP网站和数据库
  4. hdu 1811Rank of Tetris (并查集 + 拓扑排序)
  5. JAVA Io 缓冲输入输出流
  6. 微软曝光眼球追踪新专利,未来或将可以使用眼球控制HoloLens
  7. spring boot实战(第十三篇)自动配置原理分析
  8. Bugtags 那些事儿
  9. JavaFx版本植物大战僵尸
  10. WPF中利用后台代码实现窗口分栏动态改变
  11. java源码解析——Stack类
  12. C++11 新特性总结
  13. oracle状态
  14. 认识Sass和Compass
  15. java基础 第六章课后习题
  16. iOS编程(双语版)-视图-Frame/Bounds/Center
  17. rotate-list 旋转部分链表
  18. 杂项:flex (adobe flex)
  19. 二、vue学习--父元素如何获取子元素的值,子元素如何获取父元素的值
  20. poj_1743 后缀数组

热门文章

  1. TypeScript——原始数据类型
  2. Echarts入门踩坑记录
  3. etcd学习(3)-grpc使用etcd做服务发现
  4. python 连接远程服务器,修改时间
  5. 爬取房价信息并制作成柱状图XPath,pyecharts
  6. 论文笔记:(2019)LDGCNN : Linked Dynamic Graph CNN-Learning on PointCloud via Linking Hierarchical Features
  7. 为什么 WordPress 镜像用起来顺手?
  8. Centos忘记密码怎么修改
  9. 手脱UPX壳的方法
  10. Windows根据端口号查找对应的进程和服务