从spring MVC @ExceptionHandler方法执行重定向

bal*_*teo 14 spring-mvc post-redirect-get

我想要以下方法:

@ExceptionHandler(MyRuntimeException.class)
public String myRuntimeException(MyRuntimeException e, RedirectAttributes redirectAttrs){//does not work
    redirectAttrs.addFlashAttribute("error", e);
    return "redirect:someView";
}
Run Code Online (Sandbox Code Playgroud)

我得到一个:

java.lang.IllegalStateException: No suitable resolver for argument [1] type=org.springframework.web.servlet.mvc.support.RedirectAttributes]
Run Code Online (Sandbox Code Playgroud)

有没有办法从@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
}
Run Code Online (Sandbox Code Playgroud)

这是可能抛出异常的方法:

@RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html")
public String activateMember(@PathVariable("token") String token) {
    signupService.activateMember(token);
    return "redirect:memberArea/index";
}
Run Code Online (Sandbox Code Playgroud)

我修改过的异常处理程序的问题是它系统地将我重定向到以下URL:

http://localhost:8080/bignibou/activateMember/signin?message=invalid+token%2Fmember+not+found 
Run Code Online (Sandbox Code Playgroud)

代替:

http://localhost:8080/bignibou/signin?message=invalid+token%2Fmember+not+found
Run Code Online (Sandbox Code Playgroud)

编辑2:

这是我修改过的处理程序方法:

@ExceptionHandler(InvalidTokenException.class)
public String invalidTokenException(InvalidTokenException e, HttpSession session) {
session.setAttribute("message", "invalid token/member not found");// TODO:i18n
return "redirect:../signin";
}
Run Code Online (Sandbox Code Playgroud)

我现在遇到的问题是消息被卡在会话中......

mer*_*no1 24

请注意,这实际上是Spring 4.3.5+支持开箱即用(有关详细信息,请参阅SPR-14651).

我已经设法使用RequestContextUtils类使其工作.我的代码看起来像这样

@ExceptionHandler(MyException.class)
public RedirectView handleMyException(MyException ex,
                             HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
    String redirect = getRedirectUrl(currentHomepageId);

    RedirectView rw = new RedirectView(redirect);
    rw.setStatusCode(HttpStatus.MOVED_PERMANENTLY); // you might not need this
    FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
    if (outputFlashMap != null){
        outputFlashMap.put("myAttribute", true);
    }
    return rw;
}
Run Code Online (Sandbox Code Playgroud)

然后在jsp页面中我只访问该属性

<c:if test="${myAttribute}">
    <script type="text/javascript">
      // other stuff here
    </script>
</c:if>
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


Mic*_*son 6

您可以随时转发然后重定向(或重定向两次)..首先到另一个请求映射,您可以正常访问RedirectAttributes,然后再次到达最终目的地.

    @ExceptionHandler(Exception.class)
    public String handleException(final Exception e) {

        return "forward:/n/error";
    }

    @RequestMapping(value = "/n/error", method = RequestMethod.GET)
    public String error(final RedirectAttributes redirectAttributes) {

        redirectAttributes.addAttribute("foo", "baz");
        return "redirect:/final-destination";
    }
Run Code Online (Sandbox Code Playgroud)