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


当前回答

对于我的风格,我总是使用@ModelAttribute从spring form jsp捕获对象。例如,我在jsp页面上设计表单,该表单存在commandName

<form:form commandName="Book" action="" methon="post">
      <form:input type="text" path="title"></form:input>
</form:form>

我在控制器上用跟随代码捕获对象

public String controllerPost(@ModelAttribute("Book") Book book)

book的每个字段名必须与form子元素中的path匹配

其他回答

这用于Spring MVC中的数据绑定目的。让你的jsp有一个表单元素,例如

在JSP

<form:form action="test-example" method="POST" commandName="testModelAttribute"> </form:form> .

(Spring Form方法,也可以使用Simple Form元素)

控制器端

@RequestMapping(value = "/test-example", method = RequestMethod.POST)
public ModelAndView testExample(@ModelAttribute("testModelAttribute") TestModel testModel, Map<String, Object> map,...) {

}

现在,当您提交表单时,表单字段值将对您可用。

@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”。

所以我会试着用更简单的方式来解释。让我们有:

public class Person {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }
}

正如Spring MVC文档中所描述的那样——@ModelAttribute注释可以用于方法或方法参数。当然我们可以在一个控制器中同时使用这两种方法。

1.方法注释

@ModelAttribute("cities")
 public List<String> checkOptions(){
 return new Arrays.asList(new[]{"Sofia","Pleven","Ruse"});//and so on
}

这种方法的目的是在模型中添加属性。因此,在我们的案例中,cities key将拥有列表new Arrays.asList(new[]{"Sofia","Pleven","Ruse"})作为模型中的值(您可以将模型看作map(key:value))。在同一个控制器中,在@RequestMapping方法之前调用控制器中的@ModelAttribute方法。

在这里,我们希望向Model添加将在表单中用于显示给用户的公共信息。例如,它可以用来填充HTML选择:

2.方法参数

public String findPerson(@ModelAttriute(value="person") Person person) {
    //..Some logic with person
    return "person.jsp";
}

An @ModelAttribute on a method argument indicates the argument should be retrieved from the model. So in this case we expect that we have in the Model person object as key and we want to get its value and put it to the method argument Person person. If such does not exists or (sometimes you misspell the (value="persson")) then Spring will not find it in the Model and will create empty Person object using its defaults. Then will take the request parameters and try to data bind them in the Person object using their names.

name="Dmitrij"&countries=Lesoto&sponsor.organization="SilkRoad"&authorizedFunds=&authorizedHours=&

我们有了name,它会使用setName(String name)绑定到person。name。因此,在

//..Some logic with person

我们可以访问这个值为“Dimitrij”的填充名称。

当然,Spring可以绑定更复杂的对象,比如List、map、List Of Sets Of Maps等等,但在幕后,它实现了数据绑定的魔力。

我们可以同时拥有带模型注释的方法和参数中带有@ModelAttribute的请求方法处理程序。那我们就得统一规则。 当然,我们有很多不同的情况——@ModelAttribute方法也可以在@ControllerAdvice中定义,等等……

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

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