java - Thymeleaf LD+JSON 输出报价转义
问题描述
我在服务器上生成 LD+JSON 字符串,我需要使用 Thymeleaf 在客户端上输出它。
生成的 JSON 在服务器上如下所示:
{
"@context" : "http://schema.org",
"@type" : "FAQPage",
"mainEntity" : [ {
"@type" : "Question",
"name" : "question text",
"acceptedAnswer" : {
"@type" : "Answer",
"text" : "answer text <a href=\"\">link</a> answer text."
}
如您所见,文本在服务器上的格式正确。我在客户端上渲染它是这样的:
<script type="application/ld+json" th:utext="${faqsJson}">
但 HTML 中的输出如下所示:
{
"@context" : "http://schema.org",
"@type" : "FAQPage",
"mainEntity" : [ {
"@type" : "Question",
"name" : "question text",
"acceptedAnswer" : {
"@type" : "Answer",
"text" : "answer text <a href="\"\""> answer text."
}
如您所见<a href
,没有正确转义。由于某种原因,它添加"
到 href 属性中并且不会转义双引号。
如何强制 Thymeleaf 完全按照它的样子输出字符串?
解决方案
要在 HTML 页面中显示文字文本,您可以使用th:inline="text"
- 并保留换行符,您还可以使用white-space: pre-wrap;
:
<div th:inline="text" style="white-space: pre-wrap;">[[${faqsJson}]]</div>
生成的 HTML 显示在浏览器页面上是这样的:
请参阅文本内联以供参考。
不要>
在 div 标签中的 和[
Thymeleaf 表达式的开头之间留下任何空白 - 否则它将成为 HTML 页面上的空白。
推荐阅读
- javascript - 多次添加时,Div 不起作用
- api - 逆向工程移动与 Web API
- json - JSON 标记 - SyntaxError: JSON Parse error: Unrecognized token '<'
- node.js - Apache ReverseProxy 不适用于 Node 和 SSL
- python - “张量流”没有属性“配置”
- output - 为什么for循环只输出第一个和最后一个条目
- java - 有没有办法使用 PythonInterpreter 将 Python 代码的输出值(如“print('python code')”)返回到字符串或其他对象中
- javascript - 在ajax请求中使用2个函数
- javascript - 在同一路由Angular上将数据从一个解析器传递到另一个解析器
- flutter - 在颤动中删除下拉底线