I'm sending a request with form data to ProcessorActionBean for processing. An error occurs, but the ProcessorActionBean does not have a JSP view - it is just for processing form data - so I catch the errors by implementing a ValidationErrorHandler on the ProcessorActionBean, and from within handleValidationErrors(), I redirect it to DisplayerActionBean.
The problem is that the error that caused the method to be run disappears after the redirect. I can put non-error messages in the context, and they'll be shown in the DisplayerActionBean's page, but error messages appear to go to /dev/null.
How do I get the errors to display, too?
The solution turned out to be to:
a) in the handleValidationErrors() method, use a FlashScope to put the validation errors somewhere they'll survive until the next request:
FlashScope scope = FlashScope.getCurrent(getRequest(), true);
scope.put("your_key",listOfValidationErrors);
b) in an Interceptor (I've used a modified ErrorMessageInterceptor), where if you find some errors under your_key
, you put them in context's validation errors:
ValidationErrors errors = ctx.getActionBeanContext().getValidationErrors();
errors.add(someError.getFieldName(), someError);
(The salient difference to the ErrorMessageInterceptor is that you put each error you get out of the list (which you put in there in the validation error handler) in the regular validation errors, not global errors. This allows them to retain the field they are related to.)