1、创建Java代码
package com.yacol.kzt.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.yacol.kzt.utils.LogUtil;
import com.yacol.kzt.utils.UtilMethods;
public class LogInterceptor extends HandlerInterceptorAdapter{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
Object handle) throws Exception {
LogUtil.info("拦截器测试===============================");
System.err.println("拦截器测试=============================================");
System.err.println("拦截器测试=============================================");
System.err.println("拦截器测试=============================================");
String requestUrl=UtilMethods.genRequestString(request);
LogUtil.infoBusiness(requestUrl);
return true;
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handle, Exception exception)
throws Exception {
// TODO Auto-generated method stub
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response,
Object arg2, ModelAndView arg3) throws Exception {
// TODO Auto-generated method stub
}
}
2、在spring-mvc.xml中配置
配置文件别配错了。
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean id="logInterceptor" class="com.yacol.kzt.interceptor.LogInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>