springmvn的modelAndview具体功能解释

xiaoxiao2021-02-27  191

我是一个新手,一直不懂springMVC里面的modelandview到底是是么意思,今天看了一下视频,突然感觉有一点懂了,其实,modelandview顾名思义,就是模型数据和视图一起传递到前台,比如如下列子!

1.首先要创建一个带有modelandview对象的方法

@Controller public class UserController { @RequestMapping("/index") public ModelAndView index(String name){ } 这个方法可以看出返回值是modelandview,是一个带字符串参数的方法等会儿要将此name变量带着视图传入前台,

2.创建modelandview对象,

@Controller public class UserController { @RequestMapping("/index") public ModelAndView index(String name){ ModelAndView modelAndView=new ModelAndView(); modelAndView.addObject("name",name); modelAndView.setViewName("index"); return modelAndView; } (1)把name前台获取的值添加到modelandview,name变量也传入modelandview,使"name”和name相互映射,其中“name”是前台传过来的值,name是controller设置的变量,

(2)modelandview.setviewname是设置视图解析器名,通过modelandview将数据和视图传入视图解析器

(3)而后返回modelandview,返回得到的结果,

转载请注明原文地址: https://www.6miu.com/read-11553.html

最新回复(0)