Thymeleaf 春季景观的正确位置 [英] Proper location of Thymeleaf views for Spring

查看:37
本文介绍了Thymeleaf 春季景观的正确位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Thymeleaf 运行 Spring Boot 应用程序.当我通过 IDE (IntelliJ) 运行应用程序时,一切正常.但是,当我通过命令行 (java -jar) 运行应用程序时,视图无法解析,并且出现以下错误:

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)

这是我的设置:

我的目录结构

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

我的模板解析器:

@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;
  }

我应该把视图放在哪里,以便在从 jar 文件运行时可以正确解析它们?

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

推荐答案

我认为答案取决于您的构建配置.目录src/main/views"不是任何常见构建工具的标准资源位置,因此您必须将其显式添加到用于构建 jar 的工具的配置中.

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.

如果我是你,我会顺其自然(为什么不同?),并且只使用src/main/resources"作为类路径资源.我也会完全省略 thymeleaf 配置,让 Spring Boot 处理它,将我的模板放在src/main/resources/templates"中.

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".

这篇关于Thymeleaf 春季景观的正确位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆