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


当前回答

这用于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引用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 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匹配

我知道我来晚了,但我要引用他们的话, “迟到总比不到好”。让我们开始吧, 每个人都有自己解释事情的方式,让我试着总结一下,用一个例子用几个步骤为你简单解释一下; 假设你有一个简单的表单form.jsp:

<form:form action="processForm" modelAttribute="student">
  First Name : <form:input path="firstName" /> 
  <br/><br/>
  Last Name : <form:input path="lastName" />
  <br/><br/>
  <input type="submit" value="submit"/>
</form:form>

<form:input path="firstName" /> <form:input path="lastName" />这些是Student类中的字段/属性。当表单被调用/初始化时,它们的getter被调用。在表单提交时,它们的setter被调用,它们的值在表单标记中用modelAttribute="student"指示的bean中传输。

我们有StudentController,它包含以下方法:

@RequestMapping("/showForm")
// `Model` is used to pass data between controllers and views
public String showForm(Model theModel) {
    // attribute name, value
    theModel.addAttribute("student", new Student());
    return "form";
}
   
@RequestMapping("/processForm")
public String processForm(@ModelAttribute("student") Student theStudent) {
    System.out.println("theStudent :"+ theStudent.getLastName());
    return "form-details";
}

//@ModelAttribute("student") Student theStudent
//Spring automatically populates the object data with form data 
//all behind the scenes

现在我们终于有了一个form-details.jsp:

<b>Student Information</b>
${student.firstName}
${student.lastName}

回到这个问题,Spring MVC中的@ModelAttribute是什么? 一个来自源代码的示例定义, http://www.baeldung.com/spring-mvc-and-the-modelattribute-annotation @ModelAttribute是一个注释,它将一个方法参数或方法返回值绑定到一个命名的模型属性,然后将它公开给一个web视图。

实际发生的是,它获取表单的所有值那些由它提交的值,然后保存它们供你绑定或赋值给对象。它的工作方式类似于@RequestParameter,我们只获得一个参数并将值分配给某个方法参数。 区别在于@ModelAttribute保存所有表单数据,而不是单个参数。它为您创建一个bean,其中包含表单中提交的数据。

概括一下整件事:

步骤1: 请求被发送,我们的方法showForm()运行,一个模型,一个临时bean,被设置为student并转发给表单: 这个模型。addAttribute("student", new student ());

步骤2: 表单属性modelAttribute="student"定义了在表单提交时,模型将更新学生并保存表单的所有参数。

步骤3: 在表单提交时,使用参数@ModelAttribute("student")调用processForm()方法:使用modelAttribute="student"的表单中保存的值被提取并分配给student对象中的字段。

步骤4: 然后我们使用它,因为我们出价,就像在页面上显示它等,就像我做的

我希望它能帮助你理解这个概念。谢谢

@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象

引用http://www.javabeat.net/modelattribute-spring-mvc/

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