Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding HTML tags using th:text in thymeleaf

How can I add HTML tags into a th:text so the tags become visual?

An example of this would be the following:

A view variable called htmlcode gets injected with the value

<b>bla</b>

The view itself looks like

<div th:text="*{htmlcode}"><p>defaultcode</p></div>

Now I want the result to be something like

<div><b>bla</b></div>
like image 315
Kristof Avatar asked Dec 02 '25 05:12

Kristof


1 Answers

I was able to solve this issue by using th:utext instead of th:text. This way, the HTML tags are added unescaped. Obviously, this has to be used with caution, to prevent XSS attacks.

like image 179
Kristof Avatar answered Dec 03 '25 19:12

Kristof