💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# Spring Boot 第一个 Web 应用 > 原文: [http://zetcode.com/articles/springbootwebfirst/](http://zetcode.com/articles/springbootwebfirst/) Spring Boot 的第一个 Web 应用教程展示了如何创建一个简单的 Spring Boot Web 应用。 当前的趋势是从可执行的 JAR 启动 Spring Boot 应用。 (有关传统 WAR 部署的示例,请参见[`SpringBootServletInitializer`教程](/springboot/springbootservletinitializer/)。) Spring 是流行的 Java 应用框架。 Spring Boot 致力于以最小的努力创建独立的,基于生产级别的基于 Spring 的应用。 ## Spring Boot Web 应用示例 该应用显示一条消息和今天的日期。 该消息是从应用的属性中检索的。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ └───controller │ │ MyController.java │ └───resources │ │ application.properties │ └───templates │ index.html └───test └───java ``` 这是项目结构。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springbootfirstweb</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> ``` 这是 Maven 构建文件。 `spring-boot-starter-web`是使用 Spring MVC 构建 Web(包括 RESTful)应用的入门程序。 `spring-boot-starter-thymeleaf`包含 Thymeleaf 模板引擎。 当 Spring Boot 检测到该启动程序时,它将自动为我们配置 Thymeleaf。 该应用打包到一个 JAR 文件中,该文件包含一个嵌入式 Tomcat Web 服务器。 `resources/application.properties` ```java application.message: Hello there ``` `application.properties`文件包含 Spring Boot 应用的各种配置设置。 我们有一个自定义消息选项。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; 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(Map<String, Object> model) { model.put("now", LocalDate.now()); model.put("message", this.message); return "index"; } } ``` 这是 Spring Boot Web 应用的控制器类。 控制器被饰以`@Controller`注解。 控制器具有一个映射。 映射解析为`index.jsp`,它位于`WEB-INF/jsp`目录中。 ```java @Value("${application.message}") private String message = "Hi there"; ``` 我们将`application.properties`中的值注入`message`变量中。 ```java @GetMapping("/") public String index(Map<String, Object> model) { model.put("now", LocalDate.now()); model.put("message", this.message); return "index"; } ``` `@GetMapping`注解将带有/路径的 GET 请求映射到索引方法处理器。 创建一个模型并填充数据。 该模型是`Map`接口,可以完全抽象视图技术。 `com/zetcode/Application.java` ```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 应用。 `resources/templates/index.html` ```java <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <p th:text="'Date: ' + ${now}"></p> <p th:text="'Message: ' + ${message}"></p> </body> </html> ``` `index.html`显示两个值:当前日期和收到的消息。 这两个值都通过控制器传递到模板。 ```java $ mvn spring-boot:run ``` 我们运行该应用。 现在我们可以导航到`localhost:8080`以查看应用消息。 在本教程中,我们创建了第一个 Spring Boot Web 应用。 您可能也对相关教程感兴趣: [Spring Web 应用简介](/articles/springwebfirst/),[独立的 Spring 应用](/articles/standalonespring/), [FreeMarker 教程](/java/freemarker/), [Java 教程](/lang/java/), 或列出[所有 Spring Boot 教程](/all/#springboot)。