【问题标题】:Proper location of Thymeleaf views for Spring春季 Thymeleaf 视图的正确位置
【发布时间】:2014-05-18 00:52:13
【问题描述】:

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

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 文件运行时可以正确解析它们?

【问题讨论】:

    标签: spring-mvc thymeleaf spring-boot


    【解决方案1】:

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

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

    【讨论】:

    • 这行得通,只是我不得不保留百里香叶配置。当我删除它时,spring 由于某种原因无法解决它。
    • 只是让其他人知道,我还必须将我的前缀更改为 templates/
    【解决方案2】:

    我遇到了同样的问题,从Springboot documentation 中发现框架使用了以下类路径

    • /META-INF/resources/
    • /资源/
    • /公共/
    • /静态/

    表示你可以将文件夹模板放在以下任意目录中,里面的视图会被你的应用程序找到。

    例子:

    文件位置

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

    并且在application.properties中

    spring.view.prefix: /

    spring.view.suffix: .html

    注意,由于您使用的是 thymeleaf,因此您都需要在依赖项(Maven?)中导入它并在 html 中使用它,如下所示:

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

    【讨论】:

      猜你喜欢
      • 2017-12-03
      • 2017-04-15
      • 2017-07-28
      • 2020-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多