终于又要用到这玩意了
过滤器、拦截器、监听器器,一个都不会用
使用拦截器就不能用
<mvc:annotation-driven />开启注解
要用
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
配置文件
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/user/*"/>
<bean class="interceptor.SystemInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
自定义拦截器要继承自HandlerInterceptorAdapter
只需要在三个方法中写好逻辑
package interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
public class SystemInterceptor extends HandlerInterceptorAdapter {
@Override
//页面渲染完之后,要preHandle返回true才会执行
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
// TODO Auto-generated method stub
System.out.println("--------------complete----------");
}
@Override
//在controller执行完之后,可以操作modelAndView
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
// TODO Auto-generated method stub
System.out.println("----------------after-------------");
}
@Override
//执行controller之前,若返回的是false执请求结束
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// TODO Auto-generated method stub
System.out.println("-----------------before----------");
return true;
}
}