我了解@ModelAttribute吗? [英] Did I understand @ModelAttribute right?

查看:75
本文介绍了我了解@ModelAttribute吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助从 Spring 文档中了解以下示例与 @ModelAttribute :(方法 populatePetTypes()

I need help with understanding following example with @ModelAttribute from Spring documentation: (method populatePetTypes())

@Controller
@RequestMapping("/owners/{ownerId}/pets/{petId}/edit")
@SessionAttributes("pet")
public class EditPetForm {

    // ...

    @ModelAttribute("types")
    public Collection<PetType> populatePetTypes() {
        return this.clinic.getPetTypes();
    }

    @RequestMapping(method = RequestMethod.POST)
    public String processSubmit(
            @ModelAttribute("pet") Pet pet,
            BindingResult result, SessionStatus status) {

        new PetValidator().validate(pet, result);
        if (result.hasErrors()) {
            return "petForm";
        }
        else {
            this.clinic.storePet(pet);
            status.setComplete();
            return "redirect:owner.do?ownerId=" + pet.getOwner().getId();
        }
    }

}

我是这就像我们的Model对象可以在整个当前控制器中获得的额外值一样。这是真的吗?

I am undestood this like additional "value" which our Model object could get in the whole current controller. Is it true?

我试图通过将代码添加到我的控制器来进行一些测试:

I am trying to do some test, by adding that code to my controller:

@ModelAttribute("user")
public Integer getKod(){
    return new Integer(123321);
}

另一种方法是:

@RequestMapping("/register")
public String register(Map<String, Object> map, @ModelAttribute("user") MyUser user, BindingResult result) {
...
}

现在我尝试只显示kod 进入我的表格:

and now I am try just show "kod" into my form:

<form:form method="post" action="" commandName="user">
(...)
        <form:label path="kod">kod</form:label>:
        <form:input path="kod"/>
(...)
</form:form>

但我得到了:

java.lang.IllegalArgumentException:java.lang.ClassCastException@229d7c57

请帮助!

推荐答案

注释意味着不同的东西取决于你把它放在哪里。

The annotation means different things depending on where you put it.

方法上的@ModelAttribute 告诉Spring将该方法的return vlaue放在Map下。它没有告诉它对该对象进行属性绑定。所以你要在Map中放置一个名为user的Integer。

@ModelAttribute on a method tells Spring to put the method's return vlaue into the Map, under that name. It is not telling it to do property binding on that object. So you're putting an Integer called "user" in the Map.

@ModelAttribute 参数告诉Spring 使用此名称在地图中查找内容,并将其指定给此方法参数。

@ModelAttribute on a parameter tells Spring "Find something in the map with this name, and assign it to this method parameter."

因此,您将整数放在地图下的user名称下,然后询问Spring分配给MyUser类型的Object。当然Integer不能被强制转换为MyUser!

So you put an Integer in the map under the name "user" then asked Spring to assign to an Object of type MyUser. Of course Integer cannot be cast to MyUser!

我认为你混淆了经典MVC意义上的'Model',这是你的MyUser,有Spring的ModelMap,是所有数据,这些数据可作为视图的一部分使用。当Spring控制器引用'Model'时,它表示与构建屏幕相关的所有事物的ModelMap。您的MyUser对象是该地图中的一个条目,但可能还有更多其他内容也是该屏幕的一部分。在spring-ese中,我们经常将你的MyUser称为'Form Backing Object',因为它是你表单的实际绑定目标。

I think you're confusing the 'Model' in the classic MVC sense, which is your MyUser, with Spring's ModelMap, which is all of the data that's available as part of the view. When the Spring controller refers to 'Model' it means the ModelMap of all things that are relevant to building the screen. Your MyUser object is one entry in that map, but there can be more other things that are also part of the screen in there. In spring-ese we would frequently refer to your MyUser as the 'Form Backing Object' because it's the actual binding target of your form.

例如,在你的代码片段中发布后,PetTypes的类型列表是Spring ModelMap的一部分。它是构建视图所需的参考数据。但它不是'Pet'对象的属性,它是表单支持对象。

For example, in the snippet you posted, the "types" list of PetTypes is part of the Spring ModelMap. It's reference data needed to build the view. But it's not an attribute of the 'Pet' object, which is the form backing object.

这篇关于我了解@ModelAttribute吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆