简体   繁体   中英

Thymeleaf - How to display text not included in th:text

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?

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.

Try this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM