带有HTML文件的Spring Boot

问题描述 投票:4回答:1

我有一个Spring Boot应用程序。我试图将变量传递给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

enter link description here

How to handle static content in Spring MVC?

有一些有用的例子,但在我的项目中我没有像mvc-dispatcher-servlet.xml,web.xml甚至整个WEB-INF目录这样的文件......

我创建Spring MVC Project时的这些文件。然后一切正常 - 将变量传递给JSP文件。我应该添加这些文件还是什么?

这是我的项目文件层次结构:

Project Structure

我们使用.hml圆角:

<?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>
java html spring view resolver
1个回答
2
投票

你使用Thymeleaf作为你的模板语言,你应该为它做read the documentation。在Thymeleaf中,要替换变量,您将在HTML标记上使用属性,如下所示:

<html>
<body>
<h1 data-th-text="${message}">this gets replaced</h1>
<h2>WORLD</h2>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.