Spring MVC中@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”。
其他回答
以任何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) {
...
}
在方法层面
1.当注释在方法级别上使用时,它表明了它的目的 方法是添加一个或多个模型属性
@ModelAttribute
public void addAttributes(Model model) {
model.addAttribute("india", "india");
}
在方法参数处 1. 当用作方法参数时,它表示应该从模型检索参数。当不存在时,应该首先实例化,然后添加到模型中,一旦出现在模型中,参数字段应该从所有具有匹配名称的请求参数中填充。因此,它将表单数据绑定到bean。
@RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
public String submit(@ModelAttribute("employee") Employee employee) {
return "employeeView";
}
首先,模型在MVC Spring (MVC =模型,视图,控制器)中使用。也就是说,模型与“视图”一起使用。
这些观点是什么?视图可以被认为是“由我们的后端框架(在我们的例子中是Spring)在html页面的某些部分生成的一些可变数据的html页面”。
所以我们有了模型,它是一个包含数据的实体,被“注入”到视图中。
您可以使用Spring使用几个“视图”库:其中包括JSP、Thymeleaf、Mustache等。
例如,让我们假设我们正在使用Thymeleaf(它们都是相似的。更重要的是,除了JSP, Spring甚至不知道他正在使用哪些视图库。所有模型都是通过Spring的Servlet提供服务的。这意味着对于所有这些视图库,Spring代码都是相同的。你唯一需要改变的是这些html页面的语法,它们位于resources/static/templates中)
resources/static/templates //All our view web pages are here
控制器负责路由。例如,我们的站点托管在localhost:8080上。我们需要一个显示学生的路径(URL)。让我们假设这在localhost:8080/students上可用。做这个的控制器是StudentController:
@Controller //Not @RestController
public class StudentController {
@GetMapping(/students)
public String getStudentView() {
return "student";
}
}
这段代码的作用是说,如果我们要
localhost: 8080 /学生
然后调用getStudentView()方法。但是注意它应该返回一个字符串。然而,当使用视图库时,控制器被@Controller(而不是@RestController)注释,spring所做的是寻找一个带有方法返回的String名称的html视图页面,在我们的例子中,它将在
/ /静态/模板/ student.html资源
这对于没有数据的静态页面来说已经足够好了。然而,如果我们需要一个带有一些数据的动态页面,Spring提供了另一个很大的优势:getStudentView()上面的方法,也会在底层传递一个模型给我们的视图“student.html”。我们的模型将包含可以使用视图库中的特定语法在“student.html”文件中访问的数据。例如,用百里香叶:
<div th:text="${attribute1}"> </div>
这将访问模型的属性“attribute1”。 我们可以通过模型传递不同的数据。这是通过给它分配各种属性来实现的。有不同的方式分配属性,使用@ModelAttribute:
@Controller //Not @RestController
public class StudentController {
@ModelAttribute(name = "attribute1")
public int assignAttribute1() {
return 123454321
} // Think it as "model.attribute1 = 123454321"
@GetMapping(/students)
public String getStudentView() {
return "student";
}
}
上面的代码将为模型(在底层创建)分配一个名为“attribute1”的属性(将其视为一个键),值为12354321。比如“模特”。Attribute1 = 123454321”。
最后,当我们访问url时,模型被传递给视图
localhost: 8080 /学生
注意:所有用@ModelAttribute标注的方法都是在视图返回之前调用的。一旦创建了所有属性,模型就会传递给我们的视图。简单地说,在getStudentView()方法被调用之后,所有带有@ModelAttribute的方法都被调用。
也就是说,上面写的html代码将从浏览器中被视为:
<div> 123454321 </div> // th:text is a command of
//THymeleaf, and says to substitute the text
// between the tags with the attribute "attribute1"
// of our model passed to this view.
这是@ModelAttribute的基本用法。
还有另一个重要的用例:
模型可能需要在相反的方向:即,从视图到控制器。在上面描述的情况下,模型从控制器传递到视图。但是,假设用户从我们的html页面发回一些数据。我们可以用模型属性@ModelAttribute捕获它。这已经被其他人描述过了
@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象
引用http://www.javabeat.net/modelattribute-spring-mvc/
推荐文章
- 如何格式化Joda-Time DateTime仅为mm/dd/yyyy?
- 如何在POM.xml中引用环境变量?
- 如何在android中复制一个文件?
- 将整数转换为字符串,以逗号表示千
- 接口方法的最终参数-有什么意义?
- Java中的@UniqueConstraint注释
- 如何在清洁模式下运行eclipse ?如果我们这样做会发生什么?
- 获取java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory异常
- Java中的正则表达式命名组
- c#和Java的主要区别是什么?
- 什么是NullPointerException,我如何修复它?
- 在Java中使用“final”修饰符
- 无法在Flutter上找到捆绑的Java版本
- 如何在Kotlin解析JSON ?
- 如何在新的材质主题中改变背面箭头的颜色?