写一个spring拦截器
时间: 2023-05-25 18:03:33 浏览: 78
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 {
/**
* 在请求处理之前进行调用(Controller方法调用之前)
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("在请求处理之前进行调用");
return true; // 只有返回true才会继续向下执行,返回false取消当前请求
}
/**
* 请求处理之后进行调用(Controller方法调用之后)
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("请求处理之后进行调用");
}
/**
* 请求处理完成后进行调用,一般用于资源清理操作
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("请求处理完成后进行调用");
}
}
```
阅读全文