Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring mvc 如何从thymeleaf模板访问spring控制器对象?_Spring Mvc_Thymeleaf - Fatal编程技术网

Spring mvc 如何从thymeleaf模板访问spring控制器对象?

Spring mvc 如何从thymeleaf模板访问spring控制器对象?,spring-mvc,thymeleaf,Spring Mvc,Thymeleaf,我想从thymeleaf模板访问spring控制器对象 谢谢。在控制器中,您可以像这样向模型属性添加对象 @GetMapping("/") public String index(Model model){ model.addAttribute("person", new Person()); return "index"; } 然后在index.html中,可以将对象称为${person}。下面可以看到一个例子 &l

我想从thymeleaf模板访问spring控制器对象


谢谢。

在控制器中,您可以像这样向模型属性添加对象

@GetMapping("/")
public String index(Model model){
    model.addAttribute("person", new Person());
    return "index";
}
然后在
index.html
中,可以将对象称为
${person}
。下面可以看到一个例子

<p th:text="${person}}"></p>
此示例意味着此人的国家可以为空,例如给定国家的受欢迎程度


如果您有更多关于Thymeleaf的问题,请随时与我联系。

我想扩展@Prebiutsta的答案。 当您在模板中使用模型对象时(如
index.html
),您可以获得对象的属性,如下所示:

<label>Person Id: </label> <p th:text="${person.id}}"></p>
<label>Person Name: </label> <p th:text="${person.name}}"></p>
<label>Person Adress: </label> <p th:text="${person.adress}}"></p>
此示例意味着此人的国家可以为空,例如给定国家的受欢迎程度

如果您有更多关于Thymeleaf的问题,请随时与我联系

<label>Person Id: </label> <p th:text="${person.id}}"></p>
<label>Person Name: </label> <p th:text="${person.name}}"></p>
<label>Person Adress: </label> <p th:text="${person.adress}}"></p>
<p th:text="${person.country?.popularity?}}"></p>