我想要以下方法:
@ExceptionHandler(MyRuntimeException.class)
public String myRuntimeException(MyRuntimeException e, RedirectAttributes redirectAttrs){//does not work
redirectAttrs.addFlashAttribute("error", e);
return "redirect:someView";
}
我得到一个:
java.lang.IllegalStateException: No suitable resolver for argument [1] type=org.springframework.web.servlet.mvc.support.RedirectAttributes]
有没有办法从一个执行重定向@ExceptionHandler
?或者也许有某种方法可以规避这个限制?
编辑:
我修改了我的异常处理程序如下:
@ExceptionHandler(InvalidTokenException.class)
public ModelAndView invalidTokenException(InvalidTokenException e, HttpServletRequest request) {
RedirectView redirectView = new RedirectView("signin");
return new ModelAndView(redirectView , "message", "invalid token/member not found");//TODO:i18n
}
这是可能引发异常的方法:
@RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html")
public String activateMember(@PathVariable("token") String token) {
signupService.activateMember(token);
return "redirect:memberArea/index";
}
我修改的异常处理程序的问题在于它系统地将我重定向到以下 URL:
http://localhost:8080/bignibou/activateMember/signin?message=invalid+token%2Fmember+not+found
代替:
http://localhost:8080/bignibou/signin?message=invalid+token%2Fmember+not+found
编辑 2:
这是我修改后的处理程序方法:
@ExceptionHandler(InvalidTokenException.class)
public String invalidTokenException(InvalidTokenException e, HttpSession session) {
session.setAttribute("message", "invalid token/member not found");// TODO:i18n
return "redirect:../signin";
}
我现在遇到的问题是消息卡在会话中...