Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement a custom interceptor with Java SpringMVC

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article focuses on "how to implement a custom interceptor in Java SpringMVC". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to implement a custom interceptor with Java SpringMVC".

SpringMVC implements the role of custom interceptor 1 interceptor (interceptor)

Spring MVC's interceptor is similar to the filter Filter in Servlet development and is used for pre-processing and post-processing of processors.

The interceptor is connected into a chain in a certain order, which is called the interceptor chain (InterceptorChain). When you access an intercepted method or field, the interceptors in the interceptor chain are called in the order they were previously defined. Interceptor is also a concrete implementation of AOP idea.

2 the difference between interceptor and filter

The difference between interceptor and filter is shown in the figure:

3. Realization process

Step analysis:

1. Create interceptor class to implement HandlerInterceptor interface

two。 Configure interceptor

3. Test the interception effect of the interceptor

Create interceptor class to implement HandlerInterceptor interface public class MyInterceptor implements HandlerInterceptor {/ * preHandle: intercept return false before target method execution: do not release * / @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println ("preHandle1...."); return true } / * postHandle: after the target method is executed and before the view object returns, the executed method * / @ Override public void postHandle (HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {System.out.println ("postHandle1....") } / * afterCompletion: after the process has been executed, the method * / @ Override public void afterCompletion (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {System.out.println ("afterCompletion1....") RequestMapping ("/ target") public String targetMethed () {System.out.println ("target method executed"); return "interceptor";} 3.4 write jsp page Title success~ 3.5 test results

4. Interceptor chain

Interceptors can be used alone in development, or multiple interceptors can be used at the same time to form an interceptor chain. The development steps are the same as a single interceptor, except that multiple interceptors are registered. Note that the order in which the interceptor is registered represents the order in which the interceptor is executed.

As above, write another MyHandlerInterceptor2 operation to test the execution order:

At this point, I believe you have a deeper understanding of "how to implement a custom interceptor in Java SpringMVC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 258

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report