gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:19:56 25 4
gpt4 key购买 nike

我有一个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com