java - Spring Boot 入门项目 - 错误解决模板

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

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

Html路径:

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

JUnit测试:

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

Dir tree

最佳答案

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

关于java - Spring Boot 入门项目 - 错误解决模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44996651/

相关文章:

Java Ajax 显示进度

java - SonarQube 在 Spring Framework Controller 和 Spring Framework 应用程序主类中显示安全错误

java - 没有@ EnableWebMvc,NoHandlerFoundException的自定义异常处理程序将无法工作

html - Thymeleaf - Chrome 看不到 css 文件

java - 我可以注释从父类(super class)继承的成员吗?

Javadoc @param 与 -

java - 如何在 ResourceStreamRequestHandler#configure(..) 中设置Header(迁移问题)

java - 从 Spring Boot 中排除 Maven 依赖项

jquery - 自定义滚动事件

java - 单选按钮标签更改