[英]Customize bad request HTTP400 page Spring MVC
当错误的请求完成并返回HTTP400错误代码时,是否可以自定义Spring返回的html页面?
是的,您可以实现这一目标。
您需要定义全局错误处理程序ControllerAdvice批注。示例代码为:
@ControllerAdvice public class GlobalExceptionHandler { public static final String DEFAULT_ERROR_VIEW = "error"; @ExceptionHandler(value = Exception.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception { // If the exception is annotated with @ResponseStatus rethrow it and let // the framework handle it. if (AnnotationUtils.findAnnotation(e.getClass(), ResponseStatus.class) != null) throw e; // setup and send the user to a default error-view. ModelAndView mav = new ModelAndView(); mav.addObject("exception", e); mav.addObject("url", req.getRequestURL()); mav.setViewName(DEFAULT_ERROR_VIEW); return mav; } }
进一步阅读:
要添加到@Yogi中,请回答2个其他选择:
<error-page> <error-code>400</error-code> <location>/WEB-INF/my/location/to/http/errors/400.jsp</location> </error-page>
/resources
目录下的/error
目录下提供“查看”页面或“ HTTP错误代码”组页面(方便的配置约定)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.