Spring MVC中@ModelAttribute的目的和用法是什么?
当前回答
在方法层面
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";
}
其他回答
以任何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";
}
希望这个图解解释对你有帮助!
@ModelAttribute可以用作方法参数/参数,也可以用于方法声明之前。 该注释的主要目的是将请求参数或表单字段绑定到模型对象
引用http://www.javabeat.net/modelattribute-spring-mvc/
在方法层面
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";
}
这用于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注释被用作Spring MVC Web应用程序的一部分,可以在两种场景中使用。
首先,它可以用于将数据注入到预jsp加载模型中。这在确保需要JSP来显示所有数据本身时特别有用。注入是通过将一个方法连接到模型来获得的。
其次,它可以用于从现有模型中读取数据,并将其分配给教练方法的参数。
具有一定https://dzone.com/articles/using-spring-mvc%E2%80%99s
推荐文章
- 在流中使用Java 8 foreach循环移动到下一项
- 访问限制:'Application'类型不是API(必需库rt.jar的限制)
- 用Java计算两个日期之间的天数
- 如何配置slf4j-simple
- 在Jar文件中运行类
- 带参数的可运行?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- 我可以在Java中设置enum起始值吗?
- Java中的回调函数
- c#和Java中的泛型有什么不同?和模板在c++ ?
- 在Java中,流相对于循环的优势是什么?
- Jersey在未找到InjectionManagerFactory时停止工作
- 在Java流是peek真的只是调试?
- Recyclerview不调用onCreateViewHolder
- 将JSON字符串转换为HashMap