I am trying to leverage an existing validation framework on the service layer of my application that throws a ValidationException in the event of validation failures. This exception object holds multiple errors, and I want to utilize an exception handler in Spring MVC to extract those errors and return validation messages to the view (thymeleaf).
So I have two questions about how to do this:
1) Can I manually create the BindingResult object out of a list of custom error objects to pass to the view layer?
2) Can I catch a ValidationException from a handler in a base Controller (could be thrown from multiple methods) and have it intercept the response and return the view with the validation messages? Or do I have to try/catch this exception in every controller method where it could be thrown and pull out the binding result there?
Obviously it would be better if all of this happened in one spot- if one handler can catch a ValidationException and inject error messages into the view. But would that method have access to the returned model and view? Any advice on this architecture would be very helpful.
Have you checked google? My first hit is
https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc
That would allow you to return a view for the desired exceptions, with the proper model.
Also look at the spring documentation for exception handler? http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/HandlerExceptionResolver.html
The question then becomes how the bindingresult can be retrieved. If I google for it, it looks like it's available as a request attribute: Spring 3 web request interceptor - how do I get BindingResult?