一,实现HandlerInterceptor接口
package org.spoto.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class loginedInterceptor implements HandlerInterceptor {
/**
*
* @param request 请求
* @param response 响应
* @param handler 处理头部
* @return true放行,false拦截
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//业务代码
return true;
}
}
二,在springMVC的配置文件中配置
<!-- 拦截器配置 -->
<mvc:interceptors>
<!-- 一个拦截器(可配置多个) -->
<mvc:interceptor>
<mvc:mapping path="/**"/>
<!-- 排除哪些路径,即拦截器不处理的路径 -->
<mvc:exclude-mapping path="/3rd/**"/>
<mvc:exclude-mapping path="/login.html"/>
<mvc:exclude-mapping path="/favicon.ico"/>
<mvc:exclude-mapping path="/dologin"/>
<!-- 配置拦截器的全路径 -->
<bean class="org.spoto.interceptor.loginedInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
<mvc:mapping path=“”/>中的path如果为/* ,可以匹配/aa(匹配一级)不能匹配/aa/bb ,而如果为/**可匹配所有路径。