• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

need clarification on @ModelAttribute

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am implementing form handling. I have a form page student.jsp where the user is required to fill in the details.
I have a POJO class for that. Then I have a formOutput.jsp which displays the user details.

Here is the code for my controller



My code is working fine. Can anyone please tell me what is the role of "@ModelAttribute("SpringWeb")Student student" parameter in addStudent method.
Thanks.

My POJO class



My form jsp

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@ModelAttribute is used to direclty set the values in the Student object from the jsp, other wise in the servlet you have to get the properties using request.getattribute() and than call student setter method.
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is an implicit model even if you do not pass one in. This Model is used by spring to store information needed to render the view. A model is just a glorified Map. A ModelAttribute is stored in the model under the name you give or using convention if a name is not provided. In your case you have a Person, that means that if a Person object exists in the Model under the key "SpringWeb" (would make more sense to name it "person" here. It will be retrieved and passed into your method. If it does not exist a new Person object will be created automatically and put in the model. You can than access it directly in your jsp (you would get rid of command)






 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic