• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Passing a new object in Spring MVC model.addAttribute() method

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am implementing the Spring PetClinic Project from here

http://docs.spring.io/docs/petclinic.html

I have a small doubt, the user clicks the find owner page from the welcome page.

Controller code which handles the request

Now the control is moved to the Search.jsp page, which also allows the user to add a new owner.

My doubt is why in the model.addAttribute a new owner object is put?.


What is the need for that?. Why doesn't the flow directly navigated to Search.jsp

The Owner class has the properties of the owner like firstName, lastName. etc.

Please advice if you need any more information in the code. I will put it here.

Search.jsp

Thanks. I would really appreciate the advice.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its needed because modelAttributes are provided to the view in order to render it. If you look at that view the empty form is 'bound' to and owner object. You need to give it an empty one so that those field input values that are populated can be put on that object. Then when the form is submitted you get the populated owner.



When this view is rendered (even on the get request where it is empty) it is expecting a model attribute named owner to be present. In the case of the get for an empty form it will be a new Owner(). Now there is most likely another RequestMapping that handles the post, in which that model attribute is present as a parameter. In this case it will have the form input fields bound to it, so you receive a populated object.
 
reply
    Bookmark Topic Watch Topic
  • New Topic