views:

287

answers:

2

I want to add some data to model if there wasn't any validation errors. I thought I could do this in onBindAndValidate, where I have the access to error object, which contains model:

errors.getModel().putAll(map);

I also tried to put values one by one using put(key, value) but without success.

What can I do?

A: 

I can do this after validation in processFinish:

return showPage(request, errors, getCurrentPage(request))
       .addAllObjects(map);
egaga
+1  A: 

You cannot directly add data to BindException's model. The reason you're not able to do:

errors.getModel().putAll(map);

is because errors.getModel() creates and returns new map each time you call it. So in your onBindAndValidate example, you're getting a new model from the BindException object, adding your data to the model, and then throwing it away. In your second example, you're adding the data to a model and then returning it.

Other common usage when using BindException's model from controller methods would look like this:

Map errorModel = errors.getModel();
errorModel.putAll(otherMap);
return new ModelAndView("viewName", errorModel );

See also: BindException#getModel()

Lyle