Spring Boot 模型
最后修改于 2023 年 7 月 24 日
在本文中,我们将展示如何在 Spring Boot 应用程序中使用模型。模型在 Spring 中由 Model、ModelMap 和 ModelAndView 表示。
Spring 是一个流行的 Java 应用程序框架,而 Spring Boot 是 Spring 的一个演进,它有助于轻松创建独立的、生产级的基于 Spring 的应用程序。
MVC
MVC (模型-视图-控制器) 是一种软件架构模式,它将应用程序分为三个部分:模型、视图和控制器。模型表示一个包含数据的 Java 对象。视图将模型包含的数据可视化。控制器管理数据流到模型对象中,并在数据更改时更新视图;它使视图和模型保持分离。
Spring MVC
Spring MVC 是基于 Servlet API 构建的原始 Web 框架。它是基于 MVC 设计模式构建的。Spring Framework 5.0 引入了一个并行的反应式堆栈 Web 框架,称为 Spring WebFlux。
Model、ModelMap、ModelAndView
Model、ModelMap 和 ModelAndView 用于在 Spring MVC 应用程序中定义模型。Model 定义了模型属性的持有者,主要用于将属性添加到模型中。ModelMap 是 Model 的扩展,它具有将属性存储在 Map 中并链接方法调用的能力。ModelAndView 是模型和视图的持有者;它允许在一个返回值中同时返回模型和视图。
Spring Boot 模型示例
以下简单的 Web 应用程序在控制器方法中使用 Model、ModelMap 和 ModelAndView。该模型保存应用程序数据,这些数据将显示在视图中。我们使用 Freemarker 库作为视图层。
build.gradle
...
src
├── main
│ ├── java
│ │ └── com
│ │ └── zetcode
│ │ ├── Application.java
│ │ └── controller
│ │ └── MyController.java
│ └── resources
│ ├── application.properties
│ ├── static
│ │ └── index.html
│ └── templates
│ └── show.ftlh
└── test
├── java
└── resources
这是 Spring 应用程序的项目结构。
plugins {
id 'org.springframework.boot' version '3.1.1'
id 'io.spring.dependency-management' version '1.1.0'
id 'java'
}
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-freemarker'
}
这是 Gradle 构建文件。
spring.main.banner-mode=off spring.main.log-startup-info=false mymessage=Hello there
application.properties 是 Spring Boot 中的主要配置文件。我们关闭了 Spring 横幅和 Spring 框架的启动日志。mymessage 属性包含消息。
package com.zetcode.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale;
@Controller
public class MyController {
@Value("${mymessage}")
private String message;
@GetMapping("/getMessage")
public String getMessage(Model model) {
model.addAttribute("message", message);
return "show";
}
@GetMapping("/getMessage2")
public ModelAndView getMessage() {
var mav = new ModelAndView();
mav.addObject("message", message);
mav.setViewName("show");
return mav;
}
@GetMapping("/getMessageAndTime")
public String getMessageAndTime(ModelMap map) {
var ldt = LocalDateTime.now();
var fmt = DateTimeFormatter.ofLocalizedDateTime(
FormatStyle.MEDIUM);
fmt.withLocale(new Locale("sk", "SK"));
fmt.withZone(ZoneId.of("CET"));
String time = fmt.format(ldt);
map.addAttribute("message", message).addAttribute("time", time);
return "show";
}
}
这是 MyController。它有三个方法响应客户端请求。
@Controller
public class MyController {
MyController 使用 @Controller 注解进行注解。
@Value("${mymessage}")
private String message;
使用 @Value 注解,我们将 application.properties 文件中的 mymessage 属性插入到 message 属性中。
@GetMapping("/getMessage")
public String getMessage(Model model) {
model.addAttribute("message", message);
return "show";
}
@GetMapping 将 /getMessage URL 模式映射到 getMessage 方法。在 getMessage 方法中,我们使用 Model。它使用 addAttribute 方法接收一个 message 属性。return 关键字返回视图的名称,由于我们使用 Freemarker 模板系统,它将被解析为 show.ftlh。
@GetMapping("/getMessage2")
public ModelAndView getMessage() {
var mav = new ModelAndView();
mav.addObject("message", message);
mav.setViewName("show");
return mav;
}
在第二种情况下,我们使用 ModelAndView。我们使用 addObject 和 setViewName 来添加模型数据和视图名称。该方法返回 ModelAndView 对象。
@GetMapping("/getMessageAndTime")
public String getMessageAndTime(ModelMap map) {
var ldt = LocalDateTime.now();
var fmt = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
fmt.withLocale(new Locale("sk", "SK"));
fmt.withZone(ZoneId.of("CET"));
var time = fmt.format(ldt);
map.addAttribute("message", message).addAttribute("time", time);
return "show";
}
在 getMessageAndTime 方法中,我们使用 ModelMap。模型 Map 接收两个属性:message 和 time。
<!DOCTYPE html>
<html>
<head>
<title>Home page</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<ul>
<li><a href="getMessage">Get message</a></li>
<li><a href="getMessage2">Get message 2</a></li>
<li><a href="getMessageAndTime">Get message and time</a></li>
</ul>
</body>
</html>
这是主页。它包含三个链接,这些链接调用 Spring 控制器方法。它是一个静态资源,位于预定义的 src/main/resources/static 目录中。
<!DOCTYPE html>
<html>
<head>
<title>Message</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<p>
Message: ${message}
</p>
<#if time??>
<p>Date and time: ${time}</p>
</#if>
</body>
</html>
show.ftlh 是一个 Freemarker 模板文件。它位于预定义的 src/main/resources/templates 目录中。它使用 ${} 语法输出消息,并可以选择性地输出时间。
package com.zetcode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Application 是设置 Spring Boot 应用程序的入口点。
$ ./gradlew bootRun
启动应用程序后,我们导航到 localhost:8080。
在本文中,我们使用 Spring 应用程序中的模型进行了工作。