一:创建一个基本控制器类:
public class BaseController extends
HandlerInterceptorAdapter{
public static HttpServletRequest
request;
public static HttpServletResponse
reponse;
public static HttpSession session;
@Override
public boolean preHandle(HttpServletRequest
request,
HttpServletResponse
response, Object handler) throws Exception {
BaseController.request
= request;
BaseController.reponse
= response;
BaseController.session
= request.getSession();
return
super.preHandle(request, response, handler);
}
@InitBinder
protected void initBinder(WebDataBinder binder)
{
binder.registerCustomEditor(Date.class,
new CustomDateEditor(
new
SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
}
}
二:注册这个实现拦截器的主控制器:(在子mvc配置文件.xml
中实现)
1
拦截所有请求
<!-- 注册拦截器
-->
<mvc:interceptors>
<bean
class="com.base.BaseController"></bean>
</mvc:interceptors>
2 拦截指定请求
<!-- 注册拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping
path="/*" />
<bean
class="com.base.BaseController"></bean>
</mvc:interceptor>
</mvc:interceptors>
三
所有的控制器可以实现这个基类,从而访问request、respons、session
个人理解:由于实现拦截器功能的bean
并发单态、或者控制器并非单态,所以每次请求的static request 并不是同一个对象