java - Thymeleaf 在 Spring Boot 项目中不显示值

标签 java spring spring-boot thymeleaf

我是 Thymeleaf 的新手。我有一个 spring-boot 项目,我试图在其中显示一些已从 spring Controller 设置的属性值。

@Controller
public class HomeController {
    @RequestMapping(path = "/info", method = RequestMethod.GET)
    public String info(Model model) {
    model.addAttribute("message", "Thymeleaf");
    return "info";
    }
}

请在下面找到我的 info.html 页面:

<!DOCTYPE html>
<html xmlns:thm="http://www.thymeleaf.org">
   <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <title>Info</title>
   </head>
   <body>
     <h1 thm:text="${message}"></h1>
   </body>
</html>

请在下面找到我的 pom.xml 文件 [仅依赖项]

   <dependencies>
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
      <dependency>
          <groupId>org.apache.tomcat.embed</groupId>
          <artifactId>tomcat-embed-jasper</artifactId>
      </dependency>
       <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
  </dependencies>

我知道 stackoverflow 上很少有关于这个主题的帖子,我已经浏览过其中的一些帖子,如下所示:

example 1 example 2

我根据 stackoverflow 博客的建议相应地修改了我的 html 页面。但问题仍然存在。

我的网页根本没有重新渲染 Thymeleaf 文本。 谁能弄清楚确切的问题吗?

最佳答案

thm不是 Thymeleaf 中的有效前缀,请使用 <h1 th:text="${message}"></h1>相反。

关于java - Thymeleaf 在 Spring Boot 项目中不显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49210589/

相关文章:

java - 在 Docker Swarm 中运行集群时暴露 Nifi UI

eclipse - 无法启动 Eclipse -- 无法打开\Java\jre6\lib\i386\jvm.cfg

java - Spring Boot Starter Web 依赖项给出缺少 Artifact 的错误

java - 如何使用 Spring Boot 将 git commit id 输出到所有 logback 日志?

java - 使用随机枢轴元素而不是中间元素代码进行快速排序

java - 在 struts2 中,是否可以将可选的 patternMapping 与变量一起使用?

spring - 在Docker中运行嵌入式mongo时org.springframework.beans.factory.UnsatisfiedDependencyException

java - 使用@MappedSuperclass 而不是@Entity 可以不使用JPA 在数据库中创建表吗?

java - minikube 中的损坏 jar

spring-boot - EnableAutoConfiguration 生成不必要的存储库 CGLIB 代理