Issue
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)
I put the message.html file in Other Sources directory and inside src/main/resources under <default package>
.
So the structure looks like :
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:
<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>
Solution
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.
Answered By - Himanshu Bhardwaj
Answer Checked By - Mary Flores (JavaFixing Volunteer)