What is the purpose and usage of @ModelAttribute
in Spring MVC?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
@ModelAttribute
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@ModelAttribute
annotation:Check here for an example (Spring 2.5), also see "Using @ModelAttribute on a method argument" (Spring 3.1).
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:
This annotated method will allow access to the Person object in your View, since it gets automatically added to the Models by Spring.
See "Using @ModelAttribute on a method" (Spring 3.1).
Hope this helped.
This is used for data binding purposes in Spring
MVC
. Let you have a jsp having a form element in it e.g<form:form action="test-example" method="POST" commandName="testModelAttribute"> </form:form>
(Spring Form method, Simple form element can also be used)
Now when you will submit the form the form fields values will be available to you.
For my style, I always use @ModelAttribute to catch object from spring form jsp. for example, I design form on jsp page, that form exist with commandName
and I catch the object on controller with follow code
and every field name of book must be match with path in sub-element of form
@ModelAttribute
will create a attribute with the name specified by you(@ModelAttribute("Testing") Test test) as Testing
in the given example ,Test being the bean test being the reference to the bean and Testing will be available in model so that you can further use it on jsp pages for retrieval of values that you stored in youModelAttribute
.Annotation that binds a method parameter or method return value to a named model attribute, exposed to a web view.
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 :)
I found my initial struggle to understand
@ModelAttribute
was a result of Spring's decision to combine several annotations into one. It became clearer once I split it into several smaller annotations:For parameter annotations, think of
@ModelAttribute
as the equivalent of@Autowired + @Qualifier
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 returningnull
, it implicitly takes on the role of@Bean
i.e. Create a new instance using the default constructor and add the bean to the model.For method annotations, think of
@ModelAttribute
as the equivalent of@Bean + @Before
, i.e. it puts the bean constructed by user's code in the model and it's always called before a request handling method.Figuratively, I see
@ModelAttribute
as the following (please don't take it literally!!):As you can see, Spring made the right decision to make
@ModelAttribute
an all-encompassing annotation; no one wants to see an annotation smorgasbord.