【发布时间】:2010-03-17 15:30:36
【问题描述】:
我有一个类似这样的数据模型:
public class Report {
// report owner
private User user;
... typical getter setter ...
}
public class User {
... omitted for clarity
}
在创建报表时会发生什么情况,当前用户被设置为报表用户对象。编辑报告时,处理 POST 请求的弹簧控制器正在接收用户对象为空的报告。这是我的控制器的样子:
@Controller
@RequestMapping("/report")
public class ReportController {
@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
public String editReport(@PathVariable Long id, Model model) {
Report r = backend.getReport(id); // fully loads object
model.addAttribute("report", report);
return "report/edit";
}
@RequestMapping(value = "/edit/{id}", method = RequestMethod.POST)
public String process(@ModelAttribute("report") Report r) {
backend.save(r);
return "redirect:/report/show" + r.getId();
}
}
我运行的东西抛出了调试器,看起来在 editReport 方法中模型对象正在存储完全加载的报告对象(我可以在报告中看到用户)。在表单 jsp 上,我可以执行以下操作:
${report.user.username}
并呈现正确的结果。但是,当我在 process 方法中查看调试器时,传入的 Report r 有一个 null 用户。我不需要做任何特殊的数据绑定来确保信息被保留吗?
【问题讨论】:
-
spring 是否只是从表单中包含的值实例化一个新对象?因此,丢失任何未通过隐藏标签嵌入表单的信息?还是我没有正确设置。
标签: java forms spring-mvc