将带有 HTML 代码的字符串注入和解释到 JSF 页面的组件 [英] Component to inject and interpret String with HTML code into JSF page

查看:16
本文介绍了将带有 HTML 代码的字符串注入和解释到 JSF 页面的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 PrimeFaces 和 JSF 2.0 来构建一个应用程序.我正在使用 PrimeFaces 组件使用户能够创建富文本.但是这个组件的输出是 HTML 源代码,如下所示:

I'm using PrimeFaces with JSF 2.0 to build one application. I'm using PrimeFaces <p:editor> component to enable user to create rich text. But the output of this component is HTML source which look like this:

String text = "<p>This text <i>contains</i> some <b>HTML</b> code.</p>";

当我在 中显示此内容时,如下所示:

When I show this in a <h:outputText> as below:

<h:outputText value="#{bean.text}" />

然后它将 HTML 代码显示为纯文本:

Then it shows the HTML code as plain text:

<p>此文本<i>包含</i>一些 <b>HTML</b>代码.</p>

<p>This text <i>contains</i> some <b>HTML</b> code.</p>

是否有任何组件可以解释 HTML 源代码,例如 实际上显示为斜体, 显示为粗体?

Is there any component which can interpret the HTML source so that e.g. <i> is actually shown as italics and <b> as bold?

此文本包含一些HTML代码.

推荐答案

JSF 默认从支持 bean 属性中转义 HTML 以防止 XSS 攻击漏洞.要禁用此功能,只需将 escape 属性设置为 false.

JSF by default escapes HTML from backing bean properties in order to prevent XSS attack holes. To disable this, just set the escape attribute of the <h:outputText> to false.

<h:outputText ... escape="false" />

这样 HTML 就不会被转义,因此会被网络浏览器解释.

This way the HTML won't be escaped and will thus be interpreted by the webbrowser.

与具体问题无关,注意 XSS 攻击,因为您在这里基本上是重新显示未转义的用户控制输入.您可能想事先对其进行消毒.

Unrelated to the concrete problem, beware of XSS attacks as you're here basically redisplaying user-controlled input unescaped. You might want to sanitize it beforehand.

这篇关于将带有 HTML 代码的字符串注入和解释到 JSF 页面的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆