Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://static.springsource.org/spring/docs/3.1.x/javadoc-api/org/springframework/web/bind/annotation/ModelAttribute.html" rel="noreferrer"><code>@ModelAttribute</code></a> refers to a property of the Model object (the M in MVC ;) so let's say we have a form with a form backing object that is called "Person" Then you can have Spring MVC supply this object to a Controller method by using the <code>@ModelAttribute</code> annotation:</p> <pre><code>public String processForm(@ModelAttribute("person") Person person){ person.getStuff(); } </code></pre> <p><a href="http://blog.jteam.nl/2009/05/14/simple-forms-with-spring-mvc-2-5/" rel="noreferrer">Check here</a> for an example (Spring 2.5), also see <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-method-args" rel="noreferrer">"Using @ModelAttribute on a method argument"</a> (Spring 3.1).</p> <p>On the other hand the annotation is used to define objects which should be part of a Model. So if you want to have a Person object referenced in the Model you can use the following method:</p> <pre><code>@ModelAttribute("person") public Person getPerson(){ return new Person(); } </code></pre> <p>This annotated method will allow access to the Person object in your View, since it gets automatically added to the Models by Spring. </p> <p>See <a href="http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods" rel="noreferrer">"Using @ModelAttribute on a method"</a> (Spring 3.1).</p> <p>Hope this helped.</p>
 

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