您的位置:首页 > 编程语言 > Java开发

spring 学习1-Request, session, and global session scopes|(Initial web configuration)

2014-11-05 14:52 447 查看


Initial web configuration

To support the scoping of beans at the
request
,
session
,
and
global session
levels
(web-scoped beans), some minor initial configuration is required before you define your beans. (This initial setup is not required for the standard scopes, singleton and prototype.)

How you accomplish this initial setup depends on your particular Servlet environment..

If you access scoped beans within Spring Web MVC, in effect, within a request that is processed by the Spring
DispatcherServlet
,
or
DispatcherPortlet
,
then no special setup is necessary:
DispatcherServlet
and
DispatcherPortlet
already
expose all relevant state.

If you use a Servlet 2.5 web container, with requests processed outside of Spring’s DispatcherServlet (for example, when using JSF or Struts), you need to register the
org.springframework.web.context.request.RequestContextListener
ServletRequestListener
.
For Servlet 3.0+, this can done programmatically via the
WebApplicationInitializer
interface.
Alternatively, or for older containers, add the following declaration to your web application’s
web.xml
file:
<web-app>
...
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
...
</web-app>


Alternatively, if there are issues with your listener setup, consider the provided
RequestContextFilter
.
The filter mapping depends on the surrounding web application configuration, so you have to change it as appropriate.
<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-mapping>
...
</web-app>


DispatcherServlet
,
RequestContextListener
and
RequestContextFilter
all
do exactly the same thing, namely bind the HTTP request object to the
Thread
that
is servicing that request. This makes beans that are request- and session-scoped available further down the call chain.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: