文章目录
-
- 1.自定义拦截器
-
-
- 1.基本介绍
-
- 2.快速入门
-
- 1.Myinterceptor01.java
- 2.FurnHandler.java
- 3.springDispatcherServlet-servlet.xml配置拦截器
- 4.单元测试
- 3.拦截特定路径
-
- 4.细节说明
- 5.多个拦截器
-
- 1.执行流程图
- 2.应用实例
-
- 1.FurnHandler.java目标方法
- 2.拦截器
- 3.结果展示
- 2.异常处理
-
-
- 1.基本介绍
- 2.局部异常处理器
-
- 1.需求分析
- 2.抛出问题
-
- 1.MyExceptionHandler.java
- 2.不处理异常则交给tomcat处理
- 3.局部异常实例
-
- 1.MyExceptionHandler.java
- 2.exception_mes.jsp
- 3.结果展示
- 3.全局异常处理器
- 1.基本介绍
- 2.全局异常实例
-
-
- 1.MyGlobalException.java
- 2.global_exception.jsp
- 3.结果展示
- 3.细节说明
-
- 4.自定义异常
-
- 1.基本介绍
- 2.自定义异常应用实例
-
- 1.SelfException.java
- 2.selfex.java
- 3.结果展示
- 3.可以使用有参构造,抛出异常交给异常处理器接管
-
- 1.SelfException.java
- 2.selfex.java抛出自定义异常并制定message
- 3.MyGlobalException.java捕获自定义异常
- 4.结果展示
- 5.统一异常处理器
-
- 1.基本介绍
- 2.需求分析
- 3.具体实现
-
- 1.MyExceptionHandler.java抛出数组越界异常
- 2.springDispatcherServlet-servlet.xml配置统一异常处理器
- 3.arrEx.jsp异常处理页面
- 4.结果展示
- 6.对未知异常进行统一处理
-
- 1.具体实现
-
- 1.MyExceptionHandler.java抛出异常
- 2.springDispatcherServlet-servlet.xml修改统一异常处理器
- 3.allEx.jsp所有未知异常的处理
- 4.结果展示
- 7.异常处理的优先级
-
- 1.局部异常处理器
- 2.全局异常处理器
- 3.统一异常处理器
- 4.Tomcat默认机制
- 3.springMVC阶段的配置文件
-
-
- 1.springDispatcherServlet-servlet.xml
- 2.web.xml
1.自定义拦截器
1.基本介绍
1.说明
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-bcea25e17adfd4b30473e528a3e09b9d.png)
2.自定义拦截器的三个方法
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-5660ca972f7a3180fc3ffc7b07950501.png)
3.流程图
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-7e6d8528d4abe1461031b02c784723de.png)
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-702e83c13bcf92b1bb1740fddbeefd75.png)
2.快速入门
1.Myinterceptor01.java
package com.sun.web.interceptor;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class Myinterceptor01 implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion");
}
}
2.FurnHandler.java
package com.sun.web.interceptor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class FurnHandler {
@RequestMapping("/hi")
public String hi() {
System.out.println("hi方法被调用");
return "success";
}
@RequestMapping("/hello")
public String hello() {
System.out.println("hello方法被调用");
return "success";
}
}
3.springDispatcherServlet-servlet.xml配置拦截器
mvc:interceptors>
ref bean="myinterceptor01"/>
mvc:interceptors>
4.单元测试
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-9702d79517f013fb219e25d9d7827fc8.png)
3.拦截特定路径
1.拦截指定路径
mvc:interceptors>
mvc:interceptor>
mvc:mapping path="/hi"/>
ref bean="myinterceptor01"/>
mvc:interceptor>
mvc:interceptors>
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-ca2f416a8f6f30699a6fc5facbfe6418.png)
2.通配符配置路径
mvc:interceptors>
mvc:interceptor>
mvc:mapping path="/h*"/>
mvc:exclude-mapping path="/hi"/>
ref bean="myinterceptor01"/>
mvc:interceptor>
mvc:interceptors>
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-1ec9ed596d110311b5d3ff7ac47b0568.png)
4.细节说明
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-5a8faf0e514a1ecf4de4c364c41b84fd.png)
5.多个拦截器
1.执行流程图
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-6c967133d79c67426113302595029f2c.png)
2.应用实例
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-38f9e0319bda04cad7723965135d6322.png)
1.FurnHandler.java目标方法
@RequestMapping("/topic")
public String topic() {
System.out.println("topic执行!");
return "success";
}
2.拦截器
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String topic = request.getParameter("topic");
if (topic.equals("topic")) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("请不要乱说话!!
");
}
return false;
}
3.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-ff05498e2cc246c8383ae659d67f497c.png)
2.异常处理
1.基本介绍
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-64a80151fe93da198691e84fdf99d4f4.png)
2.局部异常处理器
1.需求分析
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-f169d5729f7ef048a1504bf12a031a2b.png)
2.抛出问题
1.MyExceptionHandler.java
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyExceptionHandler {
@RequestMapping("/getNum/{num}")
public String getNUm(@PathVariable("num") Integer num) {
System.out.println(10 / num);
return "success";
}
}
2.不处理异常则交给tomcat处理
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-5eed73415615b18d8c3bdcd61c729228.png)
3.局部异常实例
1.MyExceptionHandler.java
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
@Controller
public class MyExceptionHandler {
@RequestMapping("/getNum/{num}")
public String getNUm(@PathVariable("num") Integer num) {
System.out.println(10 / num);
return "success";
}
@ExceptionHandler({ArithmeticException.class, NullPointerException.class})
public String exceptionHandler(Exception ex, HttpServletRequest request) {
System.out.println("异常的信息=" + ex.getMessage());
request.setAttribute("reason", ex.getMessage());
return "exception_mes";
}
}
2.exception_mes.jsp
Title
出现异常
异常信息是${requestScope.reason}
3.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-f713d3a895eebd51acef14443eed6a47.png)
3.全局异常处理器
1.基本介绍
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-a2bd8eda8ab226c95af3373af7afb868.png)
2.全局异常实例
1.MyGlobalException.java
package com.sun.web.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class MyGlobalException {
@ExceptionHandler(ArithmeticException.class)
public String globalException(Exception ex, HttpServletRequest request) {
System.out.println("全局异常信息是=" + ex.getMessage());
request.setAttribute("reason", ex.getMessage());
return "global_exception";
}
}
2.global_exception.jsp
Title
出现异常
全局异常信息是${requestScope.reason}
3.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-3865a0675b9133003a18a198613e7a58.png)
3.细节说明
1.局部异常优先级高于全局异常
2.处理异常机制
- 先从发生异常的方法所在的类中查找有
@ExceptionHandler
注解的方法,如果异常不匹配则进行下一步
- 从有
@ControllerAdvice
注解的类查找有@ExceptionHandler
注解的方法,如果匹配异常则处理,无法匹配则交给tomcat处理
4.自定义异常
1.基本介绍
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-67e825478d1c8741d862b68af161c283.png)
2.自定义异常应用实例
1.SelfException.java
package com.sun.web.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(reason = "年龄需要在1-120之间", value = HttpStatus.BAD_REQUEST)
public class SelfException extends RuntimeException{
}
2.selfex.java
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class selfex {
@RequestMapping("/selfex")
public void ex() {
throw new SelfException();
}
}
3.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-5036b4c227d8f7340456299df734ea24.png)
3.可以使用有参构造,抛出异常交给异常处理器接管
1.SelfException.java
package com.sun.web.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(reason = "年龄需要在1-120之间", value = HttpStatus.BAD_REQUEST)
public class SelfException extends RuntimeException{
public SelfException() {
}
public SelfException(String message) {
super(message);
}
}
2.selfex.java抛出自定义异常并制定message
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class selfex {
@RequestMapping("/selfex")
public void ex() {
throw new SelfException("年龄需要在1-100");
}
}
3.MyGlobalException.java捕获自定义异常
package com.sun.web.exception;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
public class MyGlobalException {
@ExceptionHandler(SelfException.class)
public String selfex(Exception ex, HttpServletRequest request) {
request.setAttribute("reason", ex.getMessage());
return "global_exception";
}
}
4.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-c4ecb8865e3d476abba6e2706433c43a.png)
5.统一异常处理器
1.基本介绍
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-53b0f00e4527790663ddda4016d00083.png)
2.需求分析
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-cae50309ff3ac81d7a65e0d837759028.png)
3.具体实现
1.MyExceptionHandler.java抛出数组越界异常
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyExceptionHandler {
@RequestMapping("/arr")
public String array() {
int[] ints = {1, 2, 3};
System.out.println(ints[1222]);
return "success";
}
}
2.springDispatcherServlet-servlet.xml配置统一异常处理器
bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" id="exceptionResolver">
property name="exceptionMappings">
props>
prop key="java.lang.ArrayIndexOutOfBoundsException">arrExprop>
props>
property>
bean>
3.arrEx.jsp异常处理页面
Title
数组越界异常
4.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-31eb50733dd17bc0fd26db7984f03025.png)
6.对未知异常进行统一处理
1.具体实现
1.MyExceptionHandler.java抛出异常
package com.sun.web.exception;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyExceptionHandler {
@RequestMapping("/test")
public String test() {
String str = "hello";
char c = str.charAt(10);
return "success";
}
}
2.springDispatcherServlet-servlet.xml修改统一异常处理器
bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" id="exceptionResolver">
property name="exceptionMappings">
props>
prop key="java.lang.ArrayIndexOutOfBoundsException">arrExprop>
prop key="java.lang.Exception">allExprop>
props>
property>
bean>
3.allEx.jsp所有未知异常的处理
Title
所有未知异常的处理界面
4.结果展示
![](http://www.e-1.cn/wp-content/uploads/2024/05/frc-4360a40b6b629a5ab43791af665fd721.png)
7.异常处理的优先级
1.局部异常处理器
- 方法上加@ExceptionHandler({ArithmeticException.class, NullPointerException.class}) //参数是一个数组,可以接受多个异常类型
2.全局异常处理器
- 类上加@ControllerAdvice //表示这个是处理全局异常的类
- 方法上加@ExceptionHandler({ArithmeticException.class, NullPointerException.class}) //参数是一个数组,可以接受多个异常类型
3.统一异常处理器
- 匹配方式是匹配尽可能具体的类型,跟配置的顺序无关
- 在Spring配置文件中配置
bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" id="exceptionResolver">
property name="exceptionMappings">
props>
prop key="java.lang.ArrayIndexOutOfBoundsException">arrExprop>
prop key="java.lang.Exception">allExprop>
props>
property>
bean>
4.Tomcat默认机制
3.springMVC阶段的配置文件
1.springDispatcherServlet-servlet.xml
beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
context:component-scan base-package="com.sun.web"/>
bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
property name="prefix" value="/WEB-INF/pages/"/>
property name="suffix" value=".jsp"/>
bean>
bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
property name="basename" value="i18n">property>
bean>
bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">bean>
mvc:annotation-driven>mvc:annotation-driven>
mvc:default-servlet-handler/>
mvc:interceptors>
mvc:interceptor>
mvc:mapping path="/topic"/>
ref bean="myinterceptor01"/>
mvc:interceptor>
mvc:interceptors>
bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" id="exceptionResolver">
property name="exceptionMappings">
props>
prop key="java.lang.ArrayIndexOutOfBoundsException">arrExprop>
prop key="java.lang.Exception">allExprop>
props>
property>
bean>
beans>
2.web.xml
web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
filter>
filter-name>CharacterEncodingFilterfilter-name>
filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
init-param>
param-name>encodingparam-name>
param-value>utf-8param-value>
init-param>
filter>
filter-mapping>
filter-name>CharacterEncodingFilterfilter-name>
url-pattern>/*url-pattern>
filter-mapping>
filter>
filter-name>HiddenHttpMethodFilterfilter-name>
filter-class>org.springframework.web.filter.HiddenHttpMethodFilterfilter-class>
filter>
filter-mapping>
filter-name>HiddenHttpMethodFilterfilter-name>
url-pattern>/*url-pattern>
filter-mapping>
servlet>
servlet-name>springDispatcherServletservlet-name>
servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
load-on-startup>1load-on-startup>
servlet>
servlet-mapping>
servlet-name>springDispatcherServletservlet-name>
url-pattern>/url-pattern>
servlet-mapping>
web-app>