3

I am running a spring boot application with Thymeleaf. When I run the application through my IDE (IntelliJ) everything runs fine. However, when I run the application through the command line (java -jar) the views do not resolve 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:

My Directory Structure

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 ran from a jar file?

Nick Humrich
  • 14,905
  • 8
  • 62
  • 85

2 Answers2

9

I think the answer is that it depends on your build configuration. The directory "src/main/views" is not a standard resource location for any common build tool, so you would have to explicitly add it to the configuration of the tool you use to build your jar.

If I were you I would go with the flow (why be different?), and just use "src/main/resources" for classpath resources. I would also leave out the thymeleaf configuration completely and let Spring Boot handle it, putting my templates in "src/main/resources/templates".

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
0

I had the same problem and found out from the Springboot documentation that the following classpath are used by the framework:

  • /META-INF/resources/
  • /resources/
  • /public/
  • /static/

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

Example:

The file location

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

And in the application.properties

spring.view.prefix: /

spring.view.suffix: .html

Note that since you are using thymeleaf, you both need to import it in 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">
Community
  • 1
  • 1
slonepi
  • 164
  • 2
  • 6