如何在 freemarker 中渲染原始 html?
How to render raw html in freemarker?
我有这样的模板:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">
<p>server response: ${response}</p>
...
</table>
</body>
</html>
实际上服务器可以获得任何响应。
在某些情况下,服务器 returns html 和 freemarker 将其解析为 html,看起来很糟糕。
我想显示带有标签等的用户行 html,如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
我该怎么做?
开启 HTML 自动转义,这样 ${response}
就不会呈现为 HTML (这样你就可以避免 XSS 漏洞)。以 <#ftl output_format="HTML">
开头的文件将执行此操作。虽然推荐的方法是使用 ftlh
文件扩展名,然后将 incompatible_improvements
设置为至少 2.3.24,或者将 recognize_standard_file_extensions
设置为 true
.
如果你出于某种原因不想使用自动转义,你可以写成${response?html}
.
由于 white-space 不应该被浏览器折叠,您当然也想将该插值放入 HTML pre
或 textarea
,或者变成 div
和一些 class 有 white-space: pre-wrap
.
我有这样的模板:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<table align="center" border="0" cellpadding="0" cellspacing="0" width="800" style="border-collapse: collapse;">
<p>server response: ${response}</p>
...
</table>
</body>
</html>
实际上服务器可以获得任何响应。
在某些情况下,服务器 returns html 和 freemarker 将其解析为 html,看起来很糟糕。
我想显示带有标签等的用户行 html,如下所示:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not understand.</p>
我该怎么做?
开启 HTML 自动转义,这样 ${response}
就不会呈现为 HTML (这样你就可以避免 XSS 漏洞)。以 <#ftl output_format="HTML">
开头的文件将执行此操作。虽然推荐的方法是使用 ftlh
文件扩展名,然后将 incompatible_improvements
设置为至少 2.3.24,或者将 recognize_standard_file_extensions
设置为 true
.
如果你出于某种原因不想使用自动转义,你可以写成${response?html}
.
由于 white-space 不应该被浏览器折叠,您当然也想将该插值放入 HTML pre
或 textarea
,或者变成 div
和一些 class 有 white-space: pre-wrap
.