I'm new to Spring MVC. Please help me unpack the documentation.
Documentation
Spring MVC Documentation states (emphasis mine):
@ModelAttribute
on a method argument indicates the argument should be retrieved from the model. If not present in the model, the argument should be instantiated first and then added to the model. Once present in the model, the argument’s fields should be populated from all request parameters that have matching names. The WebDataBinder class matches request parameter names — including query string parameters and form fields — to model attribute fields by name.@RequestParam
binds request parameters to a method parameter in your controller.
Disclaimer / Clarifier
I know that @ModelAttribute
and @RequestParam
are not the same thing, are not mutually exclusive, do not perform the same role, and can be used simultaneously, as in this question - indeed, @RequestParam
can be used to populate fields of @ModelAttribute
. My question is more geared towards the difference between their internal workings.
Question:
What is the difference between @ModelAttribute
(used on a method argument, not method) and @RequestParam
? Specifically:
- Source: Do
@RequestParam
and@ModelAttribute
have the same source of information / population, i.e. request parameters in URL, which may have been supplied as elements of a form / model that wasPOST
ed? - Usage: Is it correct that variables retrieved with
@RequestParam
are thrown away (unless passed into a model), whereas variables retrieved with@ModelAttribute
are automatically fed into the model to be returned?
Or in very basic coding examples, what is the real working difference between these two examples?
Example 1: @RequestParam
:
// foo and bar are thrown away, and are just used (e.g.) to control flow?
@RequestMapping(method = RequestMethod.POST)
public String testFooBar(@RequestParam("foo") String foo,
@RequestParam("bar") String bar, ModelMap model) {
try {
doStuff(foo, bar);
}
// other code
}
Example 2: @ModelAttribute
:
// FOOBAR CLASS
// Fields could of course be explicitly populated from parameters by @RequestParam
public class FooBar{
private String foo;
private String bar;
// plus set() and get() methods
}
// CONTROLLER
// Foo and Bar become part of the model to be returned for the next view?
@RequestMapping(method = RequestMethod.POST)
public String setupForm(@ModelAttribute("fooBar") FooBar foobar) {
String foo = fooBar.getFoo();
String bar = fooBar.getBar();
try {
doStuff(foo, bar);
}
// other code
}
My current understanding:
@ModelAttribute
and @RequestParam
both interrogate the request parameters for information, but they use this information differently:
@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.@ModelAttribute
populates the fields of a class, which then populates an attribute of the model to be passed back to the view
Is this correct?