2

I am creating an application using Spring4/JPA/JSoup/Ckeditor.

User logins->opens a page->enters text using ckeditor->publishes it (kind of a small blog).

Data is saved successfully in mysql database, so for example:

  1. User enters a bold text in ckeditor and it gets saved as

<p><strong>Bold</p></strong>
  1. But when I launch the view page(jsp) this data appears along with html tags as it is from the database, like this :-

<p><strong>Bold</p></strong>

For correct behaviour it should have displayed like - Bold

When i view source of jsp page, html saved in database appears there as below Output from

&lt;p&gt;&lt;strong&gt;Bold&lt;/strong&gt;&lt;/p&gt;

Could you please help me out in this, I am not able to find where the conversion of <> to

&lt; &gt;

is taking place?

Thanks

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
user3367569
  • 103
  • 1
  • 1
  • 9

2 Answers2

3

Adding below code in jsp worked

<c:out escapeXml="false" value="${ticket.body}" />
Stephan
  • 41,764
  • 65
  • 238
  • 329
user3367569
  • 103
  • 1
  • 1
  • 9
1

Your problem is that these data are with escaped HTML. Most common solution is using of apache-common-lang library. Then you only unescape HTML by this:

StringEscapeUtils.unescapeHtml(retrivedDataFromDatabase)

For this you have to import Apache Common:

import org.apache.commons.lang.StringEscapeUtils;

You can download it and add it to your libraries from this link.

Here is JavaDoc for StringEscpaeUtils.


EDIT: And check if you register encodingFilter in web.xml:

<filter>  
    <filter-name>encodingFilter</filter-name>  
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
    <init-param>  
       <param-name>encoding</param-name>  
       <param-value>UTF-8</param-value>  
    </init-param>  
    <init-param>  
       <param-name>forceEncoding</param-name>  
       <param-value>true</param-value>  
    </init-param>  
</filter>  
<filter-mapping>  
    <filter-name>encodingFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping> 

ANOTHER SOLUTION: (and final solution by @user3367569 - owner of question)

Insert into JSP: <c:out escapeXml="false" value="${ticket.body}" />

Hrabosch
  • 1,541
  • 8
  • 12
  • I tried it as below `@Lob @XmlElement @JsonProperty public String getBody() { body = StringEscapeUtils.unescapeHtml4(body); return body; } public void setBody(String body) { String cleanHTML = Jsoup.clean(body, Whitelist.basic()); this.body = cleanHTML; }` Still not working, am i missing something here? – user3367569 Jul 30 '16 at 09:33
  • And you still have a escaped HTML tags? – Hrabosch Jul 30 '16 at 09:35
  • Yes, still escaped html tags – user3367569 Jul 30 '16 at 09:36
  • And output is the same in source code? I know it is still escaped, but if there are no changes, it is strange. Or I am missing something ... :/ – Hrabosch Jul 30 '16 at 09:42
  • Yes, output is same as before with &lt and &gt tags, i confirmed that html code is returned from controller in the correct way, its somewhere after that its changing,,, not sure if it is some encoding related issue or something else... Could you please point me in a direction to debug it? – user3367569 Jul 30 '16 at 09:56
  • Maybe there is problem in client. Check my update in answer and let me know – Hrabosch Jul 30 '16 at 10:00
  • Adding '' in jsp worked – user3367569 Jul 30 '16 at 18:04
  • Nice! I saw this somewhere as another solution too yesterday (somewhere on stackoverflow). – Hrabosch Jul 31 '16 at 07:08