如何从春季启动中找到Thymeleaf模板 [英] How to locate Thymeleaf template from spring boot

查看:377
本文介绍了如何从春季启动中找到Thymeleaf模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试按照 http://www.thymeleaf.org /doc/articles/springmvcaccessdata.html 了解如何向Thymeleaf模板发送回复。但我收到此错误:无法找到模板位置:classpath:/ templates /(请添加一些模板或检查您的Thymeleaf配置)

I am trying to follow this tutorial at http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html to 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)

我将message.html文件放入其他Sources目录和< default package> 下的src / main / resources内部。

I put the message.html file in Other Sources directory and inside src/main/resources under <default package>.

所以结构如下:

SomeProject
-Other Sources
- src / main / resources
--- <默认包>
---- message.html

SomeProject -Other Sources --src/main/resources ---<default package> ----message.html

我想知道为什么它显示在< default package> 下,但不在< template> ?可能是问题吗?如果是这样我怎么能改变它?我正在使用netbeans和maven。有什么想法吗?这些是我在pom.xml中的依赖项:

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:

    <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>   


推荐答案

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

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.

这篇关于如何从春季启动中找到Thymeleaf模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆