10

How can I display a string that contains HTML tags in Thymeleaf?

So this piece of code:

<div th:each="content : ${cmsContent}"> <div class="panel-body" sec:authorize="hasRole('ROLE_ADMIN')"> <div th:switch="${content.reference}"> <div th:case="'home.admin'"> <p th:text="${content.text}"></p> </div> </div> </div> //More code.... 

And at this line of piece of code ${content.text} it literally generates this on the browser:

<p>test</p> 

But I want to show this instead on the browser:

test

1

1 Answer 1

27

You can use th:utext (unescaped text) for such scenarios.

Simply change

<p th:text="${content.text}"></p> 

to

<p th:utext="${content.text}"></p> 

I will suggest to also have a look into documentation here to know all about using Thymeleaf.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.