RequestAttributes ra = RequestContextHolder.getRequestAttributes(); HttpServletRequest request = ((ServletRequestAttributes)ra).getRequest(); request.getSession().setAttribute("sessionMessage", "im'sessionMessage!");
也可以让所有的Control继承一个BaseControl类 ,BaseControl类的定义为:
public class BaseControl{
protected HttpServletRequest request ;
protected HttpServletResponse response ;
protected HttpSession session ;
// setter ... getter...
//.......
}
@Component("SpringMVCInterceptor")
public class SpringMVCInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
BaseControl bc = (BaseController)handler ;
bc.setRequest(request);
bc .setResponse(response);
bc.set Session(request.getSession());
return super.preHandle(request, response, handler);
}
这样 ,每个Control就能够方便的得到Servlet API 中的对象了。