【问题标题】:My Spring Boot app cannot see the Thymeleaf templates我的 Spring Boot 应用程序看不到 Thymeleaf 模板
【发布时间】:2015-09-14 11:19:44
【问题描述】:

我有一个 Spring Boot 应用程序,我在 pom.xml 中使用这些行定义我的 thymeleaf 依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

所以当我启动应用程序并打印预定义的 bean 名称时,我会看到这些 bean(我认为这对于 thymeleaf 正常运行很重要):

  • 模板引擎
  • thymeleafResourceResolver
  • thymeleafViewResolver

项目结构如下:

我在fragments.html中定义了一些片段:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">

  <body>

    <div class="container" th:fragment="copy">
        <hr>
        <footer>
            <div class="row">
                <div class="col-lg-12">
                    <p>Copyright &copy; Teamtool 2015</p>
                </div>
            </div>
        </footer>
    </div>

  </body>

</html>

.. 我正在尝试像这样使用我的 index.html 中的那些:

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello AngularJS</title>
<base href="/" />
<link href="css/angular-bootstrap.css" rel="stylesheet">
<style type="text/css">
[ng\:cloak], [ng-cloak], .ng-cloak {
    display: none !important;
}
</style>
</head>

<body ng-app="hello" ng-cloak class="ng-cloak">
    <div ng-controller="navigation" class="container">
        <ul class="nav nav-pills" role="tablist">
            <li ng-class="{active:tab('home')}"><a href="/">home</a></li>
            ...
        </ul>
    </div>
    <div ng-view class="container"></div>

    <div class="col-md-3" th:include="fragments :: copy"></div>

    <script src="js/angular-bootstrap.js" type="text/javascript"></script>
    ...
</body>
</html>

但是我的模板没有添加到 index.html 中。我做错了什么?

【问题讨论】:

    标签: spring spring-boot thymeleaf


    【解决方案1】:

    应用程序扫描 templates 文件夹中的 Thymeleaf 模板,将 index.html 移动到 templates 应该可以工作。

    【讨论】:

    • 我看到一个 Whitelabel 错误页面,我的 index.html 在模板文件夹中看不到。
    • 它应该,如果你使用的是 spring boot,你不必配置任何东西,至少如果你不想覆盖某些东西,所以检查你是否用 @Controller 注释控制器并有在你的控制器类@RequestMapping(value = "/") public String indexPage() { return "index"; }中类似这样的东西,然后检查index.htmlfragments.html如果你正确关闭了所有标签&lt;div&gt;&lt;hr&gt;甚至&lt;link&gt;应该关闭&lt;link&gt;&lt;/link&gt;&lt;link/&gt; , 最后一个 &lt;div ng-view &gt;&lt;/div&gt; 不允许在 thymeleaf 中使用,请使用 &lt;div ng-view=""&gt;&lt;/div&gt;
    【解决方案2】:

    我在这里尝试通用,Spring boot 将静态文件夹视为公共资源文件夹,用于提供 static 文件。正如您告诉 spring boot 还配置一个模板系统 - 在您的情况下为 thymeleaf - 它将开始在模板文件夹中查找。 Thymeleaf 模板不是静态的,而是一种动态页面构建机制。

    现在,因为您配置了 thymeleaf,Springboot 期望您要呈现动态 thymeleaf 模板,并从模板文件夹中寻找起点。它找不到 index.html 文件向您显示 Whitelabel 页面。当您将 index.html 移动到模板文件夹时,它有一个默认起点。您可以通过在其中一个控制器中映射像 @RequestMapping(value = "/") 这样的 URL 路径,将任何其他文件名映射为指向您的应用程序的声明点。

    您可以使用静态文件夹来提供 Angular JS 文件,但模板必须位于模板文件夹中,除非您覆盖它。

    【讨论】:

      猜你喜欢
      • 2023-03-22
      • 2016-01-26
      • 2021-03-03
      • 2017-06-21
      • 2020-11-16
      • 2021-12-14
      • 1970-01-01
      • 2019-04-11
      • 2020-03-14
      相关资源
      最近更新 更多