文章目录 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.说明
2.自定义拦截器的三个方法
3.流程图
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.单元测试
3.拦截特定路径
1.拦截指定路径
< mvc: interceptors> < mvc: interceptor> < mvc: mapping path = " /hi" /> < ref bean = " myinterceptor01" /> </ mvc: interceptor> </ mvc: interceptors>
2.通配符配置路径
< mvc: interceptors> < mvc: interceptor> < mvc: mapping path = " /h*" /> < mvc: exclude-mapping path = " /hi" /> < ref bean = " myinterceptor01" /> </ mvc: interceptor> </ mvc: interceptors>
4.细节说明
5.多个拦截器
1.执行流程图
2.应用实例
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 ( "<h1>请不要乱说话!!</h1>" ) ; } return false ; }
3.结果展示
2.异常处理
1.基本介绍
2.局部异常处理器
1.需求分析
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处理
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
<%--Date: 2024/3/1Time: 8:47User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h3>出现异常</h3>
<h4>异常信息是${requestScope.reason}</h4>
</body>
</html>
3.结果展示
3.全局异常处理器
1.基本介绍
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
<%--Date: 2024/3/1Time: 9:02User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h3>出现异常</h3>
<h4>全局异常信息是${requestScope.reason}</h4>
</body>
</html>
3.结果展示
3.细节说明
1.局部异常优先级高于全局异常
2.处理异常机制
先从发生异常的方法所在的类中查找有@ExceptionHandler
注解的方法,如果异常不匹配则进行下一步 从有@ControllerAdvice
注解的类查找有@ExceptionHandler
注解的方法,如果匹配异常则处理,无法匹配则交给tomcat处理
4.自定义异常
1.基本介绍
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.结果展示
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.结果展示
5.统一异常处理器
1.基本介绍
2.需求分析
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" > arrEx</ prop> </ props> </ property> </ bean>
3.arrEx.jsp异常处理页面
<%--Date: 2024/3/1Time: 10:02User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>数组越界异常</h1>
</body>
</html>
4.结果展示
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" > arrEx</ prop> < prop key = " java.lang.Exception" > allEx</ prop> </ props> </ property> </ bean>
3.allEx.jsp所有未知异常的处理
<%--Date: 2024/3/1Time: 10:12User: 孙显圣Version:1.0
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Title</title>
</head>
<body>
<h1>所有未知异常的处理界面</h1>
</body>
</html>
4.结果展示
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" > arrEx</ prop> < prop key = " java.lang.Exception" > allEx</ prop> </ props> </ property> </ bean>
4.Tomcat默认机制
3.springMVC阶段的配置文件
1.springDispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
< 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" > arrEx</ prop> < prop key = " java.lang.Exception" > allEx</ prop> </ props> </ property> </ bean>
</ beans>
2.web.xml
<?xml version="1.0" encoding="UTF-8"?>
< 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> CharacterEncodingFilter</ filter-name> < filter-class> org.springframework.web.filter.CharacterEncodingFilter</ filter-class> < init-param> < param-name> encoding</ param-name> < param-value> utf-8</ param-value> </ init-param> </ filter> < filter-mapping> < filter-name> CharacterEncodingFilter</ filter-name> < url-pattern> /*</ url-pattern> </ filter-mapping> < filter> < filter-name> HiddenHttpMethodFilter</ filter-name> < filter-class> org.springframework.web.filter.HiddenHttpMethodFilter</ filter-class> </ filter> < filter-mapping> < filter-name> HiddenHttpMethodFilter</ filter-name> < url-pattern> /*</ url-pattern> </ filter-mapping> < servlet> < servlet-name> springDispatcherServlet</ servlet-name> < servlet-class> org.springframework.web.servlet.DispatcherServlet</ servlet-class> < load-on-startup> 1</ load-on-startup> </ servlet> < servlet-mapping> < servlet-name> springDispatcherServlet</ servlet-name> < url-pattern> /</ url-pattern> </ servlet-mapping> </ web-app>