wood burning stoves 2.0*
The moose likes Spring and the fly likes ID isn't binded with form object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "ID isn Watch "ID isn New topic
Author

ID isn't binded with form object

Jozo Stan
Greenhorn

Joined: Aug 18, 2012
Posts: 12
Hello everybody!

I have a form in which I select sport and country with dropdown list and there is also a text box where i write a name for the new team. When I click on save I receive this exception:



Here is my code:

Team.java


NewTeam controller




Part of NewTeam.jsp



I tried to find the cause of the error in debug mode and here is what I received.



Sport_id is 0 and drzava_id is 0 and I don't understand why. I'm out of ideas, please help.
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

This is a Spring issue not a hibernate issue so I will move this to the Spring forums.


[How To Ask Questions][Read before you PM me]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

When you bind to the select lists you are actually only going to get the id (as a string) . Spring will not know how to convert this to a Sport object. To do this you will have write your own formatter. I will give you a overly simplified example but this is just for illustration. For your purposes it would make the most sense to make your formatter take a generic entity and do a find by id. This would look into persistence context for the entity with the given id.

Once again this an oversimplified example to illustrate the point..

Controller



Person






home.jsp (only the form part)


Now the formatter this does the work converting the college id we receive into a college object. Remember this is over simplified in your case you would probably at the very least autowire in a service or repository to do a findById. If you wanted you could make it more generic to allow it to accept any entity type.




Now lastly we have to tell spring to use our new formatter in the controllers


Now when we get into the addPerson method of the controller after posting the form our Person object will have a fully populated College object on it, and there will not be any errors in the binding result which I am sure you probably had before.

Jozo Stan
Greenhorn

Joined: Aug 18, 2012
Posts: 12
Thank you very much! I solved that issue. I couldn't post before because I still have other problems with my application.
 
 
subject: ID isn't binded with form object