Differences between ModelAndView, Model, and ModelMap in the SPRING framework

Reprinted content: http://www.cnblogs.com/google4y/p/3421017.html

 

1. Model

Model is an interface whose implementation class is ExtendedModelMap, which inherits the ModelMap class.

public class ExtendedModelMap extends ModelMap implements Model
  • 1

2.ModelMap

The declaration format of ModelMap:

public class ModelMap extends LinkedHashMap<String, Object>
  • 1

The ModelMap object is mainly used to transfer the control method to process the data to the result page, that is to say, we can put the data needed on the result page into the ModelMap object. Its function is similar to the function of the setAttribute method of the request object: it is used in a The processed data is passed during the request. ModelMap or Model passes parameters to the page through the addAttribute method, where the addAttribute method is overloaded in multiple ways:

public ModelMap addAttribute(String attributeName, Object attributeValue){...}
public ModelMap addAttribute(Object attributeValue){...} public ModelMap addAllAttributes(Collection<?> attributeValues) {...} public ModelMap addAllAttributes(Map<String, ?> attributes){...}

On the page, you can obtain and display the data in the modelmap through a series of data display tags such as the el expression language $attributeName. 
The modelmap itself cannot set the url address alias or physical jump address of page jump, then we can set the jump url address alias or physical jump address through the string return value of the controller method.

3.ModelAndView

The ModelAndView object has two functions: 
(1). Set the steering address, which is also the main difference between ModelAndView and ModelMap. The setting method is as follows:

ModelAndView view = new ModelAndView("path:ok");

Or through the setViewName method:

public void setViewName(String viewName){...}

(2). Transfer the result data processed in the controller method to the result page, that is, put the data required on the result page into the ModelAndView object. Its function is similar to the function of the setAttribute method of the request object, which is used to Pass processed data during a request. Pass parameters to the page by:

public ModelAndView addObject(String attributeName, Object attributeValue){...}
public ModelAndView addObject(Object attributeValue){...}

On the page, you can also obtain and display the data in ModelAndView through the el expression language $attributeName and other series of data display tags.

4. Use as follows:

(1) ModelMap 
The instance of ModelMap is automatically created by the spirng mvc framework and passed in as a controller method parameter, and the user does not need to create it by himself.

public String xxxxmethod(String someparam,ModelMap model)
{
     //省略方法处理逻辑若干
     //将数据放置到ModelMap对象model中,第二个参数可以是任何java类型 model.addAttribute("key",someparam); ...... //返回跳转地址 return "path:handleok"; }

(2) ModelAndView 
The instance of ModelAndView is created manually by the user, which is also a difference from ModelMap.

public ModelAndView xxxxmethod(String someparam)
{
     //省略方法处理逻辑若干
      //构建ModelAndView实例,并设置跳转地址 ModelAndView view = new ModelAndView("path:handleok"); //将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型 view.addObject("key",someparam); ...... //返回ModelAndView对象view return view; }

Refer to the above content here

 

Note: If the method declares the annotation @ResponseBody, the return value will be output directly to the page.

 

First introduce the role of ModelMap[Model] and ModelAndView

Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。 
ModelMap
ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数:
addAttribute(String key,Object value);
在页面上可以通过el变量方式$key或者bboss的一系列数据展示标签获取并展示modelmap中的数据。
modelmap本身不能设置页面跳转的url地址别名或者物理跳转地址,那么我们可以通过控制器方法的返回值来设置跳转url地址别名或者物理跳转地址。

ModelAndView
ModelAndView对象有两个作用:
作用一 设置转向地址,如下所示(这也是ModelAndView和ModelMap的主要区别)
ModelAndView view = new ModelAndView("path:ok");

作用二 用于传递控制方法处理结果数据到结果页面,也就是说我们把需要在结果页面上需要的数据放到ModelAndView对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数:
addObject(String key,Object value);

在页面上可以通过el变量方式$key或者bboss的一系列数据展示标签获取并展示ModelAndView中的数据。

作用介绍完了后,接下来介绍使用方法


ModelMap
ModelMap的实例是由bboss mvc框架自动创建并作为控制器方法参数传入,用户无需自己创建。

public String xxxxmethod(String someparam,ModelMap model)
{
     //省略方法处理逻辑若干
      //将数据放置到ModelMap对象model中,第二个参数可以是任何java类型
      model.addAttribute("key",someparam);
     ......
     //返回跳转地址
      return "path:handleok";
}



ModelAndView
ModelAndView的实例是由用户手动创建的,这也是和ModelMap的一个区别。

public ModelAndView xxxxmethod(String someparam)
{
     //省略方法处理逻辑若干
      //构建ModelAndView实例,并设置跳转地址
      ModelAndView view = new ModelAndView("path:handleok");
      //将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型
      view.addObject("key",someparam);
     ......
     //返回ModelAndView对象view
      return view;
}



到此bboss mvc中ModelMap和ModelAndView两个对象的作用和使用方法介绍完毕

如下为我自己写的测试代码

复制代码
@RequestMapping(value = "/demo",method = RequestMethod.GET)
    public ModelAndView getBusinessIdListByIp(@RequestParam("ip") String ip,@RequestParam("phoneId") String phoneId,Model model){
        ModelAndView mav = new ModelAndView();
        model.addAttribute("ip", ip);
        model.addAttribute("phoneId", phoneId);
        mav.addObject(model);
        mav.setViewName("user/mav");
        return mav;
    }
    
    @RequestMapping(value = "/demo2",method = RequestMethod.GET)
    public ModelAndView getBusinessIdListByIp(){
        return new Mo

注意:如果方法声明了注解@ResponseBody ,则会直接将返回值输出到页面。

 

首先介绍ModelMap[Model]和ModelAndView的作用

Model 是一个接口, 其实现类为ExtendedModelMap,继承了ModelMap类。 
ModelMap
ModelMap对象主要用于传递控制方法处理数据到结果页面,也就是说我们把结果页面上需要的数据放到ModelMap对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数:
addAttribute(String key,Object value);
在页面上可以通过el变量方式$key或者bboss的一系列数据展示标签获取并展示modelmap中的数据。
modelmap本身不能设置页面跳转的url地址别名或者物理跳转地址,那么我们可以通过控制器方法的返回值来设置跳转url地址别名或者物理跳转地址。

ModelAndView
ModelAndView对象有两个作用:
作用一 设置转向地址,如下所示(这也是ModelAndView和ModelMap的主要区别)
ModelAndView view = new ModelAndView("path:ok");

作用二 用于传递控制方法处理结果数据到结果页面,也就是说我们把需要在结果页面上需要的数据放到ModelAndView对象中即可,他的作用类似于request对象的setAttribute方法的作用,用来在一个请求过程中传递处理的数据。通过以下方法向页面传递参数:
addObject(String key,Object value);

在页面上可以通过el变量方式$key或者bboss的一系列数据展示标签获取并展示ModelAndView中的数据。

作用介绍完了后,接下来介绍使用方法


ModelMap
ModelMap的实例是由bboss mvc框架自动创建并作为控制器方法参数传入,用户无需自己创建。

public String xxxxmethod(String someparam,ModelMap model)
{
     //省略方法处理逻辑若干
      //将数据放置到ModelMap对象model中,第二个参数可以是任何java类型
      model.addAttribute("key",someparam);
     ......
     //返回跳转地址
      return "path:handleok";
}



ModelAndView
ModelAndView的实例是由用户手动创建的,这也是和ModelMap的一个区别。

public ModelAndView xxxxmethod(String someparam)
{
     //省略方法处理逻辑若干
      //构建ModelAndView实例,并设置跳转地址
      ModelAndView view = new ModelAndView("path:handleok");
      //将数据放置到ModelAndView对象view中,第二个参数可以是任何java类型
      view.addObject("key",someparam);
     ......
     //返回ModelAndView对象view
      return view;
}



到此bboss mvc中ModelMap和ModelAndView两个对象的作用和使用方法介绍完毕

如下为我自己写的测试代码

复制代码
@RequestMapping(value = "/demo",method = RequestMethod.GET)
    public ModelAndView getBusinessIdListByIp(@RequestParam("ip") String ip,@RequestParam("phoneId") String phoneId,Model model){
        ModelAndView mav = new ModelAndView();
        model.addAttribute("ip", ip);
        model.addAttribute("phoneId", phoneId);
        mav.addObject(model);
        mav.setViewName("user/mav");
        return mav;
    }
    
    @RequestMapping(value = "/demo2",method = RequestMethod.GET)
    public ModelAndView getBusinessIdListByIp(){
        return new Mo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325086509&siteId=291194637