package com.zlz.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//① 实现HandlerInterceptor接口
public class MyInterceptor1 implements HandlerInterceptor {
//②重写preHandle方法,目的是Controller处理之前进行一些操作
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//③ 打印测试语句
System.out.println("拦截器代码1");
//④ 设置为true,就放行
return true;
}
}
package com.zlz.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyInterceptor2 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//进控制器拦截
System.out.println("进入拦截器 2");
return true;
}
}
package com.zlz.config;
import com.zlz.interceptor.MyInterceptor;
import com.zlz.interceptor.MyInterceptor2;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//只要是配置类都得加上
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
//添加拦截器1,order的顺序,谁的数字小就谁先走
registry.addInterceptor(new MyInterceptor()).addPathPatterns("/**").order(3);
//添加拦截器2
registry.addInterceptor(new MyInterceptor2()).addPathPatterns("/**").order(2);
}
}
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>404页面——您访问的资源找不到h1>
body>
html>
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>500页面——程序员小哥哥正在努力修复中,请稍后访问h1>
body>
html>
2.3 在config包下面创建ErrorPageConfig.java文件
package com.zlz.config;
import org.springframework.boot.web.server.ErrorPage;
import org.springframework.boot.web.server.ErrorPageRegistrar;
import org.springframework.boot.web.server.ErrorPageRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
//配置404页面和500页面,一般是在程序已经确定没啥bug的情况下再去做这件事情的
//① 加上@Configuration注解,表示其是一个配置类
@Configuration
//② 实现ErrorPageRegistrar接口
public class ErrorPageConfig implements ErrorPageRegistrar {
//③ 重写registerErrorPages方法
@Override
public void registerErrorPages(ErrorPageRegistry registry) {
//跳转到页面,static目录是不需要加上的
//④ 添加404状态码和出现404时需要跳转的页面
registry.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/err/404.html"));
//⑤ 添加4500状态码和出现500时需要跳转的页面
registry.addErrorPages(new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/err/500.html"));
}
}
//注意:TestController类上面并没有加上@RequestMapping注解
@RequestMapping("t1")
public String t1(){
//指向下面这句话,会出现算术异常错误
System.out.println(1/0);
return "123";
}
package com.zlz.config;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* 全局异常处理
* @RestControllerAdvice 异常处理类 返回结果都是json
* @ControllerAdvice 异常处理类 返回结果都是页面 都会走视图解析器
*/
//①加上@RestControllerAdvice注解
@RestControllerAdvice
public class MyExceptionHandler {
//②使用@ExceptionHandler,指定异常类型
@ExceptionHandler(NullPointerException.class)
//③定义方法,并且形参里面写对应的异常类型,便于调试(控制台部分打印信息)
public Map<String,Object> ex1(NullPointerException ex){
//④ 打印异常报错信息
ex.printStackTrace();
//⑤ 存储错误信息到map集合里面
Map<String,Object> map=new LinkedHashMap<>();
//服务器自定义状态码
map.put("code", 500);
map.put("msg","服务器出现了空指针异常");
//⑥ 把结果返回给前台
return map;
}
@ExceptionHandler(Exception.class)
public Map<String,Object> ex2(Exception ex){
//打印异常信息,便于调试者调试,用了全局异常 一定要打印
ex.printStackTrace();
Map<String,Object> map=new LinkedHashMap<>();
//服务器自定义状态码
map.put("code", 500);
map.put("msg","服务器出现了未知异常");
return map;
}
}
@RequestMapping("err1")
public String err1(){
String str=null;
System.out.println(str.equals(""));
return "123";
}
@RequestMapping("err2")
public String err2(){
System.out.println(6/0);
return "123";
}
<dependency>
<groupId>org.apache.tomcat.embedgroupId>
<artifactId>tomcat-embed-jasperartifactId>
dependency>
<packaging>warpackaging>
package com.zlz.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class JspController {
@RequestMapping("index")
public String a(){
return "index";
}
}
spring:
mvc:
view:
prefix: /
suffix: .jsp