【问题标题】:SpringBoot Starter Project - Error resolving templateSpring Boot Starter Project - 错误解析模板
【发布时间】:2017-07-09 12:58:01
【问题描述】:

我正在编写一个 JUnit 测试,它应该测试一个 html 页面的标题。本项目是一个 SpringBoot、Thymeleaf 入门项目。

HTML路径:

private final static String HTML_PATH = "/pages/dashboard/dashboard.html";

JUnitTest:

@Test
public void shouldRenderPageTitle() throws IOException, NodeSelectorException {
    Map<String,Object> model = new HashMap<>();
    model.put("pageTitle", "expected title");
    HtmlElements tags = testEngine.process(HTML_PATH, model);
    assertThat(tags.matching("title"), isSingleElementThat(hasOnlyText("expected title")));
}

Thymeleaf 配置

@Configuration
public class ThymeleafConfig {

@Bean
public TemplateResolver templateResolver() {
    TemplateResolver templateResolver = new TemplateResolver();
    templateResolver.setPrefix("/resources/templates/");
    return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    templateEngine.setTemplateResolver(templateResolver());
    templateEngine.addDialect(new LayoutDialect());
    templateEngine.addDialect(new SpringStandardDialect());
    return templateEngine;
}

@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
    ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
    viewResolver.setTemplateEngine(templateEngine());
    return viewResolver;
}

}

错误:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/pages/dashboard/dashboard.html"

【问题讨论】:

  • private final static String HTML_PATH = "pages/dashboard/dashboard.html";?
  • 我尝试过这种方法,但没有成功
  • 尝试不使用.html

标签: java spring-boot thymeleaf spring-boot-test


【解决方案1】:

你必须使用
私有最终静态字符串 HTML_PATH = "/pages/dashboard/dashboard"; //没有“.html”

【讨论】:

    【解决方案2】:

    你能不能试试替换这行代码

    templateResolver.setPrefix("/resources/templates/");
    

    用这个

    templateResolver.setPrefix("templates/");
    

    【讨论】:

      猜你喜欢
      • 2019-11-29
      • 2018-11-30
      • 2019-05-02
      • 2014-05-11
      • 1970-01-01
      • 2014-12-29
      • 2021-09-25
      • 2020-10-07
      • 1970-01-01
      相关资源
      最近更新 更多