Displaying text in Thymeleaf

12,608

Solution 1

In this case, you should use th:text. For example, Username: <p th:text=${account.username}>Username will be rendered here</p>. Note that, the text inside p tag won't be shown.

See here for more details: Standard thymeleaf syntax

Solution 2

If you want to get rid of the "awkward" Html tag you can do as follows.

<span th:text="${account.username}" th:remove="tag">userName</span>
Share:
12,608
bencampbell_14
Author by

bencampbell_14

Software Developer and a Master of Data Science student.

Updated on July 10, 2022

Comments

  • bencampbell_14
    bencampbell_14 almost 2 years

    I'm totally new in Thymeleaf. Just read about it earlier, now, I'm trying to display some text using Thymeleaf in the front end, getting the values from Spring MVC in back-end.

    File successPwd.html

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" lang="en">
    <head>
      <meta charset="UTF-8"/>
      <title>Password Change</title>
     </head>
    <body>
     <h2>Password Changed</h2>
     Your password has been changed. Your account information is below.
    
     Username: [[${account.username}]] <br/>
     First Name: [[${account.firstName}]] <br/>
     Last Name: [[${account.surname}]] <br/>
    </body>
    </html>
    

    File PasswordResetController.java

    @RequestMapping(value= "/user/new_password", method = RequestMethod.POST)
    public String saveNewPassword(@RequestParam(value="new_password",required=false) String password, @RequestParam(value="hash") String hash, Model model)
    
    {
        //some codes to check hash
        AccountInfo account = accountInfoService.getAccount(hash);
        model.addAttribute("account", account);
        return "/successPwd";
    }
    

    What I'm getting is like this:

    Password Changed
    
    Your password has been changed. Your account information is below. 
    Username: [[${account.username}]] 
    First Name: [[${account.firstName}]] 
    Last Name: [[${account.surname}]] 
    

    Thymeleaf is not converting to the proper values, most likely I missed something very very basic here.

  • bencampbell_14
    bencampbell_14 over 6 years
    Thanks! I figured it out after some trial and error, but it seems awkward that it needs to be on a <p> tag. Or maybe I just don't fully understand thymeleaf yet.