SpringMVC 域对象共享数据的实现示例
作者:gonghr 时间:2021-07-18 15:45:57
使用ModelAndView向request域对象共享数据
index.html
<a th:href="@{/testModelAndView}" rel="external nofollow" >使用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}" rel="external nofollow" >使用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}" rel="external nofollow" >使用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}" rel="external nofollow" >使用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
的实现类是ModelAndView
,Model
、Map
和ModelMap
的实现类都是BindingAwareModelMap
。
Model
、ModelMap
、Map
类型的参数其实本质上都是 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
的源码可以发现,ModeAndView
和ModelMap
是组合关系。下面是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()
方法时其实是调用ModelMap
的addAttribute()
方法,本质上与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}" rel="external nofollow" >使用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}" rel="external nofollow" >使用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>
来源:https://www.cnblogs.com/gonghr/p/15202176.html
标签:SpringMVC,域对象,共享数据
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SpringBoot深入分析运行原理与功能实现
2022-01-03 14:48:43
![](https://img.aspxhome.com/file/2023/0/71220_0s.png)
Unity的IPostBuildPlayerScriptDLLs实用案例深入解析
2022-12-23 00:12:38
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
2023-10-18 10:36:43
![](https://img.aspxhome.com/file/2023/9/64029_0s.jpg)
hadoop是什么语言
2021-06-18 07:02:38
![](https://img.aspxhome.com/file/2023/0/70970_0s.png)
java中Locks的使用详解
2023-11-29 00:18:38
Spring Bean常用依赖注入方式详解
2022-06-05 21:43:33
SpringBoot与Angular2的集成示例
2021-09-02 22:24:55
浅谈利用Spring的AbstractRoutingDataSource解决多数据源的问题
2021-09-07 07:20:59
SQLServer批量插入数据的三种方式及性能对比
2022-07-01 12:05:07
![](https://img.aspxhome.com/file/2023/7/71077_0s.jpg)
java中超过long范围的超大整数相加算法详解(面试高频)
2022-09-15 11:22:05
![](https://img.aspxhome.com/file/2023/0/61850_0s.png)
Spring框架通过工厂创建Bean的三种方式实现
2022-11-23 11:29:54
![](https://img.aspxhome.com/file/2023/7/70527_0s.png)
Java的SPI机制实例详解
2021-08-15 08:47:41
java 线程方法join简单用法实例总结
2022-10-26 19:27:38
深入探究Java线程的创建与构造方法
2023-05-29 22:02:45
![](https://img.aspxhome.com/file/2023/5/72315_0s.png)
java 命名空间 命名规则第1/2页
2021-06-06 14:20:19
JAVA操作MongoDB数据库实例教程
2023-11-18 13:22:27
Default Methods实例解析
2023-05-18 19:28:01
实例详解Java调用第三方接口方法
2023-10-15 06:34:32
![](https://img.aspxhome.com/file/2023/9/67469_0s.png)
Java SpringMVC异步处理详解
2021-08-10 15:03:58
![](https://img.aspxhome.com/file/2023/0/62050_0s.png)
java中Class.getMethods()和Class.getDeclaredMethods()方法的区别
2021-05-28 23:05:46