【问题标题】:Thymeleaf th:text returns nullThymeleaf th:text 返回 null
【发布时间】: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


【解决方案1】:

对不起,我只是将 control.java 放在错误的包中,这就是它没有正确绑定的原因。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-31
    相关资源
    最近更新 更多