在 <h:outputText escape=“false"> 中显示来自 DB 的 HTML导致 HTML 页面损坏 [英] Displaying HTML from DB in <h:outputText escape="false"> results in broken HTML page

查看:16
本文介绍了在 <h:outputText escape=“false"> 中显示来自 DB 的 HTML导致 HTML 页面损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以设置 Firefox 和 Chrome 以使用 h:outputText 标签中的 escape=false 属性.当有一些 html 需要在浏览器中显示时,Firefox 和 Chrome 会正确显示解析后的字符串,但应用程序中的任何其他链接都被冻结 (??).

Is there any way to setup Firefox and Chrome to work with escape=false attribute in h:outputText tag. When there is some html that needs to be shown in the browser, Firefox and Chrome show parsed string correctly, but any other links in application are freezed (??).

来自db的示例html:

The example html from db:

<HEAD>
<BASE href="http://"><META content="text/html; charset=utf-8" http-equiv=Content-Type>          
<LINK rel=stylesheet type=text/css href=""><META name=GENERATOR content="MSHTML 9.00.8112.16434">
</HEAD>
<BODY><FONT color=#000000 size=2 face="Segoe UI">läuft nicht</FONT></BODY>

页面上解析的 HTML:

Parsed HTML on the page:

无所谓

很奇怪的是,在 IE 中一切正常(通常是相反的).

What is very weird, is that in IE everything works (usually it is opposite).

我使用 primefaces 组件 (v2.2)、.xhtml、tomcat 7 和 JSF 2.0

I use primefaces components (v2.2), .xhtml, tomcat 7 and JSF 2.0

推荐答案

你最终在语法上无效 HTML这样:

You end up with syntactically invalid HTML this way:

<html>
    <head></head>
    <body>
        <head></head>
        <body>...</body>
    </body>
</html>

这是不对的.只能有一个 .浏览器的行为未指定.您需要从该 HTML 中删除 整个 和包装 以便您最终得到 只有

This is not right. There can be only one <head> and <body>. The browsers will behave unspecified. You need to remove the entire <head> and the wrapping <body> from that HTML so that you end up with only

<FONT color=#000000 size=2 face="Segoe UI">läuft nicht</FONT>

您需要更新数据库以删除不必要的 HTML,或者使用 Jsoup 来解析这部分内容每个请求的基础如下所示:

You'd need to either update the DB to remove unnecessary HTML, or to use Jsoup to parse this piece out on a per-request basis something like as follows:

String bodyContent = Jsoup.parse(htmlFromDB).body().html();
// ...

或者,您也可以在 servlet 的帮助下将其显示在 HTML