org.springframework.web.context.request
public class: RequestContextListener [javadoc |
source]
java.lang.Object
org.springframework.web.context.request.RequestContextListener
All Implemented Interfaces:
ServletRequestListener
Servlet 2.4+ listener that exposes the request to the current thread,
through both
org.springframework.context.i18n.LocaleContextHolder and
RequestContextHolder . To be registered as listener in
web.xml.
Alternatively, Spring's org.springframework.web.filter.RequestContextFilter
and Spring's org.springframework.web.servlet.DispatcherServlet also expose
the same request context to the current thread. In contrast to this listener,
advanced options are available there (e.g. "threadContextInheritable").
This listener is mainly for use with third-party servlets, e.g. the JSF FacesServlet.
Within Spring's own web support, DispatcherServlet's processing is perfectly sufficient.
| Field Summary |
|---|
| protected final Log | logger | Logger available to subclasses |
| Method from org.springframework.web.context.request.RequestContextListener Detail: |
public void requestDestroyed(ServletRequestEvent requestEvent) {
ServletRequestAttributes attributes =
(ServletRequestAttributes) requestEvent.getServletRequest().getAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE);
ServletRequestAttributes threadAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (threadAttributes != null) {
// We're assumably within the original request thread...
if (attributes == null) {
attributes = threadAttributes;
}
RequestContextHolder.resetRequestAttributes();
LocaleContextHolder.resetLocaleContext();
}
if (attributes != null) {
attributes.requestCompleted();
if (logger.isDebugEnabled()) {
logger.debug("Cleared thread-bound request context: " + requestEvent.getServletRequest());
}
}
}
|
public void requestInitialized(ServletRequestEvent requestEvent) {
if (!(requestEvent.getServletRequest() instanceof HttpServletRequest)) {
throw new IllegalArgumentException(
"Request is not an HttpServletRequest: " + requestEvent.getServletRequest());
}
HttpServletRequest request = (HttpServletRequest) requestEvent.getServletRequest();
ServletRequestAttributes attributes = new ServletRequestAttributes(request);
request.setAttribute(REQUEST_ATTRIBUTES_ATTRIBUTE, attributes);
LocaleContextHolder.setLocale(request.getLocale());
RequestContextHolder.setRequestAttributes(attributes);
if (logger.isDebugEnabled()) {
logger.debug("Bound request context to thread: " + request);
}
}
|