我想要按照本教程http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html瞭解如何發送對Thymeleaf模板的響應。但我得到這個錯誤:無法找到模板位置:classpath:/模板/(請添加一些模板或檢查您的Thymeleaf配置)如何從彈簧啓動中找到Thymeleaf模板
我把message.html文件放在其他源目錄和src/main/resources下<default package>
。
所以結構如下:
SomeProject - 其他來源 --src /主/資源 --- <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>
@HhuShu Bhardwaj我在這個問題上犯了一個錯字。在NetBeans中,文件夾結構顯示如下src/main/resources,它位於NetBeans項目層次結構中其他資源目錄下。我會解決問題中的錯字。謝謝你的幫助。現在,也許你可以進一步反饋? – Gustav
我已經給了2分。您的模板是否位於src/main/resources下的模板目錄中? –
@ Himanshu我在src/main/resources下創建了模板目錄,並將html文件放在那裏,現在問題已解決。感謝您的支持。我會將問題標記爲已回答。 – Gustav