Model, ModelMap, and ModelAndView in Spring MVC – Spring MVC中的模型、ModelMap和ModelAndView

最后修改: 2017年 10月 17日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this article, we’ll look at the use of the core org.springframework.ui.Model, org.springframework.ui.ModelMap and org.springframework.web.servlet.ModelAndView provided by Spring MVC.

在这篇文章中,我们将看看Spring MVC提供的核心org.springframework.ui.Modelorg.springframework.ui.ModelMap和org.springframework.web.Servlet.ModelAndView的使用。

2. Maven Dependencies

2.Maven的依赖性

Let’s start with the spring-context dependency in our pom.xml file:

让我们从spring-context文件中的pom.xml依赖项开始。

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.2.2.RELEASE</version>
</dependency>

The latest version of spring-context dependency can be found here.

最新版本的spring-context依赖性可以在这里找到。

For the ModelAndView, the spring-web dependency is required:

对于ModelAndView,需要依赖spring-web

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>5.2.2.RELEASE</version>
</dependency>

The latest versions of spring-web dependency can be found here.

Spring-web依赖的最新版本可以在这里找到。

And, if we use Thymeleaf as our view, we should add this dependency to pom.xml:

而且,如果我们使用Thymeleaf作为我们的视图,我们应该在pom.xml中添加这个依赖关系。

<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring5</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

The latest version of Thymeleaf dependency can be found here.

Thymeleaf依赖性的最新版本可以在这里找到。

3. Model

3.模型

Let’s start with the most basic concept here – the Model.

让我们从这里最基本的概念开始–Model

Simply put, the model can supply attributes used for rendering views.

简单地说,模型可以提供用于渲染视图的属性。

To provide a view with usable data, we simply add this data to its Model object. Additionally, maps with attributes can be merged with Model instances:

为了给视图提供可用的数据,我们只需将这些数据添加到其Model对象中。此外,带有属性的地图可以与Model实例合并。

@GetMapping("/showViewPage")
public String passParametersWithModel(Model model) {
    Map<String, String> map = new HashMap<>();
    map.put("spring", "mvc");
    model.addAttribute("message", "Baeldung");
    model.mergeAttributes(map);
    return "viewPage";
}

4. ModelMap

4.ModelMap

Just like the Model interface above, ModelMap is also used to pass values to render a view.

就像上面的 Model接口一样, ModelMap也被用来传递值来渲染一个视图。

The advantage of ModelMap is it gives us the ability to pass a collection of values and treat these values as if they were within a Map:

ModelMap的优势在于它让我们能够传递一个值的集合,并将这些值当作是在Map中。

@GetMapping("/printViewPage")
public String passParametersWithModelMap(ModelMap map) {
    map.addAttribute("welcomeMessage", "welcome");
    map.addAttribute("message", "Baeldung");
    return "viewPage";
}

5. ModelAndView

5.ModelAndView

The final interface to pass values to a view is the ModelAndView.

向视图传递值的最后一个接口是 ModelAndView

This interface allows us to pass all the information required by Spring MVC in one return:

这个接口允许我们在一个返回中传递Spring MVC要求的所有信息。

@GetMapping("/goToViewPage")
public ModelAndView passParametersWithModelAndView() {
    ModelAndView modelAndView = new ModelAndView("viewPage");
    modelAndView.addObject("message", "Baeldung");
    return modelAndView;
}

6. The View

6.风景

All the data, we place within these models, is used by a view – in general, a templated view to render the web page.

所有的数据,我们放在这些模型中,被一个视图使用–一般来说,一个模板化的视图来渲染网页。

If we have a Thymeleaf template file targeted by our controller’s methods as their view. A parameter passed through the model will be accessible from within the thymeleaf HTML code:

如果我们有一个Thymeleaf模板文件,由我们的控制器的方法作为其视图的目标。通过模型传递的参数将可以从thymeleaf的HTML代码中访问。

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Title</title>
</head>
<body>
    <div>Web Application. Passed parameter : th:text="${message}"</div>
</body>
</html>

The parameter passed here is used through the syntax ${message}, which is known as a placeholder. The Thymeleaf template engine will replace this placeholder with an actual value from an attribute of the same name passed through the model.

这里传递的参数是通过语法${message}使用的,这被称为占位符。Thymeleaf模板引擎将用通过模型传递的同名属性的实际值来替换这个占位符。

7. Conclusion

7.结论

In this quick tutorial, we’ve discussed three core concepts in Spring MVC – the Model, the ModelMap and the ModelAndView. We also had a look at examples of how the view can make use of these values.

在这个快速教程中,我们讨论了Spring MVC的三个核心概念–ModelModelMapModelAndView。我们还看了一下视图如何利用这些值的例子。

As always, the implementation of all these examples and code snippets can be found over on Github.

一如既往,所有这些例子和代码片段的实现都可以在Github上找到over