java - 将 Jade4J 配置为 Spring 中的默认模板引擎

标签 java spring jade4j

我最近为我的大学 Java 类(class)项目选择了 Spring 框架,为我的模板引擎选择了 Jade4J,已经将依赖项放入 pom.xml 中并安装了包,但是当我调用 Jade4J.render("./index.jade", model); 但我收到“./index.jade(文件未找到)”响应。查看 github repo 后没有具体信息如何使用它将查找模板的目录来创建配置类。我什至尝试将index.jade文件放在项目中的任何位置( Controller 目录、实现main.java的目录、资源目录、webapp目录)。如果有任何帮助,我将不胜感激,我将提供任何进一步的信息

编辑1
将 JadeConfig 类添加到项目中,内容如下:

@Bean
public SpringTemplateLoader templateLoader() {
    SpringTemplateLoader templateLoader = new SpringTemplateLoader();
    templateLoader.setBasePath("/templates/");
    templateLoader.setEncoding("UTF-8");
    templateLoader.setSuffix(".jade");
    return templateLoader;
}

@Bean
public JadeConfiguration jadeConfiguration() {
    JadeConfiguration configuration = new JadeConfiguration();
    configuration.setCaching(false);
    configuration.setTemplateLoader(templateLoader());
    return configuration;
}

@Bean
public ViewResolver viewResolver() {
    JadeViewResolver viewResolver = new JadeViewResolver();
    viewResolver.setConfiguration(jadeConfiguration());
    return viewResolver;
}

我的索引 Controller 具有以下功能:

@GetMapping(value = "/")
public String greeting() throws IOException {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("title", "Index Page");
    String html = Jade4J.render("./index.jade", model);
    return html;
}

最后模板index.jade路径是src\main\webapp\templates\index.jade

最佳答案

对于尝试查找Jade4j信息的新人。

在这里您可以找到没有错误的示例以及模板引擎的其他示例 https://github.com/jreijn/spring-comparing-template-engines

主要问题是您真的需要 Jade4j 吗?

最后一次发布是2017年,他没有一个大社区https://mvnrepository.com/artifact/de.neuland-bfi/jade4j

关于java - 将 Jade4J 配置为 Spring 中的默认模板引擎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52948233/

相关文章:

使用 log4j 将 java 游戏室记录到单独的日志文件是个好主意吗?

java - CascadeType.ALL 和 "insertable = false, updatable = false"是否相互排除?

java - 访问 WAR 中的 AWS 凭证文件

node.js - 遍历jade/pugjs 中的对象数组

node.js - 按钮未插入 Pug 中的表格行内

java - 如何在类之间共享数据库连接? (JDBC)

java - 如何使用 apache http 客户端库 4.1 设置自定义用户代理?

java - bean 未知与春

java - RabbitMQ Spring AMQP - 一段时间后的消息处理