Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

jsf - h:outputText not rendering HTML from XML response in spite of escape="false"

I have the following code:

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

The result is:

<p><b>Location. </b> <br /> a

The string from #{bean.shortDescription} is being taken from an XML response that is escaped:

&lt;p&gt;&lt;b&gt;Location. &lt;/b&gt; &lt;br /&gt; a

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="&lt;p&gt;&lt;b&gt;Location. &lt;/b&gt; &lt;br /&gt; a" escape="false" />

Then the result is:

Location.
a

How can I properly render the HTML tags I get from the XML? I do not want to escape them.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 &amp;.

You need to unescape it beforehand. The Apache Commons Lang StringEscapeUtils may come handy.

String unescapedShortDescription = StringEscapeUtils.unescapeXml(shortDescription);

Then, you can use <h:outputText escape="false"> to print it as-is.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...