use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class MvcUriComponentsBuilder method getWebApplicationContext.
private static WebApplicationContext getWebApplicationContext() {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes == null) {
logger.debug("No request bound to the current thread: not in a DispatcherServlet request?");
return null;
}
HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
WebApplicationContext wac = (WebApplicationContext) request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE);
if (wac == null) {
logger.debug("No WebApplicationContext found: not in a DispatcherServlet request?");
return null;
}
return wac;
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class AbstractDispatcherServletInitializer method registerDispatcherServlet.
/**
* Register a {@link DispatcherServlet} against the given servlet context.
* <p>This method will create a {@code DispatcherServlet} with the name returned by
* {@link #getServletName()}, initializing it with the application context returned
* from {@link #createServletApplicationContext()}, and mapping it to the patterns
* returned from {@link #getServletMappings()}.
* <p>Further customization can be achieved by overriding {@link
* #customizeRegistration(ServletRegistration.Dynamic)} or
* {@link #createDispatcherServlet(WebApplicationContext)}.
* @param servletContext the context to register the servlet against
*/
protected void registerDispatcherServlet(ServletContext servletContext) {
String servletName = getServletName();
Assert.hasLength(servletName, "getServletName() must not return empty or null");
WebApplicationContext servletAppContext = createServletApplicationContext();
Assert.notNull(servletAppContext, "createServletApplicationContext() did not return an application " + "context for servlet [" + servletName + "]");
FrameworkServlet dispatcherServlet = createDispatcherServlet(servletAppContext);
dispatcherServlet.setContextInitializers(getServletApplicationContextInitializers());
ServletRegistration.Dynamic registration = servletContext.addServlet(servletName, dispatcherServlet);
Assert.notNull(registration, "Failed to register servlet with name '" + servletName + "'." + "Check if there is another servlet registered under the same name.");
registration.setLoadOnStartup(1);
registration.addMapping(getServletMappings());
registration.setAsyncSupported(isAsyncSupported());
Filter[] filters = getServletFilters();
if (!ObjectUtils.isEmpty(filters)) {
for (Filter filter : filters) {
registerServletFilter(servletContext, filter);
}
}
customizeRegistration(registration);
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class DispatcherServletTests method cleanupAfterIncludeWithRemove.
@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setAttribute("test1", "value1");
request.setAttribute("test2", "value2");
WebApplicationContext wac = new StaticWebApplicationContext();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
simpleDispatcherServlet.service(request, response);
assertEquals("value1", request.getAttribute("test1"));
assertEquals("value2", request.getAttribute("test2"));
assertEquals(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
assertNull(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
assertNull(request.getAttribute("command"));
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class DispatcherServletTests method noCleanupAfterInclude.
@Test
public void noCleanupAfterInclude() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
MockHttpServletResponse response = new MockHttpServletResponse();
request.setAttribute("test1", "value1");
request.setAttribute("test2", "value2");
WebApplicationContext wac = new StaticWebApplicationContext();
request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
TestBean command = new TestBean();
request.setAttribute("command", command);
request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
simpleDispatcherServlet.setCleanupAfterInclude(false);
simpleDispatcherServlet.service(request, response);
assertEquals("value1", request.getAttribute("test1"));
assertEquals("value2", request.getAttribute("test2"));
assertSame(wac, request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE));
}
use of org.springframework.web.context.WebApplicationContext in project spring-framework by spring-projects.
the class CglibProxyControllerTests method initServlet.
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerClass) throws ServletException {
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBeanDefinition("controller", new RootBeanDefinition(controllerClass));
DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
autoProxyCreator.setProxyTargetClass(true);
autoProxyCreator.setBeanFactory(wac.getBeanFactory());
wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator);
wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor(true)));
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
}
Aggregations