我们要在springmvc中写自己的拦截器,那么就要继承HandlerInterceptor。
HandlerInterceptor提供了3个方法
1.preHandle
在跳转controller之前拦截,在这里可以写具体的拦截业务。
2.postHandle
在跳转controler之后,返回modleandview之前拦截。
3.afterCompletion
在返回modleandview之后做的处理。
springmvc.xml
<mvc:interceptors>
<mvc:interceptor>
<!-- 拦截的路径-->
<mvc:mapping path="/**" />
<!-- 字符集拦截-->
<bean class="org.jeecgframework.core.interceptors.EncodingInterceptor" />
</mvc:interceptor>
<mvc:interceptor>
<!-- 拦截的路径-->
<mvc:mapping path="/**" />
<!-- 登录拦截-->
<bean class="com.hlmedicals.app.core.interceptors.AuthInterceptor">
<property name="excludeUrls">
<list>
<!-- 不拦截到url-->
<value>hisPatient.do?shownew</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>
java 拦截器代码
/**
* 字符集拦截器
*
* @author 张代浩
*
*/
public class EncodingInterceptor implements HandlerInterceptor {
/**
* 在controller后拦截
*/
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) throws Exception {
}
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object object, ModelAndView modelAndView) throws Exception {
}
/**
* 在controller前拦截
*/
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
return true;
}
}