Spring Boot默认的错误页面路径是在resources/templates/error/目录下,当发生404、500状态码时,默认会跳转到该路径下的页面。
也可以自定义错误页面:
配置类
@Configuration
public class ErrorConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
ErrorPage errorPage_404 = new ErrorPage(HttpStatus.NOT_FOUND,"/error_404");
ErrorPage errorPage_500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/error_500");
registry.addErrorPages(errorPage_404, errorPage_500);
}
}
处理器
@Controller
public class ErrorController {
@GetMapping("/error_{status}")
public String errorpage1(@PathVariable("status") String status) {
// System.out.println("进入错误处理页面"+status);
//这里返回的是页面的位置
return "error/"+status;
}
}
源码中springboot的web资源默认扫描位置是在一下四个路径下。
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = new String[]{
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
};
所以error/ + status 实际上他会找到public中的error文件夹。同理,放在static,或者resources等文件夹下都可以