要使用Spring的request ,session ,global session作用域bean,需要在web.xml中做如下配置:
1、如果使用Spring Web MVC,即使用SpringDispatcherServlet或DispatcherPorlet来处理请求,此时不需要做额外的
配置,上述SpringDispatcherServlet和DispatcherPorlet已经处理了有关状态。
2、使用Servlet2.4及以上的web容器时,需做如下配置:
<web-app> ... <listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> </listener> </web-app>
3、使用Servlet2.4以下版本需做如下配置:
<web-app> ... <filter> <filter-name>requestContextFilter</filter-name> <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class> </filter> <filter-mapping> <filter-name>requestContextFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mappiing> </web-app>
RequestContextListener和RequestContextFilter所做的处理都是一样的:将Http Request对象绑定到为该请求
提供服务的Thread。这使得具有request和session作用域的bean能够在后面的调用链中被访问到。