springmvc处理模型数据ModelAndView过程详解
作者:西西嘛呦 时间:2022-01-27 05:50:22
这篇文章主要介绍了springmvc处理模型数据ModelAndView过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
springmvc提供了以下几种途径来输出模型数据:
(1)ModelAndView:处理方法返回值类型为ModelAndView时,方法体即可通过该对象添加模型数据。
(2)Map及Model:入参为org.springframework.ui.Model、org.springframework.ui.ModelMap或java.uti.Map时,处理方法返回时,Map中的数据会自动添加到模型中。
(3)@SessionAttributes:将模型中的某个数据自动添加到模型中。
(4)ModelAttribute:方法入参标注该注解后,入参的对象就会放到数据模型中。
使用ModelAndView:
SpringmvcTest.java
@RequestMapping("/springmvc")
@Controller
public class SpringmvcTest {
private static final String SUCCESS = "success";
@RequestMapping(value="/testModelAndView")
public ModelAndView testModelAndView() {
String viewName = SUCCESS;
ModelAndView modelAndView = new ModelAndView(viewName);
//添加模型数据到MOdelAndView中
modelAndView.addObject("time", new Date());
return modelAndView;
}
}
Index.jsp
<a href="springmvc/testModelAndView" rel="external nofollow" >testModelAndView</a>
Success.jsp
<p>Success</p>
<p>time:${requestScope.time}</p>
启动服务器之后:
点击:
成功将time传给视图了。
来源:https://www.cnblogs.com/xiximayou/p/12178184.html
标签:spring,mvc,模型数据,Model,And,View
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
AQS(AbstractQueuedSynchronizer)抽象队列同步器及工作原理解析
2023-02-24 22:20:09
![](https://img.aspxhome.com/file/2023/6/84216_0s.png)
java使用dom4j操作xml示例代码
2022-03-21 18:28:38
dubbo服务链路跟踪方式
2023-08-24 09:54:21
![](https://img.aspxhome.com/file/2023/6/58296_0s.png)
Spring事务管理配置文件问题排查
2022-07-04 15:55:25
![](https://img.aspxhome.com/file/2023/4/76584_0s.png)
如何使用JJWT及JWT讲解和工具类
2023-10-28 04:21:35
微服务搭建集成Spring Cloud Turbine详解
2023-03-29 12:39:07
![](https://img.aspxhome.com/file/2023/5/64215_0s.jpg)
SpringBoot @Cacheable自定义KeyGenerator方式
2022-12-25 13:23:11
![](https://img.aspxhome.com/file/2023/9/88069_0s.png)
详解JAVA中获取文件MD5值的四种方法
2021-07-20 22:56:15
![](https://img.aspxhome.com/file/2023/1/78221_0s.png)
实例讲解C#中的职责链模式
2021-06-05 23:37:44
![](https://img.aspxhome.com/file/2023/7/79067_0s.png)
详解如何继承Mybatis中Mapper.xml文件
2022-12-22 14:48:34
Unity 点击UI与点击屏幕冲突的解决方案
2022-02-22 15:50:49
![](https://img.aspxhome.com/file/2023/7/92967_0s.jpg)
Java try-with-resource语法使用解析
2021-10-09 02:28:37
Spring JDK动态 代理实现过程详解
2023-11-16 19:42:15
![](https://img.aspxhome.com/file/2023/2/59392_0s.png)
SpringBoot常用数据库开发技术汇总介绍
2023-11-11 09:39:22
关于Java中的try-with-resources语句
2022-10-21 16:49:54
spring mvc中的@ModelAttribute注解示例介绍
2023-10-15 07:07:06
浅谈关于Mybatis的mapper-locations配置问题
2023-09-24 06:06:16
![](https://img.aspxhome.com/file/2023/5/67465_0s.jpg)
将本地的jar包打到Maven的仓库中实例
2022-08-29 13:38:28
Android RecyclerView添加上拉加载更多功能
2022-08-02 00:03:51
![](https://img.aspxhome.com/file/2023/8/94288_0s.gif)
Linux下java环境配置图文方法
2021-10-24 06:22:52
![](https://img.aspxhome.com/file/2023/0/70300_0s.png)