0 votes
1 view
in Java by (2.6k points)
What is the purpose and usage of @ModelAttribute in Spring MVC?

1 Answer

0 votes
by (46.1k points)

@ModelAttribute relates to a feature of the Model object (the M in MVC ;) so let's suppose we have a class with a form backing object that is called "Person" Then you can hold Spring MVC stores this object to a Controller process by applying the @ModelAttribute annotation:

public String processForm(@ModelAttribute("person") Person person){

    person.getStuff();

}

check out"Doing @ModelAttribute on a process argument" (Spring 3.1).

On the opposite hand, the annotation is applied to represent objects which must be part of a Model. So if you require to have a Person object referenced in the Model you can practice the following method:

@ModelAttribute("person")

public Person getPerson(){

    return new Person();

}

This annotated approach will provide access to the Person object in your View as it gets automatically attached to the Models by Spring.

check out "Practicing @ModelAttribute on a purpose" (Spring 3.1).

I wish this helped.

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...