views:

31

answers:

1

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

+2  A: 

I think I found it.

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

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.

Priit
You can set `WebBindingInitializer` in `AnnotationMethodHandlerAdapter`, see Petclinic: https://src.springframework.org/svn/spring-samples/petclinic/trunk/
axtavt