Proper Thymeleaf Species Layout for Spring

I am running the spring boot application with Thymeleaf. When I run the application through my IDE (IntelliJ), everything works fine. However, when I run the application through the command line ( java -jar ), the views are not allowed, and I get the following error:

 org.thymeleaf.exceptions.TemplateInputException: Error resolving template "index", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.TemplateRepository.getTemplate(TemplateRepository.java:245) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1104) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1060) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1011) at org.thymeleaf.spring3.view.ThymeleafView.renderFragment(ThymeleafView.java:335) at org.thymeleaf.spring3.view.ThymeleafView.render(ThymeleafView.java:190) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1225) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1012) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959) 

Here are my settings:

The structure of my catalog

 PROJECT-ROOT --src --main --java --controllers --[CLASS WITH MAIN METHOD] --views --index.html 

My template resolver:

 @Bean public ViewResolver viewResolver() { ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver(); templateResolver.setTemplateMode("XHTML"); templateResolver.setPrefix("views/"); templateResolver.setSuffix(".html"); SpringTemplateEngine engine = new SpringTemplateEngine(); engine.setTemplateResolver(templateResolver); ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(engine); return viewResolver; } 

Where should I put the views so that they can be correctly resolved when launched from the jar file?

+3
source share
2 answers

I think the answer is that it depends on the build configuration. The src / main / views directory is not a standard resource location for any public build tool, so you will need to explicitly add it to the configuration of the tool used to build your jar.

If I were you, I would go with the stream (why be different?) And just use the "src / main / resources" resources for the class resources. I also completely exclude thymeleaf configuration and let Spring boot process it by putting my templates in "src / main / resources / templates".

+9
source

I had the same problem, and I learned from the Springboot documentation that the following class path uses the framework:

  • / META-INF / resources /
  • /resources/
  • /state/
  • /static/

This means that you can put folder templates in any of the following directories, and the views inside will be found by your application.

Example:

File location

/src/main/public/templates/index.html

And in application.properties

spring.view.prefix: /

spring.view.suffix: .html

Note that since you are using thymeleaf, you need to import it into your dependencies (Maven?) And use it in your html, like this:

 <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"> 
0
source

Source: https://habr.com/ru/post/1208142/


All Articles