Fabio Cepeda

Dynamically binding a list of objects with Spring

Posted on: September 16, 2008

These last two weeks I’ve been re-working on dynamically binding a list of objects using SpringMVC. My goal is not to loose the elements in the collections when an error happens in the submission of the form. Right now I am adding and deleting collection elements using jQuery, and DWR excellent javascript libraries by the way.

Some history of the problem:

The first attempt used what Matt Raible used initially in Appfuse 1.8 long time ago, where a select list was used to group the elements of the collection and at submission the collection was formed in the onSubmit method of the form controller. The second attempt used a new kind of editor in spring called CustomCollectionEditor and also followed some later findings of Matt Raible. In this solution, spring was able to form a collection of objects of any type by overriding the convertElement() method in the initBinder() method. This solution clear up the code in the onSubmit() method and move it where it belongs. The addition and deletion of objects in the collections is managed by javascript, but this causes to lose the elements of a collection in the presence of an error during form submission.

For right now, I have opted to avoid the binding errors through optical cues in the form so that the user can select what is suppossed to. One thing that I discover is that if you want to have the elements binded when added to the collection with javascript you will have to exposed this functionality through DWR and make sure the element gets persisted. In my case this complicate things since the collection element is a fairly complex and large POJO. Maybe there is another way, using onBind or those methods to make sure the collection is binded. I’ll check into that and post my findings later.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s



%d bloggers like this: