【发布时间】:2019-12-05 20:51:40
【问题描述】:
我有一个 DTO,其中包含一组另一个 DTO。将其传递到百里香模板是没有问题的。但是,我如何从模板中检索它?
现在 set 属性始终为空
DTO:
public class Partner{
@Id
Long id;
Set<Account> accounts;
}
public class Account{
@Id
Long id;
String name;
}
模板:
<form method="POST" th:object="${partner}" th:action="@{/postmystuff}">
<div th:each="acc : *{accounts}">
<input type="text" th:field="${acc}" />
</div>
<button type="submit">send</button>
</form>
控制器:
@Controller
public class SomeController{
//...
@GetMapping("/")
public String getMySite(Model m){
Partner p = new Partner(accounts); //accounts is a set of 10 accounts
m.addAttribute("partner", p);
return "mytemplate";
}
@PostMapping("/postmystuff")
public String postMyStuff(@ModelAttribute Partner p){
System.out.println(p); //for now we just print
return "redirect:/";
}
}
提交后,它会打印对象的字符串表示,但所有属性都是空的。
【问题讨论】:
标签: java spring spring-mvc thymeleaf