@ModelAttribute 和 org.springframework.ui.ModelMap.get() 之间的区别?
我正在尝试使用对象作为 <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
的命令对象>元素。
在控制器的 GET 方法中,我添加如下对象:
@RequestMapping(method = RequestMethod.GET)
public String renderForm(ModelMap model, HttpServletRequest request) {
[...]
model.addAttribute("voting", voting);
[...]
}
渲染的表单在定义如下时正确显示命令对象:
<form:form action="vote" method="PUT" commandName="voting" name="oform">
现在,当尝试在控制器中重新访问表单的命令对象时,在 POST 方法上,我有两种方法。首先,我在签名中声明 @ModelAttribute:
@RequestMapping(method = RequestMethod.PUT)
public String newVoting(@ModelAttribute("voting") Voting voting, HttpServletRequest request) { [...]}
或者我访问 ModelMap 并从底层映射中获取值:
@RequestMapping(method = RequestMethod.PUT)
public String newVoting(ModelMap model, HttpServletRequest request) {
Voting voting = (Voting) model.get("voting");
[...]
}
执行第一个操作时,我获取表单提交的对象。执行后者时,我得到了对象在被表单处理之前的样子。
为什么表单的提交不会更改 ModelMap 中的对象以及为什么 @ModelAttribute 与实际模型中的不同?我觉得名称“ModelAttribute”应该产生与直接从模型获取对象相同的对象。
也许 @ModelAttribute 有点误导?
编辑 忘记提及该对象是 @SessionAttributes
@SessionAttributes({"voting", "state"})
I'm trying to use an Object as the command object of a <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
element.
In the controller's GET method, I add the Object like this:
@RequestMapping(method = RequestMethod.GET)
public String renderForm(ModelMap model, HttpServletRequest request) {
[...]
model.addAttribute("voting", voting);
[...]
}
The rendered form does show the command object correctly when defined like this:
<form:form action="vote" method="PUT" commandName="voting" name="oform">
Now when trying to access the form's command object back in the controller, on the POST method, I have two approaches. First, I declare the @ModelAttribute in the signature:
@RequestMapping(method = RequestMethod.PUT)
public String newVoting(@ModelAttribute("voting") Voting voting, HttpServletRequest request) { [...]}
Or I access the ModelMap and get the value from the underlying map:
@RequestMapping(method = RequestMethod.PUT)
public String newVoting(ModelMap model, HttpServletRequest request) {
Voting voting = (Voting) model.get("voting");
[...]
}
When doing the first, I get the object as it was submitted by the form. Doing the latter, I get the object as it was BEFORE being handled by the form.
Why does the form's submit not change the object in the ModelMap and why does the @ModelAttribute differ from whats in the actual model? I feel like the name 'ModelAttribute' should result in the same object like getting the object directly from the model.
Maybe @ModelAttribute is kind of misleading?
EDIT
Forgot to mention that the object is a @SessionAttributes
@SessionAttributes({"voting", "state"})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的第一种方法,使用 @ModelAttribute 注释做两件事:
您的第二种方法仅使用 ModelMap,因此它仅获取模型属性(从请求或在您的情况下从会话获取,具体取决于 @SessionAttributes 注释)。此方法不使用提交表单中的数据。
Your first approach, using the @ModelAttribute annotation does two things:
Your second approach uses just the ModelMap, so it's getting only model attributes (from the request or, in your case, the session, depending on the @SessionAttributes annotation). This approach does not use the data from the submitted form.