java - 是否必须将我的所有模板 thymeleaf 放在目录 "rc/main/resources/templates"中?

标签 java spring thymeleaf

是否必须将我的所有模板 thymeleaf 放在目录“src/main/resources/templates”中?,不能是它们的子文件夹吗?,以改进组织

例如,我在 Controller 中有这个方法:

/**
 * List all centros.
 *
 * @param model
 * @return
 */
@RequestMapping(value = "/centros", method = RequestMethod.GET)
public String list(Model model) {
    model.addAttribute("centros", centroService.listAllCentros());
    System.out.println("Returning centros:");
    return "centros/centros";
}

我的 Centros 模板位于 resources/templates/centros/内。 有没有办法配置 Thymeleaf 来解析这些模板?

最佳答案

您可以告诉 Thymeleaf 在哪里查找模板:Thymeleaf migration

@Bean
public ITemplateResolver templateResolver() {
    final SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
    resolver.setApplicationContext(applicationContext);
    resolver.setSuffix(".html");
    resolver.setPrefix("classpath:/templates/");
    resolver.setTemplateMode(TemplateMode.HTML);
    resolver.setCacheable(false);
    return resolver;
}

或者,如果您将使用多个子文件夹,只需让您的 Controller 返回“/centros/centros”,这就是您现在正在做的事情。

关于java - 是否必须将我的所有模板 thymeleaf 放在目录 "rc/main/resources/templates"中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43241951/

相关文章:

java - Spring 中的嵌套 Thymeleaf 模板

java - 如何在 thymeleaf 中转义 'And' 运算符

java - NullPointerException 试图在一个移动应用程序中添加 2 个 Displayables

java - 如何同时做多个任务?

java - Spring 启动 2 中的 FeignClient

java - 如何将 postgresql 添加到 OpenShift Spring 应用程序?

java - thymeleaf 错误: org. springframework.expression.spel.SpelEvaluationException

需要 JavaFX 应用程序打包示例

java - Jpanel布局问题,显示两列按钮和标签,怎么办?

java - 如何在 Spring 中使用 setter 注入(inject)和 java 配置?