java - 在 Spring Boot 应用程序中提供 HTML 页面

标签 java html spring-boot thymeleaf

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

@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 查看我的项目:

Project Structure

我的 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>

关于java - 在 Spring Boot 应用程序中提供 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33640777/

相关文章:

spring-boot - 如何知道 spring boot 应用程序中正在运行的计划作业?

java - 如何在 Spring Boot 的 Spring Security 级别启用 CORS

html - Shopify Email 模板添加条码 (UPC)

html - 将标题文本保持在与背景容器相关的相同位置

javascript - 使用 jquery 在 asp.net 中使用选项卡制作 Accordion

java - 在 hibernate 中使用多态类建模一对一映射

Spring boot 国际化 eng 没有效果

java - 捕获字符串中的属性值?

java - Tomcat启动时的触发函数

java - Tomcat JDBC 连接池是否在实例之间共享?