【问题标题】:converting html into text将html转换为文本
【发布时间】:2011-04-01 09:46:42
【问题描述】:

我正在与 JSF 合作。我使用了 RichFaces 的“RichEditor”。我将此编辑器中的内容存储到 bean 中并显示为 JSF 表单。但它在 JSF 表单上显示 HTML 标记。为此,我使用了 JSoup HTML Parser。但是它将富编辑器的书面内容完全转换为简单的文本,删除了所有格式,如粗体、使用的按钮、换行符等。我需要以 jSF 形式作为编辑器显示。

请帮忙...

富编辑器代码

            <f:param name="theme_advanced_buttons1" value="
            newdocument,separator,copy,cut,paste,pasteword,undo,redo,separator,bold,italic,underline,
            strikethrough,forecolor,backcolor,separator,
            justifyleft,justifycenter,justifyright,justifyfull,outdent,indent " />

        <f:param name="theme_advanced_buttons2" value= "bullist,numlist,separator,
        insertdate,inserttime,separator,image,emotions,styleprops,fontselect,fontsizeselect,formatselect,search,replace"/>  

        <f:param name="theme_advanced_toolbar_location" value="top"/>                               
        <f:param name="theme_advanced_toolbar_align" value="left"/>
        <f:param name="theme_advanced_font_sizes" value="10px,12px,14px,16px,18px,20px,24px,32px,36px,42px,48px,60px,72px"/>
        <f:param name="theme_advanced_fonts" value="Andale Mono=andale mono,times;
            Arial=arial,helvetica,sans-serif;
            Arial Black=arial black,avant garde;
            Book Antiqua=book antiqua,palatino;
            Calibri=calibri;
            Comic Sans MS=comic sans ms,sans-serif;
            Courier New=courier new,courier;
            Georgia=georgia,palatino;
            Helvetica=helvetica;
            Impact=impact,chicago;
            Symbol=symbol;
            Tahoma=tahoma,arial,helvetica,sans-serif;
            Terminal=terminal,monaco;
            Times New Roman=times new roman,times;
            Trebuchet MS=trebuchet ms,geneva;
            Verdana=verdana,geneva;
            Webdings=webdings;
            Wingdings=wingdings,zapf dingbats"/>

        </rich:editor>   

来自 Java....

公共字符串 saveNotice() {

    System.out.println(html2text(editor));



    return "";

}


public  String html2text(String editor)
{
    String edit;


    edit=Jsoup.parse(editor).text();
    setEditor(edit);
    return edit;


}

【问题讨论】:

  • 您能给我们一个演示链接或浏览器中的一些页面源代码吗?这对需要帮助的人会更有帮助。

标签: jsf jsoup rich-text-editor


【解决方案1】:

当您使用&lt;h:outputText&gt; 重新显示它时,JSF 将转义它们以防止 XSS 攻击。您需要添加 escape="false" 以重新显示 HTML 纯文本(因此由网络浏览器解释)。

<h:outputText value="#{bean.html}" escape="false" />

但是,这仍然是一个潜在的 XSS 漏洞。由于您已经在使用Jsoup,您可以使用Jsoup#clean() 来保留一些基本的 HTML 标签并删除所有其他恶意标签。

public String sanitizeHtml(String html) {
    return Jsoup.clean(unsafe, Whitelist.basic());
}

Whitelist 是可自定义的。详情请参阅its javadoc

【讨论】:

    【解决方案2】:

    在您的源代码中缺少富编辑器的打开标记。根据editors homepage尝试添加viewMode参数。我认为它的价值必须是“视觉的”。

    【讨论】:

      猜你喜欢
      • 2012-04-12
      • 2014-09-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-15
      • 2018-03-17
      • 2012-06-21
      • 2013-03-30
      • 1970-01-01
      相关资源
      最近更新 更多