• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
  • Piet Souris
  • salvin francis
  • fred rosenberger

need clarification on @ModelAttribute

Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

My POJO class

My form jsp

Posts: 16
  • Mark post as helpful
  • send pies
  • 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.
Posts: 1682
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • 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)

Destiny's powerful hand has made the bed of my future. And this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
    Bookmark Topic Watch Topic
  • New Topic