java - Pebble 模板继承失败且没有错误
问题描述
我在 Spring Boot 2 中使用 Pebble 模板引擎,在我开始使用继承之前一切正常。浏览器显示一个空白页面,根本没有返回任何内容。不幸的是,服务器和 Catalina(我使用的是 Tomcat 8.5)日志显示没有错误。
在我的 pom 中,我有以下依赖项:
- Spring Boot 2.0.3(作为父级)
- pebble-spring-boot-2-starter(试过 2.6.2 和 3.0.0.BETA01)
- spring-boot-starter-数据-jpa
- spring-boot-starter-web
- spring-boot-starter-安全
我有以下 application.properties
- pebble.suffix = .html.peb
- pebble.cache = false(也试过了)
我的父模板 (resources/templates/base.html.peb)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head></head>
<body>Template test</body>
</html>
我的子模板 (resources/templates/child.html.peb)
{% extends "base.html.peb" %}
当我删除继承时,Pebble 工作正常,并且包含并显示模型,所以 Pebble 确实可以工作。
解决方案
Pebble Spring Boot Starter 通过连接前缀、模板名称和后缀来解析模板路径:
公共类 PebbleTemplateAvailabilityProvider 实现 TemplateAvailabilityProvider {
@Override
public boolean isTemplateAvailable(String view, Environment environment, ClassLoader classLoader,
ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("com.mitchellbosecke.pebble.PebbleEngine", classLoader)) {
String prefix = environment.getProperty("pebble.prefix", PebbleProperties.DEFAULT_PREFIX);
String suffix = environment.getProperty("pebble.suffix", PebbleProperties.DEFAULT_SUFFIX);
return resourceLoader.getResource(ResourceLoader.CLASSPATH_URL_PREFIX + prefix + view + suffix).exists();
} else {
return false;
}
}
}
如果模板在 'extends' 指令中指定了后缀,则后缀将被再附加一次,并且找不到模板,例如:
资源/模板/base.html.peb.html.peb
为了解决这个问题,必须在 'extends' 指令中指定不带前缀的 pebble 模板名称:
{% extends "base" %}
对我来说,这是一个错误。Pebble Spring Boot Starter 应该能够检测到指定的基本模板是否带有后缀。
推荐阅读
- html - 当内容大小溢出父容器时包装内容
- wordpress - 新用户注册时如何将 wp_users -> 用户登录名插入 wp_terms?
- sql - 获取记录需要很长时间
- google-api - 谷歌云视觉无法检测到一个字符
- selenium - 如何使用 selenium webdriver 处理出现在加载页面中间的证书弹出窗口
- regex - vim regex removing all words that contain a certain character that will be at the end of the word
- c# - 当我们切换缩放比例时,AutoScale Factor 没有正确更新
- node.js - 尽管 MongoDB 中存在集合,但 Mongoose 显示空对象
- javascript - 谷歌分析是否有任何字段可用于 eventID 和 eventURL
- android - how to fetch data from parse database and use it in different function