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

人走茶凉 提交于 2020-01-10 20:09:55

问题


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


回答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.




回答2:


use

 return new ModelAndView("member2",map);

instead of

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




回答3:


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



来源:https://stackoverflow.com/questions/48400967/error-resolving-template-with-spring-boot-using-thymeleaf-packaged-in-a-jar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!