【问题标题】:How to locate Thymeleaf template from spring boot如何从 Spring Boot 中找到 Thymeleaf 模板
【发布时间】:2017-05-10 03:17:13
【问题描述】:

我正在尝试按照http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html 的本教程学习如何将响应发送到 Thymeleaf 模板。但我收到此错误:找不到模板位置:类路径:/模板/(请添加一些模板或检查您的 Thymeleaf 配置)

我将 message.html 文件放在 Other Sources 目录和 <default package> 下的 src/main/resources 中。

所以结构看起来像:

一些项目 -其他来源 --src/main/资源 ---<default package> ----message.html

我想知道为什么它显示在 <default package> 下而不是 <template> 下?会不会是问题?如果是这样我应该如何改变它?我正在使用 netbeans 和 Maven。请问有什么想法吗?这些是我在 pom.xml 中的依赖项:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
     <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>            
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>

在控制器中我有

@RequestMapping(value = "message", method = RequestMethod.GET)
    public String messages(Model model) {
    model.addAttribute("messages", messageRepository.findAll());
    return "message";
}

在视图中:

<ul th:each="message : ${messages}">
    <li th:text="${message.id}">1</li>
    <li><a href="#" th:text="${message.title}">Title ...</a></li>
    <li th:text="${message.text}">Text ...</li>
</ul>   

【问题讨论】:

    标签: java spring maven spring-boot thymeleaf


    【解决方案1】:

    Spring Boot 包括对 thymeleaf 模板引擎的自动配置支持,您的模板将自动从 src/main/resources/templates 中获取。

    如果您要自定义模板位置,请使用以下 Spring Boot 中可用的 thymeleaf 属性配置。

     spring.thymeleaf.check-template=true # Check that the template exists before rendering it.
    
     spring.thymeleaf.check-template-location=true # Check that the templates location exists.
    
     spring.thymeleaf.enabled=true # Enable MVC Thymeleaf view resolution.
    
     spring.thymeleaf.prefix=classpath:/templates/ # Prefix that gets prepended to view names when building a URL.
    
     spring.thymeleaf.suffix=.html # Suffix that gets appended to view names when building a URL.
    

    【讨论】:

    • 我应该从哪里找到这个配置文件?我正在使用 maven,当我尝试从依赖项列表中更改一些内容时,它不允许我在那里更改任何内容。有些代码甚至被隐藏了。我是否应该手动下载要更改配置的依赖项?
    • 在您的类路径中添加一个 application.properties 文件并放置所有属性。 src/main/resources/application.properties
    【解决方案2】:

    thymeleaf 模板的默认目录位置是:

    src/main/resources/templates
    

    其他路径是 Spring Boot 的标准约定。

    【讨论】:

      【解决方案3】:

      这里有两件事: 1. 如果您使用的是 Maven,并且我假设没有自定义文件夹名称。那么文件夹名称应该是 src 而不是 source。 2. 重命名文件夹后,将您的模板移动到 src/resources 内的“模板”文件夹中,这应该可以正常运行。

      【讨论】:

      • @Hmanshu Bhardwaj 我在问题中打错了字。在 netbeans 中,文件夹结构显示如下 src/main/resources,它位于 netbeans 项目层次结构中的其他资源目录下。我将修复问题中的错字。谢谢你的帮助。现在也许你可以提供一些进一步的反馈?
      • 我已经给了2分。您的模板是否在 src/main/resources 下的模板目录中?
      • @Himanshu 我在 src/main/resources 下创建了模板目录并将 html 文件放在那里,现在问题解决了。感谢您的支持。我会将问题标记为已回答。
      • 对我来说唯一的错误是文件夹的名称。只需确保正确编写 templates
      【解决方案4】:

      对于放在文件夹 src/main/resources/templates/ 中的 Thymeleaf 模板文件,您也可以查看 http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/ 。希望您会发现使用弹簧靴启动 thymeleaf 很容易。

      【讨论】:

        【解决方案5】:

        我在使用 Spring boot + Gradle 时遇到了同样的问题,这些是我解决它所遵循的步骤:

        1. 打印出类路径

        2. 一旦我确定资源文件夹没有包含在类路径中,我就从磁盘重新加载了应用程序:

        1. Gradle 清理项目

        【讨论】:

          猜你喜欢
          • 2017-06-21
          • 2016-01-26
          • 2020-11-21
          • 2019-10-31
          • 2022-11-14
          • 2020-03-14
          • 2017-03-08
          • 2021-09-04
          • 2020-08-27
          相关资源
          最近更新 更多