4

I'm using Thymeleaf in my Spring Boot project.

I've got a text string which contains some HTML tags and my goal is to print it without styling as well as without any HTML tags.

Using:

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

I've got something like this:

<b> text </b>

And that's ok because the value of myString text string is

String myString = "<b> text </b>"; 

So trying to do it in other way, using:

<p th:utext="${myString}"> </p>

I've got something like this:

text

But my goal ist to get non-formatted plain text like this:

text 

Simply text without any HTML tags and without rendering the HTML code.

How can I acheive this with using Thymeleaf only?

I've tried some th:remove="tags" along with th:inline="text" but It doesn't work so far.

Thanks in advance

2
  • To be clear, you have a String "<b> text </b>" and you want it to evaluate it to text? Commented Jun 27, 2017 at 1:55
  • Exactly. Just text not text. Commented Jun 27, 2017 at 7:39

3 Answers 3

8

Thymeleaf doesn't have any native support for this, so you're going to have to find your favorite library for removing tags use it instead. In this example, I used Jsoup.

After adding it to your pom file, something like this should work:

<div th:text="${T(org.jsoup.Jsoup).parse(myString).text()}" />

You could also create your own dialect that does it automatically, but that's more complicated. Then you could use your own attributes:

<div bohdan::removehtml="${myString}" />
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! It's perfect.
1

Use utext Example:

<p th:utext="${myString}"> </p>

Comments

0

You use th:block in Thymeleaf. it is only thymeleaf sentence and don`t rendering html code.

Sample code

<th:block th:text="${message}"></th:block>

1 Comment

Thanks for your answer but it's not what I was looking for. Using th:block the result is still <b> text </b>

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.