【发布时间】:2021-01-19 05:15:57
【问题描述】:
我尝试使用模型对象进行简单的数据绑定,并使用 Thymeleaf 在 html 页面上显示值。 th:text 中的字符串文字显示正常,但是当我尝试显示属性的值时,我得到 null。
提前致谢。
package control;
import java.time.LocalDate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class Controls {
@GetMapping("/")
public String index(Model model) {
// Pass the date to the view
model.addAttribute("serverDate", LocalDate.now());
return "index";
}
}
<!DOCTYPE html>
<html xmlns:th="https://www.thymeleaf.org">
<head>
<title>Index Page</title>
</head>
<body>
<h1>Showing Date</h1>
<h1 style="color:green;" th:text="'Today is ' + ${serverDate}">Today is Someday.</h1>
</body>
</html>
【问题讨论】:
-
我唯一看到的是,有问题的 html 模板中的行对我来说似乎很奇怪。我认为
th:text属性将被指定为th:text="Today is ${serverDate}",没有单引号或“+”。但我猜这些角色,如果他们实际上没有做我不知道的事情,只会出现在最终输出中。我仍然不希望有null。 -
试试
th:text="${'Today is ' + serverDate}"
标签: spring-boot thymeleaf