Spring tries to bind all fields

Using Spring 3

I have two forms: adding Item and invalidating Item I have ItemAddEditCommand that references Item and some other data.

Adding Item works great but I have problem with invalidating Item. In general it is an edit form that contains two form fields – expirationDate and comment.

In the controller I’m using the same command for both actions. Unfortunately Spring attempts to bind all fields when I invalidate. When starting to invalidate I populate ItemCommand with Item instance that has all possible fields filled (including id)

When user inserts expireDate and comment and submits the form then all other fields except expiresDate and comment are nulled.

Is there another way except creating another command


I think I found it.

@InitBinder(value = { INVALIDATE_ITEM_PARAM })
protected void initInvalidateItemBinder(WebDataBinder binder) {
  binder.setAllowedFields("expireDate", "comment");

Btw. Maybe anyone can give me pointers how to get rid of DefaultBindInitializer.initBinder(binder); that initializes some property editors that should be ALWAYS used. Maybe some bean to make it default so I wouldn’t have to call this method all the time.

Leave a Reply

Your email address will not be published. Required fields are marked *