🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# 在 Spring Boot 中提供静态内容 > 原文: [http://zetcode.com/springboot/static/](http://zetcode.com/springboot/static/) Spring Boot 静态内容显示了如何在 Spring Boot 应用中提供静态内容。 Spring 是流行的 Java 应用框架。 Spring Boot 致力于以最小的努力创建独立的,基于生产级别的基于 Spring 的应用。 Spring Boot 自动添加位于以下任何目录中的静态 Web 资源: * `/META-INF/resources/` * `/resources/` * `/static/` * `/public/` 目录位于类路径或`ServletContext`的根目录中。 在我们的应用中,我们有一个 HTML 文件,其中包含一个简单的链接。 该链接触发来自 Web Boot 应用的响应。 它返回纯文本消息。 ```java pom.xml src ├───main │ ├───java │ │ └───com │ │ └───zetcode │ │ │ Application.java │ │ ├───controller │ │ │ MyController.java │ │ └───model │ │ Message.java │ └───resources │ │ application.properties │ └───static │ │ index.html │ └───css │ main.css └───test └───java └───com └───zetcode └───controller MyControllerTest.java ``` 这是 Spring Boot 应用的项目结构。 `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>springbootstaticex</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-test</artifactId> <scope>test</scope> </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 应用的入门。 `spring-boot-starter-test`导入必要的测试模块。 该应用打包到一个 JAR 文件中。 `com/zetcode/model/Message.java` ```java package com.zetcode.model; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @Component public class Message { @Value("${app.message}") private String message; public String get() { return message; } } ``` `Message`设置 Spring Boot 应用。 ```java @Value("${app.message}") private String message; ``` 我们将`application.properties`中的值注入`message`变量中。 `resources/application.properties` ```java app.message=Hello there ``` `application.properties`文件包含 Spring Boot 应用的各种配置设置。 我们定义一个具有文本消息的自定义属性。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import com.zetcode.model.Message; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class MyController { @Autowired private Message message; @GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); } } ``` 这是 Spring Boot Web 应用的控制器类。 控制器以`@Controller`注解修饰。 控制器具有一个映射; 它被映射到`/message`路径并返回纯文本消息。 ```java @Autowired private Message message; ``` `Message`对象被注入到属性中。 ```java @GetMapping(path = "/message") @ResponseBody public String message() { return message.get(); } ``` `message()`方法响应 GET 请求。 `@ResponseBody`注解将字符串值放入 Web 响应正文。 `resources/static/index.html` ```java <!DOCTYPE html> <html> <head> <title>Home page</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link href="css/main.css" rel="stylesheet" type="text/css"> </head> <body> <h2>Home page</h2> <a href="/message">Get message</a> </body> </html> ``` 在`index.html`文件中,我们必须调用从 Web 应用的响应的链接。 该文件位于`src/main/resources/static`目录中,该目录是 Spring 寻找静态内容的默认目录。 ```java <link href="css/main.css" rel="stylesheet" type="text/css"> ``` 在链接标记中,我们指的是`main.css`静态资源,该资源位于`src/main/resources/static/css`目录中。 `resources/static/css/main.css` ```java h2 { color: blue } ``` 在`main.css`文件中,我们将`h2`标签设置为蓝色。 `com/zetcode/controller/MyControllerTest.java` ```java package com.zetcode.controller; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.forwardedUrl; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; @RunWith(SpringRunner.class) @SpringBootTest public class MyControllerTest { @Autowired private WebApplicationContext wac; private MockMvc mockMvc; @Before public void setup() { this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build(); } @Test public void getHome() throws Exception { this.mockMvc.perform(get("/")) .andDo(print()) .andExpect(status().isOk()) .andExpect(forwardedUrl("index.html")); } @Test public void getMessage() throws Exception { this.mockMvc.perform(get("/message")) .andDo(print()) .andExpect(status().isOk()) .andExpect(content().string("Hello there")); } } ``` 在`MyControllerTest`中,我们有两个测试:一个用于主页,另一个用于返回的消息文本。 `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 应用。 `@SpringBootApplication`注解启用自动配置和组件扫描。 在本教程中,我们在 Spring Boot 应用中提供了静态上下文。 您可能也对相关教程感兴趣: [Spring Boot `DataSourceBuilder`教程](/springboot/datasourcebuilder/), [Spring Boot iText 教程](/articles/springbootitext/), [Spring Boot RESTFul 应用](/articles/springbootrestsimple/), [Spring Web 应用简介](/articles/springwebfirst/),[独立的 Spring 应用](/articles/standalonespring/), [Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#springboot)。