简体   繁体   中英

How to print out text as HTML in Java

My apologies in advance: despite MUCH research, I'm not even sure how to construct the following question:

I'm working with Java/JBoss, and I've set up a class to read in an XML file, parse through it, and take the various nodeValues to build a string of HTML. Now all I need to do is print/insert that HTML into the DOM.

I was trying:

<div id='wrapper'>#{MyClass.methodThatReturnsAnHTMLString()}</div>

But the HTML simply appeared as escaped text on screen with source code as follows:

<div id='wrapper'>
    &lt;div class='item'&gt;content&lt;/div&gt;
</div>

Thoughts? Thanks.

Try to replace the

>#{MyClass.methodThatReturnsAnHTMLString()}

with

<c:out value="${MyClass.methodThatReturnsAnHTMLString}" escapeXml="false"/>

This happens when you use JSTl to print the out put like :

<c:out value="${body.content}" />

It basically escape HTML/XML content.

If you use script-let then it should work.

you can try this .

EDIT

The org.apache.commons.lang package provides a class called StringEscapeUtils that has methods that help to escape and unescape Strings for Java, Java Script, HTML, XML, and SQL. Given your scenario, I think this might prove useful to you. :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM