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

4
  • First try looking at this article stackoverflow.com/questions/3423262/… Commented Jul 25, 2016 at 19:57
  • Ok Im gonna read it now. Also amended my post a bit Commented Jul 25, 2016 at 19:58
  • Have you tried th:utext? thymeleaf.org/doc/tutorials/2.1/… Commented Jul 25, 2016 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 ! Commented Jul 25, 2016 at 22:46

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for response. That was the solution. Much appreciated your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.