1

I would like to display the name of a user using thymeleaf. Here is the html tag that I'm using:

<h1 th:text="${username}"></h1> 

I want it to display this: Hello, User

Given username is a string "User". I tried something like this:

<h1 th:text="${username}">Hello, </h1> 

But it didn't work. How can I fix it?

1
  • 1
    Try <h1 th:text="|Hello, ${username}|"></h1> or <h1 th:text="'Hello, ' + ${username}"></h1> Commented Oct 22, 2021 at 13:55

2 Answers 2

4

I personally prefer to just use an additional tag.

<h1>Hello, <span th:text="${username}" /></h1> 

or literal substitution

<h1 th:text="|Hello, ${username}|"></h1> 

as quoting strings just adds more complexity.

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

Comments

2

Try this:

<h1 th:text="'Hello, ' + ${username}"></h1> 

Source: https://www.wimdeblauwe.com/blog/2021/01/11/string-concatenation-with-thymeleaf/

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.