首页 文章

将html转换为文本

提问于
浏览
0

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

请帮忙...

Rich Editor的CODE

<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 ....

public String saveNotice(){

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



    return "";

}


public  String html2text(String editor)
{
    String edit;


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


}

2 回答

  • 0

    在您的源代码中,缺少丰富编辑器的开放标记 . 根据editors homepage尝试添加viewMode参数 . 我认为它的 Value 必须是'visual' .

  • 1

    当您使用 <h:outputText> 重新显示它时,JSF将逃脱它们以防止XSS攻击 . 您需要添加 escape="false" 以重新显示HTML平面(因此由webbrowser解释) .

    <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 .

相关问题