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

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

问题描述

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

  String text =< p> i>包含< / i>一些< b> HTML< / b>代码< / p>; 

当我在< h:outputText> 如下:

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

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


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


是否有任何组件可以解释HTML源, < i> 实际上以斜体显示,< b> 为粗体


此文本包含某些 HTML 代码。



解决方案

JSF默认从支持bean属性转义HTML,以防止XSS攻击漏洞。要禁用此功能,只需将< h:outputText> 转义属性设置为

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

这样HTML将不会被转义,因此将被webbrowser解释。 >




与具体问题无关,请注意XSS攻击,因为您在这里基本上重新显示用户控制输入未转义。您可能需要事先消毒。




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}" />

Then it shows the HTML code as plain text:

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

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

This text contains some HTML code.

解决方案

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" />

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


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天全站免登陆