Posts

Showing posts with the label @ModelAttribute

Spring @ModelAttribute Annotation Example

Image
In this section we will learn about  @ModelAttribute  Annotation.   The  @ModelAttribute   annotation refers to the Model object in MVC. We can pass @ModelAttribute annotation to method arguments or we can even annotate a method as well.  Passing @ModelAttribute to method argument indicates that the value should bind to the method argument. public String processForm ( @ModelAttribute ( "user" ) User user ) { user. doStuff ( ) ; } Assume that we have a form that is backed by the User object, when the user submits the form, the values should bind to the method argument with the help of @ ModelAttribute annotation.  By annotating @ModelAttribute annotation to method defines the object, which will automatically be added to the Model , and later we can use the Model object inside the view template. @ModelAttribute ( "user" ) public User getUser ( ) { return new User ( ) ; } Here the above method will allow access to the User object in our View sinc