目录
第3种:模板/静态目录种均无相应错误页面,直接响应SpringBoot默认错误页面
Spring Boot提供了默认的错误处理机制,由于弹窗信息基本使用的是WhitelabelErrorPage页面,
信息提示通常也是以英文呈现,对用户并不友好。
从出错提示中,可以发现无论发生什么错误,Spring Boot都会返回一个状态码及错误页面,如果关注源
码可以知道,这些错误信息的底层原来是ErrorMvcAutoConfiguration错误自动配置类。当应用程序
出现4xx或5xx之类错误是,ErrorPageCustomizer就会被激活,它主要用于定制错误处理的响应规
则。
自定义错误响应页面
响应页面定义可以有几种方式
第1种:有模板引擎
- 精确匹配error状态码,通过将错误页面命名为具体的错误状态码,例如:404.html放在引擎目录templates/error目录中,当发生对应状态码错误时,就可以响应对应的模板页
- 模糊匹配error状态码,通过定义4xx或5xx,匹配对应类型的所有错误
- 此种方式,精确匹配优先于模糊匹配
timestamp:时间戳
status:状态码
error:错误提示
exception:异常对象
message:异常消息
errors:JSR303数据校验错误
第2种:没有模板引擎
- 此种方式,模板引擎无法找到对应的错误页面
- 将至静态资源目录中的error目录中找相应页面。
第3种:模板/静态目录种均无相应错误页面,直接响应SpringBoot
默认错误页面
如果BasicErrorController的errorhtml方法没有找到对应的view对象,将前往error定义在
ErrorMvcAutoConfiguation的defaultErrorView中
自定义错误页面
- 在resources/templates中添加首页的index.html页面
- 在源码文件夹Application类的同目录中添加web文件夹,并创建IndexController
- 使用@Controller注解,完整名称org.springframework.stereotype.Controller
Annotation(注解)是Java的一种特殊注释机制,JVM可通过反射读取注解内容,从而根据
注解内容进行特定操作。
注解以@开头常见注解如@Override,给toString等方法注解表示重写,这类的注解仅用于开发者,编
译器处理完就会被去掉
该注解用于定义控制器类,在spring项目中由控制器负责将用户发来的请求转发到对应的
服务接口(Service层)
- 添加一个index方法,为index页面提供页面数据(此处无)
- 为index方法添加@GetMapping("/")注解,该注解提供路由信息,负责URL到Controller中的
- 具体方法的映射;也可以通过WebMvcConfigurer进行视图和控制器的映射实现页面映射
- 在templates中创建error文件夹,并创建404.html和500.html两个页面
- 运行测试,并在index方法中主动制造错误,测试500页面,访问不存在的url,测试404页面
packagecn.edu.lzzy.config;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.config.annotation.ViewControllerRegistry;
importorg.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
*
springmvc自定义配置
*
/
@Configuration
publicclassMvcConfigureimplementsWebMvcConfigurer{
publicvoidaddViewControllers(ViewControllerRegistryregistry){
//这样就不需要为每一个页面编写Controller代码了,对于纯渲染的页面可以直接这样定义就可
以
registry.addViewController("/home").setViewName("index");//forWard实现
}
}
<!--templates/index.html-->
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<title>主页</title>
</head>
<body>
<h1>
我是站点首页<br>
内容正在建设中,请耐心等待
</h1>
</body>
</html>
<!--templates/error/404.html-->
<!DOCTYPEhtml>
<htmllang="en"xmlns:th="http://www.thymeleaf.org">
<head></head>
<body>
<divclass="container">
<divclass="starter-template">
<h1>404——找不到对象</h1>
<divth:inline="text">你好,'[[${#httpServletRequest.remoteUser}]]'你访问的
页面不存在</div>
</div>
</div>
</body>
</html>
自定义错误处理
- 在web同目录下添加handler文件夹
- 添加两个自定义异常类,NotFoundException和BusinessException,代表资源不存在和业务
- 异常
- 添加一个统一的异常处理类PracticeExceptionHandler
- 使用@ControllerAdvice注解该类,该注解将所有Controller作为切面,利用切面技术为
- Controller扩展功能
- 添加专用于业务异常处理的异常处理方法,并使用ExceptionHandler注解,参数为自定义的业务
- 异常类
- 添加通用的异常处理方法,并使用ExceptionHandler注解,参数为Exception类
另一个@RestControllerAdvice注解将所有的RestController作为切面,为RESTful
Api扩展功能。
除全局异常处理外,还提供全局数据绑定功能:
全局数据绑定:@ModelAttribute,可对所有Controller的Model添加全局属性,示例如
下:
①全局控制器添加全局数据@ControllerAdvice publicclassGlobalController{ @ModelAttribute publicvoidaddGlobalData(Modelmodel){ model.addAttribute("contact","lzzy@163.com"); model.addAttribute("homeUrl","https://www.baidu.com") } }
②在控制器类中无需绑定数据,如:
@Controller publicclassHomeController{ @GetMapping("/") publicStringindex(){ return " index" } }
③在index.html中使用Thymeleaf绑定全局数据
<html> <head> <title>主页</title> </head> <body> <h1> <spanth:text="'欢迎访问必应搜索引擎:'+${homeUrl}+'惊不惊喜,意不意 外'"></span><br> <spanth:text="'联系偶们:'+${contact}"></span> </h1> </body> </html>
- 在error/error.html页面中显示异常信息,以标题显示业务异常,其他详细异常信息以注释的形式保存,在源代码视图中查看