Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC - how not to lose field values when binding to a form partially

I want to make an update form for a bean X. This bean lets say it has fields A, B, C, D. In my form I want to update just fields A, B and let C and D untouched. Is there a way to bind bean X to the update form with just fields A and B, so that when i submit the form C and D won't be changed ?

I know i can add hidden fields for C and D but what if these are not primitive fields, they are other beans or collections.

I know another solution would be to create a XUpdateBean that will have only fields A and B and after form submit copy the fields from XUpdateBean to my X bean.

Is there another way to this update better in Spring 3 MVC?

like image 621
AdrianS Avatar asked Feb 29 '12 16:02

AdrianS


1 Answers

You could have a command-Object/form-barking-Bean that contains only the fields you need.

In the controller you have to load the bean X, and need to update its fields with the one from the commandObject.

May you can also think of not having an extra class for the commandObject, instead use class BeanX. But of course you need two instances of BeanX, one for the commandObject and one for bean x.

like image 52
Ralph Avatar answered Oct 20 '22 20:10

Ralph