在freemarker中将字符串输出为html

Kat*_*e C 5 java templates freemarker

因此,我们将html输入输出数据模型。我需要将其输出到freemarker模板中:

例:

[#assign value = model.value!]
${value}

value = '<p>This is <a href='somelink'>Some link</a></p>'
Run Code Online (Sandbox Code Playgroud)

我已经尝试过[#noescape],但是它抛出一个错误,说没有转义块。请参见FREEMARKER:避免转义HTML字符。此解决方案对我不起作用。

rat*_*lue 5

[#noescape]<#noescape>仅在[#escape]标签内使用时有效。您的数据可能与 HTML 编码一起存储。您需要让后端对 html 进行取消编码。

否则你需要做类似的事情......

${value?replace("&gt;", ">")?replace("&lt;", "<")}
Run Code Online (Sandbox Code Playgroud)

但这不是一个好方法,因为它不会捕获所有编码的值,也不应该在视图层中完成。