Spring’s @ModelAttribute

What is it?

@ModelAttribute annotated methods are executed before the chosen @RequestMapping annotated handler method. They effectively pre-populate the implicit model with specific attributes, often loaded from a database. Such an attribute can then already be accessed through @ModelAttribute annotated handler method parameters in the chosen handler method, potentially with binding and validation applied to it. (Spring Docs)

refers to a property of the Model object…Spring MVC [can] supply this object to a Controller method (StackOverflow)

It can be placed in 2 locations:

  • Method Level – to provide reference data for the model
    • Note: method signature can be contain same types as @RequestMapping
  • Parameter Level – to map a model attribute to a method parameter, so Controller can access it

Good explanation here.
Another good one here.

See Spring documentation for more info: http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args

Posted in Spring, Spring MVC

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: