Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this is an old thread, but I thought I throw my hat in the ring and see if I can muddy the water a little bit more :)</p> <p>I found my initial struggle to understand <code>@ModelAttribute</code> was a result of Spring's decision to combine several annotations into one. It became clearer once I split it into several smaller annotations:</p> <p>For parameter annotations, think of <code>@ModelAttribute</code> as the equivalent of <code>@Autowired + @Qualifier</code> i.e. it tries to retrieve a bean with the given name from the Spring managed model. If the named bean is not found, instead of throwing an error or returning <code>null</code>, it implicitly takes on the role of <code>@Bean</code> i.e. Create a new instance using the default constructor and add the bean to the model.</p> <p>For method annotations, think of <code>@ModelAttribute</code> as the equivalent of <code>@Bean + @Before</code>, i.e. it puts the bean constructed by user's code in the model and it's always called before a request handling method.</p> <p>Figuratively, I see <code>@ModelAttribute</code> as the following (please don't take it literally!!):</p> <pre><code>@Bean("person") @Before public Person createPerson(){ return new Person(); } @RequestMapping(...) public xxx handlePersonRequest( (@Autowired @Qualifier("person") | @Bean("person")) Person person, xxx){ ... } </code></pre> <p>As you can see, Spring made the right decision to make <code>@ModelAttribute</code> an all-encompassing annotation; no one wants to see an annotation smorgasbord.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload