Spring MVC中@ModelAttribute的目的和用法是什么?


当前回答

@ModelAttribute将使用您指定的名称(@ModelAttribute(“Testing”)Test Test)创建一个属性,在给定的示例中作为Testing,Test是bean测试,是bean的引用,而Testing将在模型中可用,以便您可以进一步在jsp页面上使用它来检索存储在ModelAttribute中的值。

其他回答

@ModelAttribute引用Model对象的属性(MVC中的M;) 假设我们有一个表单,它有一个名为Person的表单支持对象 然后你可以让Spring MVC通过使用@ModelAttribute注释将这个对象提供给Controller方法:

public String processForm(@ModelAttribute("person") Person person){
    person.getStuff();
}

另一方面,注释用于定义应该成为Model一部分的对象。 所以如果你想在Model中引用Person对象,你可以使用下面的方法:

@ModelAttribute("person")
public Person getPerson(){
    return new Person();
}

这个带注释的方法将允许访问视图中的Person对象,因为Spring会自动将它添加到模型中。

参见“使用@ModelAttribute”。

我知道这是一个老帖子,但我想我把我的帽子扔在戒指里,看看我是否能把水弄浑一点:)

我发现我最初难以理解@ModelAttribute是因为Spring决定将几个注释合并为一个注释。当我把它分成几个更小的注释时,它变得更清晰了:

对于参数注释,可以将@ModelAttribute看作是@Autowired + @Qualifier的等量物,即它试图从Spring托管模型中检索具有给定名称的bean。如果没有找到命名bean,它不会抛出错误或返回null,而是隐式地扮演@Bean的角色,即使用默认构造函数创建一个新实例,并将bean添加到模型中。

对于方法注释,可以认为@ModelAttribute相当于@Bean + @Before,也就是说,它将由用户代码构建的bean放在模型中,并且总是在请求处理方法之前调用它。

比方说,我看到@ModelAttribute如下所示(请不要从字面上理解!!):

@Bean("person")
@Before
public Person createPerson(){
  return new Person();
}

@RequestMapping(...)
public xxx handlePersonRequest( (@Autowired @Qualifier("person") | @Bean("person")) Person person, xxx){
  ...
}

如您所见,Spring做出了正确的决定,使@ModelAttribute成为一个包罗万象的注释;没有人想看到注释的大杂烩。

以任何web应用程序为例,无论是Gmail还是Facebook或Instagram或任何其他web应用程序,都是关于最终用户和应用程序或UI和后端应用程序之间交换数据或信息。即使在Spring MVC世界中,也有两种交换数据的方式:

从控制器到UI,以及 从UI到控制器。

这里我们感兴趣的是数据如何从UI传递到Controller。这也可以通过两种方式来实现:

使用HTML表单 使用查询参数。

使用HTML表单: 考虑下面的场景,

当我们从web浏览器提交表单数据时,我们可以在Controller类中作为对象访问该数据。当我们提交一个HTML表单时,Spring容器做了四件事。它将,

first read all the data that is submitted that comes in the request using the request.getParameter method. once it reads them, it will convert them into the appropriate Java type using integer.parseInt, double.parseDouble and all the other parse methods that are available based on the data type of the data. once parsed, it will create a object of the model class that we created. For example, in this scenario, it is the user information that is being submitted and we create a class called User, which the Container will create an object of and it will set all the values that come in automatically into that object. it will then handover that object by setting the values to the Controller.

为了让整个计划顺利进行,我们必须遵循一定的步骤。

We first need to define a model class, like User, in which the number of fields should exactly match the number of fields in the HTML form. Also, the names that we use in the HTML form should match the names that we have in the Java class. These two are very important. Names should match, the number of fields in the form should match the number of fields in the class that we create. Once we do that, the Container will automatically read the data that comes in, creates an object of this model, sets the values and it hands it over to the Controller. To read those values inside the Controller, we use the @ModelAttribute annotation on the method parameters. When we create methods in the Controller, we are going to use the @ModelAttribute and add a parameter to it which will automatically have this object given by the Container.

下面是一个注册用户的示例代码:

@RequestMapping(value = "registerUser", method = RequestMethod.POST)
public String registerUser(@ModelAttribute("user") User user, ModelMap model) {
    model.addAttribute("user", user);
    return "regResult";
}

希望这个图解解释对你有帮助!

将方法参数或方法返回值绑定到已命名模型属性的注释,暴露在web视图中。

public String add(@ModelAttribute("specified") Model model) {
    ...
}

@ModelAttribute只是将jsp字段的值绑定到Pojo类,以执行控制器类中的逻辑。如果您熟悉struts,那么这就像在提交时填充formbean对象。