Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error resolving template with Spring Boot using Thymeleaf packaged in a .jar

I have a Spring Boot application using Thymeleaf as template resolver, which works fine when debugging from NetBeans, but gives me this error running its .jar:

Error resolving template "/theme/property", template might not exist or might not be accessible by any of the configured Template Resolvers

The app is set to auto-configurer with the annotation @SpringBootApplication, at an extension of SpringBootServletInitializer. I haven't set any contextPath into the properties file. I'm using Thymeleaf 2.1.6 and Spring 4 version. The jar is generated with Maven.

Doing some research I've come out that in some controllers I was passing a double slash, which I've solved but most pages still not working.

This controller works:

@GetMapping("/{idweb}")
String frontEndHome(@PathVariable("idweb")Integer idweb, Model model){
...
return "theme/home";

With the return statement set as return "/theme/home"; doesn't work. I guess, because the template resolver is recieving a double slash (//).

This other controller raises the error:

@GetMapping("/{idweb}/property")
String frontEndProperty(@PathVariable("idweb") Integer idweb, @RequestParam(value = "idproperty", required = false) Integer idproperty, Model model) throws Exception {
...
return "theme/property";

The index controller works fine as well:

@GetMapping("/")
public String index(Model model){
   ...
   return "index";
}

That's my application starter class:

@SpringBootApplication
public class RentalWebsApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder  application) {
       return application.sources(RentalWebsApplication.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(RentalWebsApplication.class, args);
    }
}

For Thymeleaf I haven't set any configuration, although I've tested the app setting this into the application.properties file, with the same result:

spring.thymeleaf.prefix=classpath:/templates/

All html files are set into:

src/main/resources/templates

The html files from the examples are in:

src/main/resources/templates/index.html

src/main/resources/templates/theme/home.html

src/main/resources/templates/theme/property.html

There are some other questions dealing with the same issue, but none has a solution that works for me. Any help, would be much appreciated.

Update

Deploying the jar into Pivotal Web Services, the whole website works fine, but not deploying it with Boxfuse, Heroku or running the jar locally. Therefore, I guess the origin of the problem is some wrong configuration, that Pivotal system detects and corrects.*

* PWS isn't correcting a configuration problem. It unpacks your jar file before running the application which stops the double slash from causing a problem. – Andy Wilkinson

like image 646
Aleix Alcover Avatar asked Jan 23 '18 11:01

Aleix Alcover


People also ask

Where do Thymeleaf templates go in spring boot?

Thymeleaf in Spring Boot By default, HTML files should be placed in the resources/templates location.


2 Answers

At the end the solution was related to the double slashes, that the classpath:/templates/ gets if we set a return statement with a slash at the beginning like:

return "/theme/property"

Instead of:

return "theme/property"

In my case, the problem was not at the controller, but in the html with the Thymeleaf references of fragments, like in this example:

<footer th:replace="/index::footer"></footer>

Instead of:

<footer th:replace="index::footer"></footer>

What I don't understand is why the IDE's (NetBeans and STS), where not raising the error.

like image 70
Aleix Alcover Avatar answered Sep 28 '22 17:09

Aleix Alcover


use

 return new ModelAndView("member2",map);

instead of

 return new ModelAndView("/member2",map);

enter image description here

like image 24
Vikki Avatar answered Sep 28 '22 17:09

Vikki