@ModelAttribute 和 org.springframework.ui.ModelMap.get() 之间的区别?

发布于 2024-11-15 07:27:55 字数 1308 浏览 0 评论 0原文

我正在尝试使用对象作为 <%@ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

此生挚爱伱 2024-11-22 07:27:55

您的第一种方法,使用 @ModelAttribute 注释做两件事

  1. 使用提交的表单数据创建投票对象;
  2. 通过将 Voting 对象添加到模型(请求,或者在您的情况下,由于 @SessionAttributes({"voting"}))而将其暴露给视图。

您的第二种方法仅使用 ModelMap,因此它仅获取模型属性(从请求或在您的情况下从会话获取,具体取决于 @SessionAttributes 注释)。此方法使用提交表单中的数据。

Your first approach, using the @ModelAttribute annotation does two things:

  1. Creating the Voting object using the submitted form data;
  2. Exposing the Voting object to the view by adding it to the model (request, or, in your case, session because of the @SessionAttributes({"voting"})).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文