在 Spring 引导应用程序中提供 HTML 页

Serving HTML pages in a Spring Boot application

我有一个 Spring 启动应用程序。我正在尝试将一个变量传递给 HTML 页面,但不幸的是我似乎无法做到这一点,在应用程序启动时,除了静态文本:“WORLD”外,没有呈现任何内容。我的控制器如下:

@Controller
public class IndexController {
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index(Model model) {
        model.addAttribute("message", "HELLO");
        return "index";
    }
}

还有我的index.html:

<html>
<body>
<h1>${message}</h1>
<h2>WORLD</h2>
</body>
</html>

我正在寻找答案并找到了这些文章:

How to serve .html files with Spring

How to serve html files with Spring

How to handle static content in Spring MVC?

这些是有用的例子,但在我的项目中我没有文件,例如:mvc-dispatcher-servlet.xml、web.xml,甚至整个 WEB-INF 目录。

创建 Spring MVC 项目后的文件在下面的屏幕截图中。我应该添加上述文献中提到的文件来解决这个问题还是什么?

通过 IntelliJ IDEA 查看我的项目:

我的 pom.xml 文件:

<?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>offersmanager</groupId>
    <artifactId>webapp</artifactId>
    <version>1</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>

    <properties>
        <!-- Java version -->
        <java.version>1.8</java.version>
        <!-- Use UTF-8 sources encoding -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <!-- Local model -->
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>model</artifactId>
            <version>${project.version}</version>
        </dependency>

        <!-- Spring boot -->
        <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>

        <!-- MAIN CODE SETTINGS -->
        <sourceDirectory>src/main/java</sourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>

        <!-- TEST CODE SETTINGS -->
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>

    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>

</project>

您正在使用 Thymeleaf 作为您的模板语言,您应该 read the documentation 它。在 Thymeleaf 中,要替换一个变量,您将在 HTML 标签上使用一个属性,就像这样:

<html>
<body>
<h1 data-th-text="${message}">this gets replaced</h1>
<h2>WORLD</h2>
</body>
</html>