Interceptor Summary

Interceptor intercepts the specified request, and performing the method wherein the. And source code examples from the point of view of the operation and effect of the interceptor.

Create an interceptor class MyInterceptor:

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class MyInterceptor implements HandlerInterceptor {

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.println("preHandle");
		return true;
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("postHandle");
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, 
			Exception ex) throws Exception {
		System.out.println("afterCompletion");
	}
}

The three methods are within the interceptors add a statement output method name, return value and preHandle method set true; interceptor arranged after the application.xml; There are two configurations:

Intercepts all requests;

<mvc:interceptors>
	<bean class="com.jd.interceptor.MyInterceptor"></bean>
</mvc:interceptors>

So configured interceptor will intercept all requests processed, wherein the bean class is the path we interceptor class; such a configuration is generally not recommended, consuming resources;

Intercept specified request:

<mvc:interceptors>
	<mvc:interceptor>
		<mvc:mapping path="/userinfo/*"/>
		<bean class="com.jd.interceptor.MyInterceptor"></bean>
	</mvc:interceptor>
</mvc:interceptors>

This configuration will intercept only mvc: mapping request to a label path; below, this will intercept all requests access requests userinfo folder jsp all files;

Look Controller code;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserInfoController {

	@RequestMapping("userinfo/div.do")
	public String div(int i) {
		System.out.println("div");
		return "userinfo/div";
	}
}

The process then performs interceptor viewed from the source (Debug Mode):

The specific process execution request of my previous blog has been introduced, so here directly from 

This code would call theme FIG. 1 (hereinafter, will be mentioned several times); performed when the number 1, mappedHandler (our Handler processor, and rise to the following figure mouse mappedHandler) performs applyPreHandle method;

We look at the code applyPreHandle method:

This method will traverse all of the interceptor, the interceptor and find what we need here is clearly written in our own MyInterceptor interceptors and perform its methods:

That is the way we perHandler MyInterceptor class, and the output string, returns true; Back layer Back to FIG. 1, if it is determined true statement is false, the method is not performed return, execution continues down;! Mv = ha.handle ( processedRequest, response, mappedHandler.getHandler ()); mv acquired data and then execute the code 2; go applyPostHandle method, as follows:

同上,依旧对拦截器进行遍历找到需要的拦截器并执行相应的 postHandle 方法;这时控制台便会有 postHandle 语句输出;然后执行 3 代码进入 processDispatchResult 方法,代码如下;

执行到 4,完成请求响应和转发,继续执行到 5 进入 triggerAfterCompletion 方,代码如下:

同样也是找到需要的拦截器并执行相应的 afterCompletion 方法,这时控制台会输出语句 afterCompletion ;执行完程序(后续没有断相关拦截器的了);控制台显示以下输出:

至此,我们可以发现,拦截器中:

  • perHandle 方法是在方法执行之前执行,这里我们可以先对请求进行处理,阻挡非法访问;
  • postHandler 方法是在方法执行之后执行,这里我们可以先对后台服务器需要的数据进行相应的处理,比如添加 UUID 的自动编码等;
  • afterCompletion 方法是在请求转发之后执行,常用来释放资源,类似于 try-catch-fianlly 中的 finally 作用。
发布了99 篇原创文章 · 获赞 3 · 访问量 1199

Guess you like

Origin blog.csdn.net/qq_44971038/article/details/104571889