Entering values ​​into error.getModel in onBindAndValidate

I want to add some data to the model if there were no validation errors. I thought I could do this in onBindAndValidate, where I have access to the error object that contains the model:

errors.getModel().putAll(map);

      

I also tried to put the values ​​one by one with help put(key, value)

, but with no success.

What can I do?

+1


a source to share


2 answers


I can do this after checking in processFinish:



return showPage(request, errors, getCurrentPage(request))
       .addAllObjects(map);

      

0


a source


You cannot directly add data to the BindException model. The reason why you cannot:

errors.getModel().putAll(map);

      

is that errors.getModel () creates and returns a new map every time you call it. So, in your onBindAndValidate example, you get a new model from a BindException object by adding your data to the model and then discarding it. In the second example, you add data to the model and then return it.



Another common usage when using the BindException model from controller methods would be like this:

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

      

See also: BindException # getModel ()

+1


a source







All Articles