Java代码部分
package interceptors;
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 {
//进入Handler路径之前拦截
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("preHandle...");
return true;
}
//在return视图之前拦截
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
System.out.println("postHandle...");
}
//转发视图后拦截
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
System.out.println("afterCompletion...");
}
}
SpringMVC配置文件中的部分
<!-- 配置过滤器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/person/test1"/>
<bean class="interceptors.MyInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>