【问题标题】:@ExceptionHandler method not redirect to return view@ExceptionHandler 方法不重定向到返回视图
【发布时间】:2016-08-31 11:44:40
【问题描述】:


我对@ControllerAdvice 中的@ExceptionHandler 方法有疑问 发生异常时会调用我的方法,但不会重定向到通过此方法返回的站点。我试图返回 ModelANdView、RedirectView 或 String,但它永远无法正常工作 这是我的方法的实际代码:

public static final String REFERER_HEADER = "Referer";
public static final String REDIRECT = "redirect:";    

@ExceptionHandler(value = MaxUploadSizeExceededException.class)
public RedirectView maxUploadSizeExceededExceptionHandler(MaxUploadSizeExceededException e,
                                                          HttpServletRequest request,HttpServletResponse response) {

    String sourcePage = request.getHeader(REFERER_HEADER);
    RedirectView rw = new RedirectView(REDIRECT + sourcePage);
    return rw;
}

但正如我所说,重定向不正确。在浏览器中,发生异常时我会留在页面上

【问题讨论】:

  • 我知道这是很久以前的事了,但你能找到解决办法吗?

标签: spring spring-mvc controller hybris


【解决方案1】:

你可以尝试其中任何一个吗...

public static final String REFERER_HEADER = "Referer";
public static final String FORWARD_PREFIX = "forward:";    

@ExceptionHandler(value = MaxUploadSizeExceededException.class)
public String maxUploadSizeExceededExceptionHandler(MaxUploadSizeExceededException e,
                                                      HttpServletRequest request,HttpServletResponse response) {

  String sourcePage = request.getHeader(REFERER_HEADER); // Assuming you have sourcePage eg : "/pageURL"
  return FORWARD_PREFIX + sourcePage;
}

public static final String REFERER_HEADER = "Referer";

@ExceptionHandler(value = MaxUploadSizeExceededException.class)
public ModelAndView maxUploadSizeExceededExceptionHandler(MaxUploadSizeExceededException e,
                                                  HttpServletRequest request,HttpServletResponse response) {

  String sourcePage = request.getHeader(REFERER_HEADER);
  final RedirectView rv = new RedirectView(sourcePage);
  rv.setStatusCode(HttpStatus.MOVED_PERMANENTLY);
  rv.setUrl(sourcePage);
  final ModelAndView mv = new ModelAndView(rv);
  return mv;
}

但我不确定...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-24
    • 1970-01-01
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 2017-09-30
    • 1970-01-01
    相关资源
    最近更新 更多