Spring SimpleFormController, Showing error msg on the formView
I'm using Spring SimpleFormController to handle a form submission with validation. I expect a case where an exception will occur inside my "onSubmit" method. In such a case, i want the user to see the original formView with all the data filled in plus the error message from the exception.
I've checked around, i see people recommend a class from Spring like *ExceptionMapper which it can forward user to a page with friendly error mess开发者_开发技巧age.
So please share me on how to make this happen?
dara kok
If you are using SimpleFormController
and onSubmit
, you are probably not using Spring annotations and related features. You could do something like the following in your controller. In your client-side, you can use the contents of error
object to display errors, along with the form. Note that onSubmit()
and showForm()
are deprecated in spring 3.0.
@Override
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse
response, Object command, BindException errors) throws Exception {
// Do your processing
if (somethingWentWrong) {
errors.reject("error.server.error");
return showForm(request, response, errors);
} else {
return new ModelAndView(getSuccessView());
}
}
精彩评论