从Spring 4.3.5和5.0 M4开始,它在@ExceptionHandler
方法中支持RedirectAttributes
参数。
@ExceptionHandler(MyCustomException.class)
public String handleError1(MyCustomException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", "abcdefg");
return "redirect:/viewName";
}
@ExceptionHandler(MultipartException.class)
public String handleError2(MultipartException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
return "redirect:/viewName";
}
PS阅读此SPR-14651
已记录
如果您使用的是Spring <4.3.5,请不要将此RedirectAttributes
作为参数添加到@ExceptionHandler
方法中,否则,Spring将无法捕获引发的异常。
参考文献
翻译自: https://mkyong.com/spring/spring-exceptionhandler-and-redirectattributes/