【发布时间】:2021-04-20 15:10:36
【问题描述】:
我有一个使用 Springboot 和 Thymeleaf 模板编写的简单 Web 应用程序。报表控制器从表单接收数据并构建作为模型属性添加的 TestPlanReportResponse 对象,如下所示:
@PostMapping("/report")
public String homeSubmit(@ModelAttribute HomeFormInput homeFormInput, Model model, Errors errors) {
final TestPlanReportResponse response = new TestPlanReportResponse(homeFormInput);
model.addAttribute("allData", response);
return "charts";
}
我可以在“图表”百里香模板中使用该数据并显示我需要的数据,但我需要在单击按钮时将完全相同的对象发送回控制器,但我得到了 TestPlanReportResponse 对象作为参数并设置了空值。
@PostMapping("/report/send")
public String sendReport(@ModelAttribute TestPlanReportResponse reportData, Model model) {
//reportData contains just nulls
}
这是我的按钮在图表模板中的设置方式:
<form action="#" th:action="@{/report/send}" th:object="${allData}" method="post">
<button type="submit">Send the report</button>
</form>
所以我的问题是如何将对象从百里香模板发送回来?我是否应该创建一个隐藏的输入并将“allData”对象放在那里只是为了将其发回?在我看来,它就像肮脏的黑客。将数据传回的适当方法是什么?我想让这个应用程序无状态,所以不要将数据存储在服务器端。
【问题讨论】:
标签: spring-boot thymeleaf