从Spring MVC @ExceptionHandler方法执行重定向 [英] Performing a redirect from a spring MVC @ExceptionHandler method

查看:205
本文介绍了从Spring MVC @ExceptionHandler方法执行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用以下方法:

@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执行重定向?还是可以通过某种方式来规避此限制?

Is there a way to perform a redirect from an @ExceptionHandler? Or maybe some way to circumvent this restriction?

我修改了我的异常处理程序,如下所示:

I have modified my exception handler as follows:

@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
}

这是可能引发异常的方法:

This is the method that may throw the exception:

@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:

The problem with my modified exception handler is that it systematically redirects me to the following 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 :

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

Here is my modified handler method:

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

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

The problem I now have is that the message is stuck in the session...

推荐答案

请注意,Spring 4.3.5+对此提供了开箱即用的支持(请参阅

Note that this is actually supported out-of-the-box by Spring 4.3.5+ (see SPR-14651 for more details).

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

I've managed to get it working using the RequestContextUtils class. My code looks like this

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

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

Then in the jsp page I simply access the attribute

<c:if test="${myAttribute}">
    <script type="text/javascript">
      // other stuff here
    </script>
</c:if>

希望有帮助!

这篇关于从Spring MVC @ExceptionHandler方法执行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆