Arj*_*jun 2 spring thymeleaf spring-boot
我正在用百里香学习弹簧靴。我已经在文件系统中存储了用户对象的配置文件图像,只有图像的 URL 是数据库中持久化用户对象的一部分。
我已将name和添加dpurl到模型中,以便从模板访问它。
@GetMapping("/profile")
public String profileController(@AuthenticationPrincipal User activeUser, Model model) {
model.addAttribute("name", activeUser.getName());
model.addAttribute("dpurl", activeUser.getDpURL());
return "profile";
}
Run Code Online (Sandbox Code Playgroud)
我可以使用name模板访问th:text="${name}"
但是,我无法像这样使用百里香叶指定背景图像
<div id="dp-container" th:style="'background-image: url(' + @?{dpurl} + ');'" >
Run Code Online (Sandbox Code Playgroud)
这导致错误
Could not parse as expression: "'background-image: url(' + @?{dpurl} + ');'"
更新
像这样改变它
<div id="dp-container" th:style="'background-image:url(' + @{dpurl} + ');'">
Run Code Online (Sandbox Code Playgroud)
现在我没有收到任何错误,但背景图像仍未显示。
如何使用 Thymeleaf 指定背景图像?
它可能需要如下所示:
<div id="dp-container" th:style="'background-image:url(' + ${dpurl} + ');'">
Run Code Online (Sandbox Code Playgroud)
但这取决于dpurl包含的内容。你真的需要使用@{...}吗?那么你应该使用@{${dpurl}}.
要调试它,您应该查看源代码。它也会显示您的表达式正在解析的内容。
| 归档时间: |
|
| 查看次数: |
6146 次 |
| 最近记录: |