Java 如何从 Spring Boot 中找到 Thymeleaf 模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41318005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to locate Thymeleaf template from spring boot
提问by Gustav
I am trying to follow this tutorial at http://www.thymeleaf.org/doc/articles/springmvcaccessdata.htmlto learn how to send responses to Thymeleaf template. But I get this error: Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
我正在尝试按照http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html上的本教程学习如何向 Thymeleaf 模板发送响应。但我收到此错误:找不到模板位置:类路径:/模板/(请添加一些模板或检查您的 Thymeleaf 配置)
I put the message.html file in Other Sources directory and inside src/main/resources under <default package>
.
我将 message.html 文件放在 Other Sources 目录和 src/main/resources 下的<default package>
.
So the structure looks like :
所以结构看起来像:
SomeProject
-Other Sources
--src/main/resources
---<default package>
----message.html
SomeProject -Other Sources --src/main/resources --- <default package>
----message.html
I was wondering why it shows under <default package>
but not under <template>
? Could it be the problem? If so how am I supposed to change it? I am using netbeans and maven. Any ideas please? These are the dependencies I have in my pom.xml:
我想知道为什么它显示在下面<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>
In the controller I have
在控制器中我有
@RequestMapping(value = "message", method = RequestMethod.GET)
public String messages(Model model) {
model.addAttribute("messages", messageRepository.findAll());
return "message";
}
And in the view:
在视图中:
<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>
采纳答案by Himanshu Bhardwaj
2 things here : 1. If you are using Maven, and I assume no customizations to folder names. Then the folder name should be src instead of source. 2. Once the folder has been renamed move your templates into 'templates' folder inside src/resources this should run fine.
这里有 2 件事: 1. 如果您使用的是 Maven,并且我假设没有对文件夹名称进行自定义。然后文件夹名称应该是 src 而不是 source。2. 文件夹重命名后,将模板移动到 src/resources 内的“模板”文件夹中,这应该可以正常运行。
回答by Chandu
Spring Boot includes auto-configuration support for the thymeleaf templating engines, your templates will be picked up automatically from src/main/resources/templates.
Spring Boot 包括对 thymeleaf 模板引擎的自动配置支持,您的模板将自动从 src/main/resources/templates 中选取。
if you are customize you template location then use below thymeleaf property configuration available in Spring Boot.
如果您自定义模板位置,则使用 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.
spring.thymeleaf.check-template=true # 在渲染之前检查模板是否存在。
spring.thymeleaf.check-template-location=true # 检查模板位置是否存在。
spring.thymeleaf.enabled=true # 启用 MVC Thymeleaf 视图分辨率。
spring.thymeleaf.prefix=classpath:/templates/ # 在构建 URL 时添加到视图名称的前缀。
spring.thymeleaf.suffix=.html # 在构建 URL 时附加到视图名称的后缀。
回答by chocksaway
回答by Yogendra Mishra
For Thymeleaf template files put in folder src/main/resources/templates/ and it will work for you also do check out http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf/. Hope you will find it easy to kick start thymeleaf with spring boot.
对于放置在文件夹 src/main/resources/templates/ 中的 Thymeleaf 模板文件,它也适用于您也可以查看http://www.mkyong.com/spring-boot/spring-boot-hello-world-example-thymeleaf /. 希望你会发现用弹簧靴轻松启动百里香。