简体   繁体   中英

Problem with HTML output in JSP

I have code which generates some HTML, but when I try to output this content in a jsp all '<' are replaced with ' &lt; ' and all '>' with ' &gt; '. Here is the piece which renders the result:

<c:out value="${data}"/>

Can someone please explain what causes the character replacement, and how it can be avoided?

PS: I've tried escapeXml='false' property of <c:out/> tag, nothing at all is displayed.

Thanks, Natasha

What do you want the generated HTML to do, do you want it to be markup used by the browser or do you want it shown on the final page, for example as sample code visible to the user? Without escapeXml='false' it will be output to the browser as HTML and interpreted along with all the other markup. With escapeXml='true' it will be turned into escaped markup which is rendered visible to the end-user. So it all depends on what you're trying to do.

If you want to have < and > characters visible to the end user then they have to appear as &lt; and &gt; in the markup.

Nick

The out tag behaves as it should - if you do wish to output the literal value you can directly insert the EL expressen ${data} in your text. That is, in stead of

Some content <c:out value="${data}"/> some more content

you would use

Some content ${data} some more content 

in your JSP.

Before you get angry with c:out, please consider that the output often is something a user has put in there - potentially with some unwanted code. Imagine using ${data} on StackOverflow in stead of the (C# version of) c:out :-)

在c:out标记上有一个escapeXml属性,默认情况下将其设置为true,将其设置为false,并且不会进行转义,即HTML将在浏览器中输出。

<c:out value="${data}"/> escapes HTML characters while ${data} does not. Take a look at this related question .

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