第六章:如何在SpringBoot项目中使用拦截器
2018-07-03 · 国内最优秀java资源共享平台
拦截器接口 HandlerInterceptor
public interface HandlerInterceptor {
boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception;
void postHandle(
HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception;
void afterCompletion(
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception;
}public class Myfilter implements HandlerInterceptor{
/**
* 页面渲染之后调用,一般用于资源清理操作
*/
@Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("------afterCompletion-----");
}
/**
* controller 执行之后,且页面渲染之前调用
*/
@Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
// TODO Auto-generated method stub
System.out.println("------postHandle-----");
}
/**
* controller 执行之前调用
*/
@Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
// TODO Auto-generated method stub
System.out.println("------preHandle-----");
return true;
}
}
3.//将Myfilter拦截器放到SpringBoot的配置中@GetMapping("/www/ijava")
public String home(){
System.out.println("--- wwww . ijava ---");
return "user home";
}