【问题标题】:Thymeleaf not displaying values in Spring Boot projectThymeleaf 未在 Spring Boot 项目中显示值
【发布时间】:2018-03-10 15:10:10
【问题描述】:

我是 Thymeleaf 的新手。我有一个 spring-boot 项目,我试图显示一些从 spring 控制器设置的属性值。

@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 文本。 谁能弄清楚确切的问题?

【问题讨论】:

    标签: java spring spring-boot thymeleaf


    【解决方案1】:

    thm 不是 Thymleaf 中的有效前缀,请改用 &lt;h1 th:text="${message}"&gt;&lt;/h1&gt;

    【讨论】:

    • 谢谢它的工作。但是你能解释一下为什么我们不能使用 thm 作为前缀,对于其他框架,如 struts,spring form 标签,前缀名称没有这样的绑定。
    • 抱歉,我无法回答这个问题,因为Thymeleaf 是我用过的唯一模板引擎...您可以参考这个以查看有关 Thymeleaf 语法​​的更多信息...thymeleaf.org/doc/tutorials/2.1/…跨度>
    【解决方案2】:

    您可以先从您发送的模型对象中检索它,然后您可以使用th:eachth:value 来使用它。

     <div th:each="message : ${message}">
       <h1 th:text="${message}"></h1>
     </div>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 2021-11-17
      • 2016-11-30
      • 2021-06-03
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      相关资源
      最近更新 更多