2

When I pass out HTML tags using Model Attribute like so it passes incorrectly leaving whats not desired


@Controller
String rating = "<i class="fa fa-star" aria-hidden="true"></i><i class="fa fa-star" aria-hidden="true"></i><i class="fa fa-star-half-o" aria-hidden="true"></i><i class="fa fa-star-o" aria-hidden="true"></i><i class="fa fa-star-o" aria-hidden="true"></i>"

model.addAttribute("rating", rating);

HTML Page
<span th:text="${rating}"></span>

Result is 
"<i class="fa fa-star" aria-hidden="true"></i><i class="fa fa-star" aria-hidden="true"></i><i class="fa fa-star" aria-hidden="true"></i><i class="fa fa-star-o" aria-hidden="true"></i><i class="fa fa-star-o" aria-hidden="true"></i>"

As we can see there are quotes around my desired string, which dont show up on other strings such as header, text or int's when passed. Seems to just show up for HTML, when its tags are present

When i pass in a single awesome font it appears the way it should, until you from inspect element hit "edit as HTML"

<span>&lt;i class="fa fa-star" aria-hidden="true"&gt;&lt;/i&gt;</span>

Thanks anyone who knows why or a way around

  • First try looking at this article http://stackoverflow.com/questions/3423262/what-is-modelattribute-in-spring-mvc – bugsiesegal Jul 25 '16 at 19:57
  • Ok Im gonna read it now. Also amended my post a bit –  Jul 25 '16 at 19:58
  • Have you tried `th:utext`? http://www.thymeleaf.org/doc/tutorials/2.1/usingthymeleaf.html#unescaped-text – Edd Jul 25 '16 at 20:39
  • Your a genius. Worked perfectly. Thanks so much man. Was impossible find solution ! Why not use this as a answer and ill rate it ! –  Jul 25 '16 at 22:46

1 Answers1

1

This is the default behaviour of the th:text attribute. If you want Thymeleaf to respect our XHTML tags and not escape them, you will have to use a different attribute: th:utext (for “unescaped text”);)

<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
This will output our message just like we wanted it:
<p>Welcome to our <b>fantastic</b> grocery store!</p>
AchillesVan
  • 4,156
  • 3
  • 35
  • 47
  • Thanks for response. That was the solution. Much appreciated your help –  Jul 26 '16 at 21:40