5_攔截器
建立攔截器
package com.happy.middleware;
-
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
-
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-
-
public class InterceptorConfig implements HandlerInterceptor {
-
-
/**
* 進入controller層以前攔截請求
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
-
System.out.print("攔截啦");
-
return true;
}
-
-
/**
* 處理請求完成後視圖渲染以前的處理操做
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param modelAndView
* @throws Exception
*/
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
-
}
-
/**
* 視圖渲染以後的操做
*
* @param httpServletRequest
* @param httpServletResponse
* @param o
* @param e
* @throws Exception
*/
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
-
}
}
2. 使用攔截器
package com.happy.middleware;
-
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
-
/**
* Created by xiao on 2018/2/22.
*/
-
@Configuration
public class MyWebAppConfigurer extends WebMvcConfigurerAdapter {
-
@Override
public void addInterceptors(InterceptorRegistry registry) {
-
registry.addInterceptor(new InterceptorConfig()).addPathPatterns("/**");
-
super.addInterceptors(registry);
}
}
3. 控制檯數據【訪問連接以後】
歡迎關注本站公眾號,獲取更多信息