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);
}
时间: 2024-11-08 21:22:28