如何从春季启动中找到Thymeleaf模板

编程入门 行业动态 更新时间:2024-10-25 00:33:52
本文介绍了如何从春季启动中找到Thymeleaf模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I am trying to follow this tutorial at www.thymeleaf/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模板

本文发布于:2023-10-12 01:27:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:春季   模板   中找到   Thymeleaf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!