6

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
  • 599
  • 3
  • 11
  • 27
  • Can you provide more information: your application starter class; thymeleaf configuration; location of html files in you project? – Den B Jan 23 '18 at 11:52
  • @Den B I have added your requested information. – Aleix Alcover Jan 23 '18 at 12:26
  • It seems that configurations is Ok. Try to locate you home.html and property.html to templates folder where index.html is located. After change your return statements in controller to "home" and "property" respectively. Let me know if error continue. – Den B Jan 23 '18 at 13:01
  • After moving the two files to the templates folder, the behaviour remains the same. The most puzzling thing, is that in /templates/ there are some other html files and none of them works, except login.html, which takes the controller from the auto-configuration. – Aleix Alcover Jan 23 '18 at 14:36
  • I've updated the question, for which I still search for a solution. – Aleix Alcover Jan 25 '18 at 07:27
  • 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. @AleixAlcover can you provide a [minimal, complete, verifiable example](/help/mcve) that illustrates the problem? – Andy Wilkinson Jan 29 '18 at 11:30
  • @Andy Wilkinson My affirmation about PWS correcting a configuration problem, demonstrates the courage of ignorance. I'm sorry about that. Finally, debugging Thymeleaf code I've been able to understand what was wrong in my code. – Aleix Alcover Jan 29 '18 at 12:26

3 Answers3

11

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.

Aleix Alcover
  • 599
  • 3
  • 11
  • 27
  • 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. – Andy Wilkinson Jan 29 '18 at 13:26
  • In my case I accidentally written "Hello" instead of "hello". "hello.html" is the name of the file resides in "templates" folder. – SametSahin Dec 13 '19 at 20:42
4

use

 return new ModelAndView("member2",map);

instead of

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

enter image description here

Vikki
  • 1,897
  • 1
  • 17
  • 24
1

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

Den B
  • 843
  • 1
  • 10
  • 26
  • 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. – Aleix Alcover Jan 25 '18 at 09:33
  • sory man, I'm our of ideas(( maybe if you send me your project (if it's not big) I can find the problem – Den B Jan 25 '18 at 09:35