Halley
Halley

Reputation: 531

How to prevent escape of html tags in Spring MVC?

I have some data inside my database with html tags like

<b>, <br>

But, when I try to store it into a model object and render it on a JSP, it is rendered with the tags. The tags are not evaluated.

Any idea on how to get my page to process those tags? Below is the code I use in my JSP.

<div class="col-xs-6 form-group">
    <label>Comments</label>
    ${requestObject.comments}
</div>

Edit: I tried the below code also, with no luck.

<c:out escapeXml="false" value="${requestObject.comments}" />

Database Content

<b>Oh Snap</b>

HTML Source

&lt;b&gt;Oh Snap&lt;&#x2f;b&gt;

I am expecting my text to be rendered bold

Oh Snap

Upvotes: 4

Views: 7164

Answers (1)

riddle_me_this
riddle_me_this

Reputation: 9125

Not sure whether this is what you are seeking, but this should allow HTML to be evaluted:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<spring:htmlEscape defaultHtmlEscape="false" />

If you have Spring form or message tags, you can do: htmlEscape="false".

UPDATE: Sadly, the c:out syntax will take precedence over the Spring tag on this but you can also try:

<spring:escapeBody htmlEscape="false">
   <%=requestObject.comments%>
</spring:escapeBody>

Upvotes: 2

Related Questions