首页 > 解决方案 > 使用片段时 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 和图像文件才能让它们工作。我尝试包含带有完整路径(如上)和仅片段/标题的片段,但每次我都会遇到相同的错误。

我在用着

Stacktrace:[删除了 bc 的字符限制]

更新

片段的正确路径是(在我的情况下)“template/pfd/fragment/header”(因此,来自资源文件夹的完整路径)。此外,th:frag e ment="header" 中有一个错字。

标签: springthymeleaf

解决方案


常见的用途是

<div th:replace="fragments/header :: header"><div>

使用百里香叶


推荐阅读