h:尽管逸出="false",但输出文本仍未从XML响应中呈现HTML. [英] h:outputText not rendering HTML from XML response in spite of escape="false"
问题描述
我有以下代码:
<h:outputText value="#{bean.shortDescription}" escape="false" />
结果是:
< p>< b>位置. </b>< br/> a
<p><b>Location. </b> <br /> a
#{bean.shortDescription}
中的字符串是从转义的XML响应中获取的:
The string from #{bean.shortDescription}
is being taken from an XML response that is escaped:
<p><b>Location. </b> <br /> a
如果我做出与上述相同的输出文本,但是我没有从XML中获取响应,而是仅放置了来自响应的转义字符串,例如:
If I make the same output text as above, but instead of taking the response from the XML, I just put the escaped string that comes from the response, e.g.:
<h:outputText value="<p><b>Location. </b> <br /> a" escape="false" />
那么结果是:
位置.
a
Location.
a
如何正确呈现从XML中获得的HTML标签?我不想逃脱他们.
How can I properly render the HTML tags I get from the XML? I do not want to escape them.
推荐答案
这是因为您的文本已已转义. escape="false"
不会 unescape 文本.它只是按原样打印文本,而不是转义文本.但是,由于您的文本从一开始就已经被转义了,所以它以转义的形式出现.删除escape="false"
,您将看到它会被两次转义. IE.每个&
都变为&
.
It's because your text is already escaped. The escape="false"
doesn't unescape text. It just prints the text as-is instead of escaping it. However, as your text is already escaped from beginning on, it appears in escaped form. Remove the escape="false"
and you'll see that it will be double-escaped. I.e. every &
becomes &
.
您需要预先取消转义. Apache Commons Lang StringEscapeUtils
可能会派上用场.
You need to unescape it beforehand. The Apache Commons Lang StringEscapeUtils
may come handy.
String unescapedShortDescription = StringEscapeUtils.unescapeXml(shortDescription);
然后,您可以使用<h:outputText escape="false">
原样打印.
Then, you can use <h:outputText escape="false">
to print it as-is.
这篇关于h:尽管逸出="false",但输出文本仍未从XML响应中呈现HTML.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!