<mvc:interceptor>
<!--进行拦截的地址-->
<mvc:mapping path="/**"/>
<bean/>
</mvc:interceptor>
<mvc:interceptor>
<!--进行拦截的地址-->
<mvc:mapping path="/**"/>
<bean/>
</mvc:interceptor>
</mvc:interceptors>
按 Ctrl+C
java 代码:
按 Ctrl+C
package com.yule.common.interceptors;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器方式一
* Created by yule on 2018/7/2 22:37.
*/
public class DemoInterceptor implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
//return true 表示继续下一个拦截器或者 control 层
//return false 表示被拦截下来
System.out.println("preHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle");
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
System.out.println("afterCompletion");
}
}
按 Ctrl+C
package com.yule.common.interceptors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 自定义拦截器方式二
* 一般都是通过实现HandlerInterceptor接口或者继承HandlerInterceptorAdapter抽象类,复写preHandle()、postHandle()和afterCompletion()这 3 个方法来对用户的请求进行拦截处理
* Created by yule on 2018/7/2 22:43.
*/
public class Demo2Interceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
System.out.println("preHandle 2222222...");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle 22222222");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("afterCompletion 2222222");
}
}
过滤器, web.xml 中:
按 Ctrl+C
<!-- 定义Filter -->
<filter>
<!-- Filter的名字 -->
<filter-name>demoFilter</filter-name>
<!-- Filter的实现类 -->
<filter-class>com.yule.common.filters.DemoFilter</filter-class>
</filter>
<!-- 定义Filter拦截的URL地址 -->
<filter-mapping>
<!-- Filter的名字 -->
<filter-name>demoFilter</filter-name>
<!-- Filter负责拦截的URL 全部以/的请求,如果/*,将会所有的请求-->
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>demo2Filter</filter-name>
<filter-class>com.yule.common.filters.Demo2Filter</filter-class>
</filter>
<filter-mapping>
<filter-name>demo2Filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
按 Ctrl+C
java 代码:
package com.yule.common.filters;
import javax.servlet.*;
import java.io.IOException;