Spring MVC: please explain difference between @RequestParam and @ModelAttribute Spring MVC: please explain difference between @RequestParam and @ModelAttribute spring spring

Spring MVC: please explain difference between @RequestParam and @ModelAttribute


@RequestParam just populates stand-alone variables (which may of course be fields in a @ModelAttribute class). These variables will be thrown away when the Controller is done, unless they have been fed into the model.

Don't confuse the word "model" with session. The http conversation is generally: HTTP.GET, server response, then HTTP.POST. When you have the @ModelAttribute annotation in use you are always constructing an instance of whatever you have annotated, this is what makes you think that 'feeding things to the model' might make variables stick around. This isn't correct, once the HttpServletRequest has finished those variables should no longer be a part of the browser/server conversation unless they've been saved in a session.

@ModelAttribute populates the fields of a class, which then populates an attribute of the model to be passed back to the view

Yes! To be correct, @ModelAttribute tells Spring to use its default web data binder to populate an instance of something with data from the HttpServletRequest. Choosing to pass this data back to the view is up to the programmer. When you have a method annotated with @ModelAttribute, it is being called every time code hits that servlet. When you have @ModelAttribute as one of the method's parameters, we are talking about incoming Http form data-binding.

Calling @RequestParam is a shortcut for saying request.getParameter("foo"); under the hood, Java's HttpServletRequest lets you get values from the request object by doing a key->value look up. The value returned is of type Object. This is what you would be typing a lot if you were not using Spring in your web application.

Spring then takes this abstraction a step further when you start to use @ModelAttribute. This annotation employs the concept of data-binding. The goal of data-binding is that the code in your controller won't have to call request.getParameter("foo1"), for every form element. Imagine you have a web form with 5 fields. Without data-binding the programmer has to manually retrieve, and validate each of those fields. The programmer has to make sure that the request contains the property, that the property's value exists, and that the property's value is of the type expected for each field. Using @ModelAttribute tells Spring to do this work for you.

If you annotate a method in your controller with @ModelAttribute("fooBar") FooBar fooBar An instance of FooBar will always be constructed by Spring, and supplied to your method. Where the data binding comes into play, is when this annotation is used in a Method's parameters; Spring looks at the instance of HttpServletRequest and sees if it can match the data in the request to the right property on an instance of FooBar. This is based off the java properties convention, where you have a field such as foo and public getters and setters called getFoo and setFoo. This might seem magic but if you were to break convention, your Spring data binding would stop working because it wouldn't be able to know where to bind the data from your HttpServletRequest You would still get an instance of FooBar, but the properties would not be set to any values from the request.


@ModelAttribute: binds an entire Java object (like Employee). supports multiple request parameters

@RequestParam: binds a single request parameter (like firstName)

In general,
@RequestParam is best for reading a small number of params.

@ModelAttribute is used when you have a form with a large number of fields.

and

@ModelAttribute gives you additional features such as data binding, validation and form prepopulation.


@ModelAttribute annotated parameters are handled by a registered ServletModelAttributeMethodProcessor (or ModelAttributeMethodProcessor) and @RequestParam annotated parameters are handled by a registered RequestParamMethodArgumentResolver or RequestParamMapMethodArgumentResolver depending on the parameter type.

Here's an explanation of how Spring uses these HandlerMethodArgumentResolvers to resolve arguments for your handler methods:

In both cases, @ModelAttribute and @RequestParam, the values to be bound are retrieved from ServletRequest parameters.

You can look at the source code of the types mentioned above, but here are the simple details.

For @ModelAttribute, Spring will create an instance of the parameter type. It will inspect the fields of that instance and attempt to bind parameter values to them based on a naming/aliasing strategy composed of the @ModelAttribute name and the field names. It typically uses a group of Converter instances to convert from String (parameter values are always String values) to whatever the target field type Integer, Date, etc. You can also register your own Converter types for custom conversions. You can also nest POJO types.

For @RequestParam, Spring will use the same Converter instances to convert from the parameter values directly to the annotated parameter type.

Note that parameter values are not "thrown away". They are stored in the HttpServletRequest for the duration of the container's request processing cycle. You can always access them through the appropriate methods.