ZetCode

Spring Boot 第一个 Web 应用程序

最后修改于 2023 年 7 月 29 日

Spring Boot 第一个 Web 应用程序教程展示了如何创建一个简单的 Spring Boot Web 应用程序。目前的趋势是从可执行的 JAR 文件启动 Spring Boot 应用程序。(有关传统 WAR 部署的示例,请参阅 SpringBootServletInitializer 教程。)

Spring 是一个流行的 Java 应用程序框架。Spring Boot 旨在以最小的努力创建独立的、生产级的基于 Spring 的应用程序。

Spring Boot Web 应用程序示例

该应用程序显示一条消息和今天的日期。消息是从应用程序的属性中检索的。

build.gradle
...
src
├───main
│   ├───java
│   │   └───com
│   │       └───zetcode
│   │           │   Application.java
│   │           └───controller
│   │                   MyController.java
│   └───resources
│       │   application.properties
│       └───templates
│               index.peb
└───test
    └───java

这是项目结构。

build.gradle
plugins {
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'com.zetcode'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'io.pebbletemplates:pebble-spring-boot-starter:3.2.1'
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

这是 Gradle 构建文件。spring-boot-starter-web 是使用 Spring MVC 构建 Web(包括 RESTful)应用程序的启动器。

pebble-spring-boot-starter 包含 Pebble 模板引擎。当 Spring Boot 检测到此启动器时,它会自动为我们配置 Pebble。

该应用程序被打包成一个 JAR 文件,其中包含一个嵌入式的 Tomcat Web 服务器。

resources/application.properties
application.message=Hello there

application.properties 文件包含 Spring Boot 应用程序的各种配置设置。我们有一个自定义的消息选项。

com/zetcode/controller/MyController.java
package com.zetcode.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.time.LocalDate;
import java.util.Map;

@Controller
public class MyController {

    @Value("${application.message}")
    private String message = "Hi there";

    @GetMapping("/")
    public String index(Model model) {

        model.addAttribute("now", LocalDate.now());
        model.addAttribute("message", this.message);

        return "index";
    }
}

这是 Spring Boot Web 应用程序的控制器类。控制器使用 @Controller 注解进行装饰。控制器有一个映射。该映射解析为 index.peb,它位于 resources/templates 目录中。

@Value("${application.message}")
private String message = "Hi there";

我们将一个值从 application.properties 注入到 message 变量中。

@GetMapping("/")
public String index(Model model) {

    model.addAttribute("now", LocalDate.now());
    model.addAttribute("message", this.message);

    return "index";
}

@GetMapping 注解将带有 / 路径的 GET 请求映射到 index 方法处理程序。创建一个模型并填充数据。Spring Boot 将 index 视图解析为 index.peb 模板文件,它也将模型数据发送到该文件。

resources/templates/index.peb
<!DOCTYPE html>
<html lang="en">
<head>
    <title>Home page</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>

<p>
Today: {{ now }}
</p>

<p>
Message: {{ message }}
</p>

</body>
</html>

index.peb 显示两个值:当前日期和收到的消息。这两个值都通过控制器传递给模板。

<p>
Today: {{ now }}
</p>

Pebble 使用 {{}} 语法来显示变量。

com/zetcode/Application.java
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 Boot Web 应用程序。

作者

我的名字是 Jan Bodnar,我是一位充满激情的程序员,拥有丰富的编程经验。自 2007 年以来,我一直在撰写编程文章。迄今为止,我撰写了 1,400 多篇文章和 8 本电子书。我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程