Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what do the terms formBackingObject and ModelAttribute mean in Spring MVC?

I guess I don't understand the difference between a "FormBackingObject" and a "ModelAttribute" in spring MVC.

Seems like both gets initiated and populated by Spring with an incoming request.

like image 730
user198530 Avatar asked Oct 29 '09 00:10

user198530


People also ask

What is ModelAttribute in Spring?

@ModelAttribute is an annotation that binds a method parameter or method return value to a named model attribute, and then exposes it to a web view. In this tutorial, we'll demonstrate the usability and functionality of this annotation through a common concept, a form submitted from a company's employee.

What is difference between @RequestBody and @ModelAttribute?

@ModelAttribute is used for binding data from request param (in key value pairs), but @RequestBody is used for binding data from whole body of the request like POST,PUT.. request types which contains other format like json, xml.

What is formBackingObject?

formBackingObject. protected Object formBackingObject(HttpServletRequest request) throws Exception. Retrieve a backing object for the current form from the given request. The properties of the form object will correspond to the form field values in your form view.

Why do we use @ModelAttribute?

@ModelAttribute can be used to expose command objects to a web view, using specific attribute names, by annotating corresponding parameters of an @RequestMapping method.


1 Answers

Good question. This comes down to a difference in terminology between Spring 2.0 MVC, which used a controller class hierarchy, and Spring 2.5 MVC, which uses annotations.

The "form backing object" is the object which the Spring 2.0-style AbstractFormController (and subclasses like SimpleFormController) would use to bind the form data on to.

@ModelAttribute does much the same thing for Spring 2.5-style annotated controllers, but in a less rigid way.

So you're right in that the two do pretty much the same thing, but the style is really quite different. Both are valid approaches, and both are supported in Spring 2.5 (and 3.0).

like image 64
skaffman Avatar answered Oct 05 '22 02:10

skaffman