Thymleaf 无法解析带片段的模板

发布于 2025-01-18 06:46:52 字数 3666 浏览 0 评论 0原文

我正在尝试使用我创建的用于 CSS 格式化的片段来创建包含一些动态数据的模板。我的应用程序基于 Spring Boot。 这是我的片段:

<!DOCTYPE html >
<html lang="en" th:fragment="common_error(code,message,description, supportMessage)" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title th:text="|${code} — ${message}|">Title here</title>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
    <link type="text/css" rel="stylesheet" th:href="@{/css/error.css}" />
    <link rel="icon" type="image/x-icon" th:href="@{/images/favicon.ico}"/>
</head>
<body>
<div class="container">
    <h1><span>error</span><span th:replace="${code}">500</span></h1>
    <p class="title" th:insert="${message}"></p>
    <span th:replace="${description}">Description here</span>
</div>
<div class="wrapper">
    <div class="triangle-container"><div></div></div>
    <p class="support" th:insert="${supportMessage}" >
        If you have further questions, please visit<br>
        <a href="#">support.example.com</a>
    </p>
</div>
</body>
</html>

当我将此片段与这样的静态数据一起使用时,一切正常。

<html  xmlns:th="http://www.thymeleaf.org" th:replace="fragments/error :: common_error(~{::[@id='code']/text()},~{::[@id='message']/text()},~{::.description}, _)">
<head></head>
<body>
    <div id="code">500</div>
    <div id="message">Internal Server Error</div>
    <p class="description">Sorry, we are currently experiencing technical difficulties.</p>
    <p class="description">Our team is addressing the issues and will make the system available as soon as possible.</p>
</body>
</html>

但是,如果我尝试添加一些变量,那么我会在模板解析期间收到错误。

<!DOCTYPE html >
<html  xmlns:th="http://www.thymeleaf.org"
       th:replace="fragments/error::common_error(~{::[@id='code']/text()},~{::[@id='message']/text()},~{::.description}, ~{::.support_message})">
<head></head>
<body>
    <h1>error<span id="code" th:text="${code} ?: #{error.default.code}"></span></h1>
    <p id="message" th:text="${message} ?: #{error.default.message}"></p>
    <div class="description"><span th:utext="${description}"></span></div>
    <p class="support_message" th:utext="#{support.default.message}"/>
</body>
</html>
Error:

    Caused by: org.attoparser.ParseException: Error resolving fragment: "${code}": template or fragment could not be resolved (template: "fragments/error" - line 13, col 33)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 43 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "${code}": template or fragment could not be resolved (template: "fragments/error" - line 13, col 33)

请帮助:)

UPD 当我从模板中排除片段时,它也可以正常工作并且我的变量插入正常。

I am trying to create template with some dynamic data using fragment I created to format with CSS. My app based on Spring Boot.
There is my fragment:

<!DOCTYPE html >
<html lang="en" th:fragment="common_error(code,message,description, supportMessage)" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title th:text="|${code} — ${message}|">Title here</title>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
    <link type="text/css" rel="stylesheet" th:href="@{/css/error.css}" />
    <link rel="icon" type="image/x-icon" th:href="@{/images/favicon.ico}"/>
</head>
<body>
<div class="container">
    <h1><span>error</span><span th:replace="${code}">500</span></h1>
    <p class="title" th:insert="${message}"></p>
    <span th:replace="${description}">Description here</span>
</div>
<div class="wrapper">
    <div class="triangle-container"><div></div></div>
    <p class="support" th:insert="${supportMessage}" >
        If you have further questions, please visit<br>
        <a href="#">support.example.com</a>
    </p>
</div>
</body>
</html>

When I use this fragment with static data like this, then all works fine.

<html  xmlns:th="http://www.thymeleaf.org" th:replace="fragments/error :: common_error(~{::[@id='code']/text()},~{::[@id='message']/text()},~{::.description}, _)">
<head></head>
<body>
    <div id="code">500</div>
    <div id="message">Internal Server Error</div>
    <p class="description">Sorry, we are currently experiencing technical difficulties.</p>
    <p class="description">Our team is addressing the issues and will make the system available as soon as possible.</p>
</body>
</html>

But if I try to add some variables, then I receive an error during template parsing.

<!DOCTYPE html >
<html  xmlns:th="http://www.thymeleaf.org"
       th:replace="fragments/error::common_error(~{::[@id='code']/text()},~{::[@id='message']/text()},~{::.description}, ~{::.support_message})">
<head></head>
<body>
    <h1>error<span id="code" th:text="${code} ?: #{error.default.code}"></span></h1>
    <p id="message" th:text="${message} ?: #{error.default.message}"></p>
    <div class="description"><span th:utext="${description}"></span></div>
    <p class="support_message" th:utext="#{support.default.message}"/>
</body>
</html>

Error:

    Caused by: org.attoparser.ParseException: Error resolving fragment: "${code}": template or fragment could not be resolved (template: "fragments/error" - line 13, col 33)
at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393)
at org.attoparser.MarkupParser.parse(MarkupParser.java:257)
at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230)
... 43 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateInputException: Error resolving fragment: "${code}": template or fragment could not be resolved (template: "fragments/error" - line 13, col 33)

Please help :)

UPD When I exclude fragment from my template, then it also works fine and my variables inserted ok.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思慕 2025-01-25 06:46:52

我以这种方式修改了我的解决方案:
在模板中,我将“描述”变量重命名为“ custom_description”,以避免模板和片段之间的可变名称冲突。
我还向片段中添加了一个其他“ titlemessage”变量,以进行正确的渲染。
而且,我也改变了将变量传递给模板和片段的方式,按照此建议

分段:

<!DOCTYPE html >
<html lang="en" th:fragment="common_error(code, titleMessage, message, description, supportMessage)" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title th:text="|${code} — ${titleMessage}|">Title here</title>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
    <link type="text/css" rel="stylesheet" th:href="@{/css/error.css}" />
    <link rel="icon" type="image/x-icon" th:href="@{/images/favicon.ico}"/>
</head>
<body>
<div class="container">
    <h1><span>error</span><span th:replace="${code}">500</span></h1>
    <p class="title" th:insert="${message}"></p>
    <span th:replace="${description}">Description here</span>
</div>
<div class="wrapper">
    <div class="triangle-container"><div></div></div>
    <div class="support">
        <p th:insert="${supportMessage}" >
            If you have further questions, please visit
        </p>
        <p><a href="#">support.example.com</a></p>
    </div>

</div>
</body>
</html>

模板:

<!DOCTYPE html >

<html  xmlns:th="http://www.thymeleaf.org"
       th:replace="fragments/error::common_error(~{::[@id='code']/text()}, #{error.default.message}, ~{::[@id='message']/text()}, ~{::.description}, ~{::[@id='support_message']/text()})">
<head></head>
<body>
    <h1>error<span id="code">500</span></h1>
    <div id="message">[[#{error.default.message}]]</div>
    <div class="description"><span th:utext="${custom_description}"></span></div>
    <p id="support_message" >[[#{support.default.message}]]</p>
</body>
</html>

现在一切正常。

I modified my solution in this way:
In the template, I renamed the "description" variable to "custom_description" to avoid variable name conflicts between the template and the fragment.
I also added an additional "titleMessage" variable to the fragment for correct rendering.
And also I changed the way I pass my variables to template and fragment as per this suggestion.

Fragment:

<!DOCTYPE html >
<html lang="en" th:fragment="common_error(code, titleMessage, message, description, supportMessage)" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title th:text="|${code} — ${titleMessage}|">Title here</title>
    <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400" rel="stylesheet">
    <link type="text/css" rel="stylesheet" th:href="@{/css/error.css}" />
    <link rel="icon" type="image/x-icon" th:href="@{/images/favicon.ico}"/>
</head>
<body>
<div class="container">
    <h1><span>error</span><span th:replace="${code}">500</span></h1>
    <p class="title" th:insert="${message}"></p>
    <span th:replace="${description}">Description here</span>
</div>
<div class="wrapper">
    <div class="triangle-container"><div></div></div>
    <div class="support">
        <p th:insert="${supportMessage}" >
            If you have further questions, please visit
        </p>
        <p><a href="#">support.example.com</a></p>
    </div>

</div>
</body>
</html>

Template:

<!DOCTYPE html >

<html  xmlns:th="http://www.thymeleaf.org"
       th:replace="fragments/error::common_error(~{::[@id='code']/text()}, #{error.default.message}, ~{::[@id='message']/text()}, ~{::.description}, ~{::[@id='support_message']/text()})">
<head></head>
<body>
    <h1>error<span id="code">500</span></h1>
    <div id="message">[[#{error.default.message}]]</div>
    <div class="description"><span th:utext="${custom_description}"></span></div>
    <p id="support_message" >[[#{support.default.message}]]</p>
</body>
</html>

And now everything works OK.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文