Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to locate Thymeleaf template from spring boot

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>   
like image 733
Gustav Avatar asked Dec 25 '16 01:12

Gustav


1 Answers

Spring Boot includes auto-configuration support for the thymeleaf templating engines, your templates will be picked up automatically from src/main/resources/templates.

if you are customize you template location then use below thymeleaf property configuration available in Spring Boot.

 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.
like image 70
Chandra Avatar answered Oct 12 '22 01:10

Chandra