在 Thymeleaf 中显示文本

ben*_*_14 1 spring-mvc thymeleaf

我是 Thymeleaf 的新手。刚刚读过它,现在,我尝试在前端使用 Thymeleaf 显示一些文本,在后端从 Spring MVC 获取值。

文件 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>
Run Code Online (Sandbox Code Playgroud)

文件 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";
}
Run Code Online (Sandbox Code Playgroud)

我得到的是这样的:

Password Changed

Your password has been changed. Your account information is below. 
Username: [[${account.username}]] 
First Name: [[${account.firstName}]] 
Last Name: [[${account.surname}]] 
Run Code Online (Sandbox Code Playgroud)

Thymeleaf 没有转换为正确的值,很可能我在这里错过了一些非常非常基本的东西。

Cản*_*Văn 7

在这种情况下,您应该使用th:text. 例如,Username: <p th:text=${account.username}>Username will be rendered here</p>。请注意,p标签内的文本不会显示。

有关更多详细信息,请参见此处:标准百里香语法