在我当前的 spring-boot 项目中,我有这个 ExceptionHandler:
@ControllerAdvice
public class GlobalDefaultExceptionHandler {
@ExceptionHandler(value = Exception.class)
public ModelAndView defaultErrorHandler(HttpServletRequest request, Exception error) throws Exception {
if (AnnotationUtils.findAnnotation(error.getClass(), ResponseStatus.class) != null)
throw error;
ModelAndView mav = new ModelAndView();
mav.setAttribute("error", error);
return mav;
}
}
我想做的是让这个处理程序重定向到不同的错误页面,这取决于错误的来源。
我的项目中有两种“类型”的页面:一种是公共的,由匿名用户或经过身份验证的用户访问,另一种是管理页面(私有),只有经过身份验证的用户才能访问。
这两种类型的页面有不同的样式。我希望,当用户在公共页面时发生错误时,会显示一个具有公共样式的错误页面。如果用户在私人页面时发生错误,则会显示另一个错误页面,该页面具有私人页面的样式。