You can use nl2br:text="${sample}" attribute, which firstly escapes the HTML special characters in the input text, then inserts <br /> tags before newlines.
Background
I wanted to show a multiple lines input with <br /> tag for the newlines. There’re several ways to realize it: using th:utext after converting newlines to <br />, or having lines as list then using th:block with th:each. But I don’t want to allow other html tags, nor to write th:block with th:each many times. Finally, I decided to create it.
Usage
Maven
<dependency> <groupId>com.github.bufferings</groupId> <artifactId>thymeleaf-extras-nl2br</artifactId> <version>1.0.0</version> </dependency>
Code
templateEngine.addDialect(new Nl2brDialect());
or if your application is Spring Boot application, you can add the dialect like this:
@Bean
public Nl2brDialect dialect() {
return new Nl2brDialect();
}
Template
Then you can use nl2br:text attribute like this:
<p nl2br:text="${sample}">Hello!</p>
If you want to stop your IDE’s warning, you can add xmlns attribute like this:
<html lang="en" xmlns="http://www.w3.org/1999/html"
xmlns:th="http://www.thymeleaf.org"
xmlns:nl2br="https://github.com/bufferings/thymeleaf-extras-nl2br">
Example Project

hope this serves someone :)