Spring @ExceptionHandler and RedirectAttributes

摘要: Since Spring 4.3.5 and 5.0 M4, it supports RedirectAttributes argument in the @ExceptionHandler method.

Since Spring 4.3.5 and 5.0 M4, it supports RedirectAttributes argument in the @ExceptionHandler method.

	@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";

P.S Read this SPR-14651

Noted
If you are using Spring < 4.3.5, do not add this RedirectAttributes as the argument into the @ExceptionHandler method, otherwise, Spring will not able to catch the thrown exception.

References

  1. SPR-14651
  2. Spring @ExceptionHandler

上一篇: Spring Boot file upload example Ajax and REST
下一篇: Oracle PL/SQL – After DELETE Trigger example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号