在SpringBoot使用错误页面非常的简单
一、 错误页面使用
只需要在templates里创建一个error文件夹,然后它会自动到error里面找到相对应的页面,比如说我们需要404页面,那么只需要在error里放入一个404页面就ok了,想要500就在里面放入500的页面(我这里写的5xx意思是说500、501、502…等都可以访问这个5xx)
【 error/5xx.html;有精确的错误状态码页面就匹配精确,没有就找 5xx.html;如果都没有就触发白页(也就是默认的)】
正常情况下,500是这样子的
使用了error里面的5xx是这样的
(修改标签体内容之前——原本)
<section class="error-wrapper text-center">
<h1><img alt="" src="images/500-error.png"></h1>
<h2>OOOPS!!!</h2>
<h3>Something went wrong.</h3>
<p class="nrml-txt">Why not try refreshing you page? Or you can <a href="#">contact our support</a> if the problem persists.</p>
<a class="back-btn" href="index.html"> Back To Home</a>
</section>
(修改标签体内容之后——修改)
<section class="error-wrapper text-center">
<h1><img alt="" src="images/500-error.png"></h1>
<h2>OOOPS!!!</h2>
<h3 th:text="${message}">Something went wrong.</h3>
<p class="nrml-txt" th:text="${trace}">Why not try refreshing you page? Or you can <a href="#">contact our support</a> if the problem persists.</p>
<a class="back-btn" href="index.html"> Back To Home</a>
</section>
二、 自定义错误页
package com.atguigu.admin.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
/**
* 处理整个Web Controller的异常
* @author LunarYouI
* @create 2021-04-13 11:06
*/
@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler({ArithmeticException.class,NullPointerException.class}) //处理异常(数学运算异常、空指针异常)
public String handleArithException(Exception e){
//打印处理了哪些异常
log.error("异常是:{}",e);
return "login"; //视图地址
}
}
出现数学运算异常或者空指针异常那么就会跳转登录页面
三、自定义异常
@GetMapping("/dynamic_table")
public String dynamic_table(Model model){
//表格内容的遍历 (Arrays.asList:将一个数组转化为一个List对象)
List<User> users = Arrays.asList(new User("张三", "123456"),
new User("李四", "654321"),
new User("王五", "135791"),
new User("张麻子", "135790"));
model.addAttribute("users",users);
//大于3就触发自定义异常
if(users.size()>3){
throw new UserTooManyException();
}
return "/table/dynamic_table";
}
自定义异常
package com.atguigu.admin.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* 自定义异常
* @author LunarYouI
* @create 2021-04-13 11:41
*/
//拒绝403
@ResponseStatus(value = HttpStatus.FORBIDDEN,reason = "用户数量太多")
public class UserTooManyException extends RuntimeException{
public UserTooManyException(){
}
public UserTooManyException(String message){
super(message);
}
}
四、自定义异常解析器
使用了@Order(value = Ordered.HIGHEST_PRECEDENCE) 那么我们自定义的异常解析器优先级就会特别高,不管触发了什么空指针异常或者自定义等其它异常,他都会优先显示“ 511 我喜欢的错误”
package com.atguigu.admin.exception;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 自定义异常解析器
*
* @author LunarYouI
* @create 2021-04-13 19:06
*/
@Order(value = Ordered.HIGHEST_PRECEDENCE) //优先级。数字越小优先级越高
@Component
public class CustomerHandlerExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request,
HttpServletResponse response,
Object o, Exception ex) {
try {
response.sendError(511,"我喜欢的错误");
} catch (IOException e) {
e.printStackTrace();
}
return new ModelAndView();
}
}
自定义实现 HandlerExceptionResolver 处理异常;可以作为默认的全局异常处理规则