spring - 使用片段时 Thymeleaf PDF 模板解析错误
问题描述
我正在尝试在我的 PDF 模板中包含一个信头,如下所示:
片段/header.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Header</title>
</head>
<body>
<div id="header" class="clearfix" th:fragement="header(sender, name, address1, address2, today)">
<div id="header-left">
<img class="logo" src="app/src/main/resources/template/pdf/assets/logo.png"/>
<div id="recipient">
<p th:object="${sender}"><span th:text="*{name}"></span> - <span th:text="*{address1short}"></span> - <span th:text="*{address2}"></span></p>
<p th:text="${name}"></p>
<p>
<span th:text="${address1}"></span><br/>
<span th:text="${address2}"></span>
</p>
</div>
</div>
<div id="header-right" th:object="${sender}">
<p><strong th:text="*{name}"></strong></p>
<p>
<span th:text="*{address1}"></span><br />
<span th:text="*{address2}"></span><br />
<span class="label" th:text="*{phoneLabel} + ':'"></span><span th:text="*{phone}"></span><br />
<span class="label" th:text="*{faxLabel} + ':'"></span><span th:text="*{fax}"></span><br />
<span class="label" th:text="*{emailLabel} + ':'"></span><span th:text="*{email}"></span><br />
<strong><span th:text="*{internetLabel}"></span>: <span th:text="*{internet}"></span></strong>
</p>
<p>
<span th:text="*{court}"></span><br />
<span th:text="*{registryNr}"></span><br />
<span th:text="*{directorLabel}"></span>: <span th:text="*{director}"></span><br />
<span th:text="*{vatId}"></span><br />
</p>
<p><span th:text="*{todayPrefix}"></span> <span th:text="${today}"></span></p>
</div>
</div>
</body>
</html>
模板.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Template</title>
<link rel="stylesheet" type="text/css" media="all" href="app/src/main/resources/template/pdf/assets/style.css"/>
</head>
<body>
<div th:replace="app/src/main/resources/template/pdf/fragments/header :: header(${sender}, ${name}, ${address1}, ${address2}, ${today})"></div>
<!-- omitted for brevity -->
</body>
</html>
当我将标头代码直接放入模板时,一切正常。但是当我尝试将其作为片段包含时,如上所示,我收到以下错误:
org.thymeleaf.exceptions.TemplateInputException:模板解析期间发生错误(模板:“app/src/main/resources/template/pdf/fragments/header.html” - 第 9 行,第 6 列)
template.html 位于 app/src/main/resources/template/pdf/ 文件夹中。我必须包含带有完整路径(从项目的根目录)的 css 和图像文件才能让它们工作。我尝试包含带有完整路径(如上)和仅片段/标题的片段,但每次我都会遇到相同的错误。
我在用着
- spring-boot-starter-thymeleaf 2.1.1
- 百里香叶 3.0.11
Stacktrace:[删除了 bc 的字符限制]
更新
片段的正确路径是(在我的情况下)“template/pfd/fragment/header”(因此,来自资源文件夹的完整路径)。此外,th:frag e ment="header" 中有一个错字。
解决方案
推荐阅读
- asp.net - ASP.NET Web api 获取参数
- shell - 如何在 go 文件中执行 shell 脚本?
- javascript - 按输入值创建深度过滤器
- algorithm - 基于另一个列表过滤列表的高效方法?
- java - 如何获取 EDT 时区的 GMT 值?
- velocity - 检查速度模板中的变量是数字还是字母
- flutter - 如何在这个 fl-chart 颤动中删除 XY 值
- python - 在 Python 中使用 .csv 列中的列表
- python - 将 pip 升级到 21.1.1 后的软件包安装问题
- git - 您可以将代码从一个分支推送到另一个分支,将不同的下游推送到不同的上游吗