spring Model、ModelMap 和 ModelAndView 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18486660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 06:11:04  来源:igfitidea点击:

What are the differences between Model, ModelMap, and ModelAndView?

springspring-mvc

提问by Basavaraj

What are the main differences between the following Spring Frameworkclasses?

以下Spring Framework类之间的主要区别是什么?

  • Model
  • ModelMap
  • ModelAndView
  • Model
  • ModelMap
  • ModelAndView

Using Model.put(String,Object)we can access the values in .jspfiles, but ModelMap.addAttribute(String,Object)also did same thing. I do not understand the difference between these classes.

使用Model.put(String,Object)我们可以访问.jsp文件中的值,但ModelMap.addAttribute(String,Object)也可以做同样的事情。我不明白这些课程之间的区别。

回答by Bart

Modelis an interface while ModelMapis a class.

Model是一个接口ModelMap而是一个类。

ModelAndViewis just a container for both a ModelMapand a view object. It allows a controller to return both as a single value.

ModelAndView只是一个容器ModelMap和一个视图对象。它允许控制器将两者作为单个值返回。

回答by Vikas Harle

Differences between Model, ModelMap, and ModelAndView

Model、ModelMap 和 ModelAndView 的区别

Model:It is an Interface. It defines a holder for model attributes and primarily designed for adding attributes to the model.

模型:它是一个接口。它定义了模型属性的持有者,主要用于向模型添加属性。

Example:

例子:

@RequestMapping(method = RequestMethod.GET)
    public String printHello(Model model) {
          model.addAttribute("message", "Hello World!!");
          return "hello";
       }

ModelMap:Implementation of Map for use when building model data for use with UI tools.Supports chained calls and generation of model attribute names.

ModelMap:Map 的实现,用于构建与 UI 工具一起使用的模型数据。支持链式调用和模型属性名称的生成。

Example:

例子:

@RequestMapping("/helloworld")
public String hello(ModelMap map) {
    String helloWorldMessage = "Hello world!";
    String welcomeMessage = "Welcome!";
    map.addAttribute("helloMessage", helloWorldMessage);
    map.addAttribute("welcomeMessage", welcomeMessage);
    return "hello";
}

ModelAndView:This class merely holds both to make it possible for a controller to return both model and view in a single return value.

ModelAndView:这个类只是为了让控制器可以在一个返回值中同时返回模型和视图。

Example:

例子:

@RequestMapping("/welcome")
public ModelAndView helloWorld() {
        String message = "Hello World!";
        return new ModelAndView("welcome", "message", message);
    }

回答by Ashu

Model: is an interface it contains four addAttribute and one merAttribute method.

Model:是一个接口,它包含四个 addAttribute 和一个 merAttribute 方法。

ModelMap: implements Map interface. It also contains Map method.

ModelMap: 实现 Map 接口。它还包含 Map 方法。

ModelAndView: As Bart explain it allows a controller return both as a single value.

ModelAndView:正如 Bart 解释的那样,它允许控制器将两者作为单个值返回。