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

13,945

Solution 1

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.

Solution 2

use

 return new ModelAndView("member2",map);

instead of

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

enter image description here

Solution 3

Remove spring.thymeleaf.prefix=classpath:/templates/ from your application.properties.

Share:
13,945
Aleix Alcover
Author by

Aleix Alcover

Updated on June 17, 2022

Comments

  • Aleix Alcover
    Aleix Alcover almost 2 years

    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

  • Aleix Alcover
    Aleix Alcover over 6 years
    Unfortunately, removing it doesn't change anything. Debugging the controller of any page, setting a breakpoint at the return statement, I get this message: Thread http-nio-8080-exec-2 stopped at java.lang.reflect.Method.invoke - compiled without debug info.
  • Den B
    Den B over 6 years
    sory man, I'm our of ideas(( maybe if you send me your project (if it's not big) I can find the problem
  • Andy Wilkinson
    Andy Wilkinson over 6 years
    The problem with // is specific to being run from a jar file. The JDK's support for loading resources from a jar file doesn't like // in a URL, whereas the support for loading resources from the file system doesn't have that limitation.
  • SametSahin
    SametSahin over 4 years
    In my case I accidentally written "Hello" instead of "hello". "hello.html" is the name of the file resides in "templates" folder.