Detailed explanation of SpringMVC interceptor and SpringMVC exception handling mechanism

SpringMVC Interceptors

The role of the interceptor

The interceptor of Spring MVC is similar to the filter Filter in Servlet development, which is used for preprocessing and postprocessing of the processor.
The interceptors are linked in a certain order into a chain, which is called the Interceptor Chain. When an intercepted method or field is accessed, the interceptors in the interceptor chain are called in the order in which they were previously defined. Interceptor is also a concrete realization of AOP idea.
 

The difference between interceptors and filters

insert image description here
 

Interceptor Quick Start

Custom interceptor is very simple, there are only three steps as follows:
① Create an interceptor class to implement the HandlerInterceptor interface
② Configure the interceptor
③ Test the interception effect of the interceptor

① Create an interceptor class to implement the HandlerInterceptor interface

public class MyHandlerInterceptor1 implements HandlerInterceptor {
    
    
public boolean preHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler) {
    
    
System.out.println("preHandle running...");
return true; }
public void postHandle(HttpServletRequest request, HttpServletResponse 
response, Object handler, ModelAndView modelAndView) {
    
    
System.out.println("postHandle running...");
}
public void afterCompletion(HttpServletRequest request, HttpServletResponse 
response, Object handler, Exception ex) {
    
    
System.out.println("afterCompletion running...");
} }

② Configure the interceptor

<!--配置拦截器--> 
<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/**"/>
<bean class="com.itheima.interceptor.MyHandlerInterceptor1"/>
</mvc:interceptor>
</mvc:interceptors>

③ Test the interception effect of the interceptor (write the target method)

@RequestMapping("/quick23") @ResponseBody
public ModelAndView quickMethod23() throws IOException, ParseException {
    
    
System.out.println("目标方法执行....");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name","itcast");
modelAndView.setViewName("index");
return modelAndView; }

 

Interceptor method description

insert image description here

 
 

SpringMVC exception handling

Exception handling ideas

There are two types of exceptions in the system: expected exceptions and runtime exceptions , RuntimeException. The former obtains exception information by catching exceptions, and the latter mainly reduces the occurrence of runtime exceptions by standardizing code development and testing.

The Dao, Service, and Controller of the system are all thrown upward through throws Exception, and finally the SpringMVC front-end controller is handed over to the exception handler for exception handling, as shown in the following figure:
insert image description here
 
 

Two ways of exception handling

1. Use the simple exception handler SimpleMappingExceptionResolver provided by Spring MVC
2. Implement Spring's exception handling interface HandlerExceptionResolver to customize your own exception handler
 

Simple exception handler SimpleMappingExceptionResolver

SpringMVC has defined the type converter, and can map the corresponding exception and view according to the project situation when using it.

<!--配置简单映射异常处理器-->
 <bean 
class=org.springframework.web.servlet.handler.SimpleMappingExceptionResolver> <property name=“defaultErrorView” value=“error”/> //默认错误视图
<property name=“exceptionMappings”> <map> //异常类型 错误视图
<entry key="com.itheima.exception.MyException" value="error"/>
<entry key="java.lang.ClassCastException" value="error"/>
</map>
</property>
</bean>

 

Custom exception handling steps

① Create exception handler class to implement HandlerExceptionResolver
② Configure exception handler
③ Write exception page
④ Test exception jump

① Create an exception handler class to implement HandlerExceptionResolver

public class MyExceptionResolver implements HandlerExceptionResolver {
    
    
@Override
public ModelAndView resolveException(HttpServletRequest request, 
HttpServletResponse response, Object handler, Exception ex) {
    
    
//处理异常的代码实现
//创建ModelAndView对象
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("exceptionPage");
return modelAndView;
} }

 

② Configure exception handler

<bean id="exceptionResolver" 
class="com.itheima.exception.MyExceptionResolver"/>

 

③ Write an exception page

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html> <head><title>Title</title>
</head> <body>
这是一个最终异常的显示页面
</body>
</html>

 

④ Test exception jump

@RequestMapping("/quick22")
@ResponseBody
public void quickMethod22() throws IOException, ParseException {
    
    
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
simpleDateFormat.parse("abcde");
}

Guess you like

Origin blog.csdn.net/JIAYOUYAA/article/details/124253244