There are two types of exceptions in the system: Expected exception and runtime exception RuntimeExceptionThe former obtains exception information by catching exceptions, while the latter mainly reduces the occurrence of exceptions at runtime by standardizing code development and testing.
The dao, service, and controller of the system are all thrown upwards 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:
The processor interceptor of Spring MVC is similar to the filter in Servlet development, which is used to pre-process and post-process the processor.
Users can define some interceptors to achieve specific functions.
Speaking of interceptors, I would like to mention one word to everyone-Interceptor Chain. The interceptor chain is to link the interceptors into a chain in a certain order. When accessing the intercepted method or field, the interceptors in the interceptor chain will be called in the order they were previously defined.
Having said that, you may have a question in your mind. Isn't this the filter we learned before? Yes, it is somewhat similar to filters, but there are also differences. Next, let's talk about their differences:
filterIt is part of the servlet specification and can be used by any java web project.
InterceptorIt is owned by the SpringMVC framework and can only be used for projects that use the SpringMVC framework.
filterAfter configuring /* in url-pattern, all resources to be accessed can be intercepted.
InterceptorIt is a controller method that only intercepts access. If the access is jsp, html, css, image or js, it will not be intercepted.
If we want to customize the interceptor, the requirements must be implemented: HandlerInterceptor interface。
38.5.2 Getting Started with Interceptors
1. Create a class, implement the HandlerInterceptor interface, and rewrite the required method