Thymleaf 无法解析带片段的模板
我正在尝试使用我创建的用于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以这种方式修改了我的解决方案:
在模板中,我将“描述”变量重命名为“ custom_description”,以避免模板和片段之间的可变名称冲突。
我还向片段中添加了一个其他“ titlemessage”变量,以进行正确的渲染。
而且,我也改变了将变量传递给模板和片段的方式,按照此建议。
分段:
模板:
现在一切正常。
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:
Template:
And now everything works OK.