@Override public void onStartup(ServletContext servletContext) throws ServletException { //register config classes AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(WebMvcConfig.class); rootContext.register(JPAConfig.class); rootContext.register(WebSecurityConfig.class); rootContext.register(ServiceConfig.class); //set session timeout servletContext.addListener(new SessionListener(maxInactiveInterval)); //set dispatcher servlet and mapping ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); //register filters FilterRegistration.Dynamic filterRegistration = servletContext.addFilter("endcodingFilter", new CharacterEncodingFilter()); filterRegistration.setInitParameter("encoding", "UTF-8"); filterRegistration.setInitParameter("forceEncoding", "true"); //make sure encodingFilter is matched first filterRegistration.addMappingForUrlPatterns(null, false, "/*"); //disable appending jsessionid to the URL filterRegistration = servletContext.addFilter("disableUrlSessionFilter", new DisableUrlSessionFilter()); filterRegistration.addMappingForUrlPatterns(null, true, "/*"); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch08-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); dispatcher.setAsyncSupported(true); //FilterRegistration.Dynamic springSecurityFilterChain = container.addFilter("springSecurityFilterChain", new DelegatingFilterProxy()); // springSecurityFilterChain.addMappingForUrlPatterns(null, false, "/*"); // springSecurityFilterChain.setAsyncSupported(true); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); FilterRegistration.Dynamic corsFilter = container.addFilter("corsFilter", new CorsFilter()); corsFilter.setInitParameter("cors.allowed.methods", "GET, POST, HEAD, OPTIONS, PUT, DELETE"); corsFilter.addMappingForUrlPatterns(null, true, "/*"); FilterRegistration.Dynamic filter = container.addFilter("hiddenmethodfilter", new HiddenHttpMethodFilter()); filter.addMappingForServletNames(null, true, "/*"); FilterRegistration.Dynamic multipartFilter = container.addFilter("multipartFilter", new MultipartFilter()); multipartFilter.addMappingForUrlPatterns(null, true, "/*"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { WebApplicationContext context = getContext(); servletContext.addListener(new ContextLoaderListener(context)); servletContext.addFilter("characterEncodingFilter", new CharacterEncodingFilter("UTF-8")); DispatcherServlet dispatcherServlet = new DispatcherServlet(context); dispatcherServlet.setThrowExceptionIfNoHandlerFound(true); ServletRegistration.Dynamic dispatcher = servletContext.addServlet("Dispatcher", dispatcherServlet); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/*"); CXFServlet cxf = new CXFServlet(); BusFactory.setDefaultBus(cxf.getBus()); ServletRegistration.Dynamic cxfServlet = servletContext.addServlet("CXFServlet", cxf); cxfServlet.setLoadOnStartup(1); cxfServlet.addMapping("/services/*"); servletContext.addFilter("springSecurityFilterChain", new DelegatingFilterProxy("springSecurityFilterChain")).addMappingForUrlPatterns(null, false, "/*"); servletContext.getSessionCookieConfig().setSecure(cookieSecure); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { //On charge le contexte de l'app AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.setDisplayName("scrumtracker"); rootContext.register(ApplicationContext.class); //Context loader listener servletContext.addListener(new ContextLoaderListener(rootContext)); //Dispatcher servlet ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
/** * Configure the given {@link ServletContext} with any servlets, filters, listeners * context-params and attributes necessary for initializing this web application. See examples * {@linkplain WebApplicationInitializer above}. * * @param servletContext the {@code ServletContext} to initialize * @throws ServletException if any call against the given {@code ServletContext} throws a {@code ServletException} */ public void onStartup(ServletContext servletContext) throws ServletException { // Spring Context Bootstrapping AnnotationConfigWebApplicationContext rootAppContext = new AnnotationConfigWebApplicationContext(); rootAppContext.register(AutoPivotConfig.class); servletContext.addListener(new ContextLoaderListener(rootAppContext)); // Set the session cookie name. Must be done when there are several servers (AP, // Content server, ActiveMonitor) with the same URL but running on different ports. // Cookies ignore the port (See RFC 6265). CookieUtil.configure(servletContext.getSessionCookieConfig(), CookieUtil.COOKIE_NAME); // The main servlet/the central dispatcher final DispatcherServlet servlet = new DispatcherServlet(rootAppContext); servlet.setDispatchOptionsRequest(true); Dynamic dispatcher = servletContext.addServlet("springDispatcherServlet", servlet); dispatcher.addMapping("/*"); dispatcher.setLoadOnStartup(1); // Spring Security Filter final FilterRegistration.Dynamic springSecurity = servletContext.addFilter(SPRING_SECURITY_FILTER_CHAIN, new DelegatingFilterProxy()); springSecurity.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*"); }
@Override public void onStartup(ServletContext container) { // Create the 'root' Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.register(AppConfig.class); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container .addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(MyMvcConfig.class); ctx.setServletContext(servletContext); // ② Dynamic servlet = servletContext.addServlet("dispatcher",new DispatcherServlet(ctx)); // 3 servlet.addMapping("/"); servlet.setLoadOnStartup(1); }
@Override public void onStartup(ServletContext servletContext) throws ServletException { int majorVersion = servletContext.getMajorVersion(); int minorVersion = servletContext.getMinorVersion(); LOG.info("Container servlet version is {}.{}", majorVersion, minorVersion); int effectiveMajorVersion = servletContext.getEffectiveMajorVersion(); int effectiveMinorVersion = servletContext.getEffectiveMinorVersion(); LOG.info("Application servlet effective version is {}.{}", effectiveMajorVersion, effectiveMinorVersion); Map<String, ? extends ServletRegistration> map = servletContext.getServletRegistrations(); Collection<? extends ServletRegistration> registrations = map.values(); for (ServletRegistration registration : registrations) { String className = registration.getClassName(); if (DispatcherServlet.class.getCanonicalName().equals(className)) { LOG.info("DispatcherServlet has been initialized"); return; } } super.onStartup(servletContext); }
private ServletRegistration.Dynamic addDispatcherServlet(ServletContext servletContext, String servletName, Class<?>[] servletContextConfigClasses, boolean allowBeanDefinitionOverriding, String... mappings) { Assert.notNull(servletName); Assert.notEmpty(servletContextConfigClasses); Assert.notEmpty(mappings); AnnotationConfigWebApplicationContext servletApplicationContext = new AnnotationConfigWebApplicationContext(); servletApplicationContext.setAllowBeanDefinitionOverriding(allowBeanDefinitionOverriding); servletApplicationContext.register(servletContextConfigClasses); ServletRegistration.Dynamic dispatcherServlet = servletContext.addServlet(servletName, new DispatcherServlet(servletApplicationContext)); dispatcherServlet.setLoadOnStartup(1); dispatcherServlet.addMapping(mappings); return dispatcherServlet; }
@Test public void dispatcherServletDefaultConfig() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); this.context.refresh(); DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); assertThat(bean).extracting("throwExceptionIfNoHandlerFound") .containsExactly(false); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true); assertThat(bean).extracting("dispatchTraceRequest").containsExactly(false); assertThat(new DirectFieldAccessor( this.context.getBean("dispatcherServletRegistration")) .getPropertyValue("loadOnStartup")).isEqualTo(-1); }
private void createMainDispatcherServlet(ApplicationContext rootContext) { mainWebApplicationContext = new XmlWebApplicationContext(); mainWebApplicationContext .setConfigLocation(getRequiredInitParameter("communoteWebContextConfigLocation")); mainWebApplicationContext.setParent(rootContext); // add ContextLoaderListener with web-ApplicationContext which publishes it under a // ServletContext attribute and closes it on shutdown. The former is required for // WebApplicationContextUtils which are used by DelegatingFilterProxy (spring security). // Closing is also done by dispatcher servlet's implementation of destroy method. this.servletContext.addListener(new ContextLoaderListener(mainWebApplicationContext)); DispatcherServlet dispatcherServlet = new DispatcherServlet(mainWebApplicationContext); ServletRegistration.Dynamic addedServlet = this.servletContext.addServlet( getInitParameter("communoteServletName", "communote"), dispatcherServlet); addedServlet.setLoadOnStartup(1); addedServlet.addMapping(getRequiredInitParameter("communoteServletUrlPattern")); this.mainDispatcherServlet = dispatcherServlet; }
@Before public void setUp() throws Exception { wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); // final Template expectedTemplate = new Template(); fc = new FreeMarkerConfigurer(); fc.setTemplateLoaderPaths("classpath:/", "file://" + System.getProperty("java.io.tmpdir")); fc.afterPropertiesSet(); wac.getDefaultListableBeanFactory().registerSingleton("freeMarkerConfigurer", fc); wac.refresh(); request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); response = new MockHttpServletResponse(); }
@Before public void setUp() throws Exception { wac = new StaticWebApplicationContext(); wac.setServletContext(new MockServletContext()); final Template expectedTemplate = new Template(); VelocityConfig vc = new VelocityConfig() { @Override public VelocityEngine getVelocityEngine() { return new TestVelocityEngine("test.vm", expectedTemplate); } }; wac.getDefaultListableBeanFactory().registerSingleton("velocityConfigurer", vc); wac.refresh(); request = new MockHttpServletRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver()); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, new FixedThemeResolver()); response = new MockHttpServletResponse(); }
@Override public void deployConfig(WebApplicationContext wac, Filter... filters) { Assert.state(this.port != -1, "setup() was never called."); this.context = this.tomcatServer.addContext("", System.getProperty("java.io.tmpdir")); this.context.addApplicationListener(WsContextListener.class.getName()); Tomcat.addServlet(this.context, "dispatcherServlet", new DispatcherServlet(wac)).setAsyncSupported(true); this.context.addServletMapping("/", "dispatcherServlet"); for (Filter filter : filters) { FilterDef filterDef = new FilterDef(); filterDef.setFilterName(filter.getClass().getName()); filterDef.setFilter(filter); filterDef.setAsyncSupported("true"); this.context.addFilterDef(filterDef); FilterMap filterMap = new FilterMap(); filterMap.setFilterName(filter.getClass().getName()); filterMap.addURLPattern("/*"); filterMap.setDispatcher("REQUEST,FORWARD,INCLUDE,ASYNC"); this.context.addFilterMap(filterMap); } }
@Test public void updateTargetUrl() throws Exception { StaticWebApplicationContext wac = new StaticWebApplicationContext(); wac.registerSingleton("requestDataValueProcessor", RequestDataValueProcessorWrapper.class); wac.setServletContext(new MockServletContext()); wac.refresh(); RequestDataValueProcessor mockProcessor = mock(RequestDataValueProcessor.class); wac.getBean(RequestDataValueProcessorWrapper.class).setRequestDataValueProcessor(mockProcessor); RedirectView rv = new RedirectView(); rv.setApplicationContext(wac); // Init RedirectView with WebAppCxt rv.setUrl("/path"); MockHttpServletRequest request = createRequest(); request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); HttpServletResponse response = new MockHttpServletResponse(); given(mockProcessor.processUrl(request, "/path")).willReturn("/path?key=123"); rv.render(new ModelMap(), request, response); verify(mockProcessor).processUrl(request, "/path"); }
@Test @SuppressWarnings("serial") public void noDefaultSuffixPattern() throws Exception { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) throws BeansException { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(ImplicitSubPathController.class)); RootBeanDefinition mappingDef = new RootBeanDefinition(DefaultAnnotationHandlerMapping.class); mappingDef.getPropertyValues().add("useDefaultSuffixPattern", false); wac.registerBeanDefinition("handlerMapping", mappingDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/hotel.with.dot"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("test-hotel.with.dot", response.getContentAsString()); }
@Test @SuppressWarnings("serial") public void emptyValueMapping() throws Exception { servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(ControllerWithEmptyValueMapping.class)); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setContextPath("/foo"); request.setServletPath(""); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("test", response.getContentAsString()); }
@Test @SuppressWarnings("serial") public void proxiedStandardHandleMethod() throws Exception { DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MyController.class)); DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator(); autoProxyCreator.setBeanFactory(wac.getBeanFactory()); wac.getBeanFactory().addBeanPostProcessor(autoProxyCreator); wac.getBeanFactory().registerSingleton("advisor", new DefaultPointcutAdvisor(new SimpleTraceInterceptor())); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("test", response.getContentAsString()); }
@Test public void emptyParameterListHandleMethod() throws Exception { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(EmptyParameterListHandlerMethodController.class)); RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class); vrDef.getPropertyValues().add("suffix", ".jsp"); wac.registerBeanDefinition("viewResolver", vrDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler"); MockHttpServletResponse response = new MockHttpServletResponse(); EmptyParameterListHandlerMethodController.called = false; servlet.service(request, response); assertTrue(EmptyParameterListHandlerMethodController.called); assertEquals("", response.getContentAsString()); }
public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(WebAppConfig.class); servletContext.addListener(new ContextLoaderListener(ctx)); // CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); // characterEncodingFilter.setEncoding("UTF-8"); // characterEncodingFilter.setForceEncoding(true); // javax.servlet.FilterRegistration.Dynamic filter = // servletContext.addFilter("characterEncodingFilter", characterEncodingFilter); // filter.addMappingForUrlPatterns( EnumSet.of(DispatcherType.REQUEST), // true, "/*"); ctx.setServletContext(servletContext); javax.servlet.ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); }
@Test public void commandProvidingFormControllerWithCustomEditor() throws Exception { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MyCommandProvidingFormController.class)); wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)); RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues().add("webBindingInitializer", new MyWebBindingInitializer()); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("defaultName", "myDefaultName"); request.addParameter("age", "value2"); request.addParameter("date", "2007-10-02"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue", response.getContentAsString()); }
@Test public void dispatcherServletCustomConfig() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.throw-exception-if-no-handler-found:true", "spring.mvc.dispatch-options-request:false", "spring.mvc.dispatch-trace-request:true", "spring.mvc.servlet.load-on-startup=5"); this.context.refresh(); DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); assertThat(bean).extracting("throwExceptionIfNoHandlerFound") .containsExactly(true); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(false); assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true); assertThat(new DirectFieldAccessor( this.context.getBean("dispatcherServletRegistration")) .getPropertyValue("loadOnStartup")).isEqualTo(5); }
@Test public void specificBinderInitializingCommandProvidingFormController() throws Exception { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(MySpecificBinderInitializingCommandProvidingFormController.class)); wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.class)); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath.do"); request.addParameter("defaultName", "myDefaultName"); request.addParameter("age", "value2"); request.addParameter("date", "2007-10-02"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("myView-String:myDefaultName-typeMismatch-tb1-myOriginalValue", response.getContentAsString()); }
@Test public void badRequestRequestBody() throws ServletException, IOException { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(RequestResponseBodyController.class)); RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues().add("messageConverters", new NotReadableMessageConverter()); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something"); String requestBody = "Hello World"; request.setContent(requestBody.getBytes("UTF-8")); request.addHeader("Content-Type", "application/pdf"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus()); }
@Test public void mavResolver() throws ServletException, IOException { @SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() { @Override protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) { GenericWebApplicationContext wac = new GenericWebApplicationContext(); wac.registerBeanDefinition("controller", new RootBeanDefinition(ModelAndViewResolverController.class)); RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class); adapterDef.getPropertyValues().add("customModelAndViewResolver", new MyModelAndViewResolver()); wac.registerBeanDefinition("handlerAdapter", adapterDef); wac.refresh(); return wac; } }; servlet.init(new MockServletConfig()); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); MockHttpServletResponse response = new MockHttpServletResponse(); servlet.service(request, response); assertEquals("myValue", response.getContentAsString()); }
@Test public void rootContextOnly() throws ServletException { initializer = new MyAnnotationConfigDispatcherServletInitializer() { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] {MyConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } }; initializer.onStartup(servletContext); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); servlet.init(new MockServletConfig(this.servletContext)); WebApplicationContext wac = servlet.getWebApplicationContext(); ((AnnotationConfigWebApplicationContext) wac).refresh(); assertTrue(wac.containsBean("bean")); assertTrue(wac.getBean("bean") instanceof MyBean); }
protected MockPageContext createPageContext() { MockServletContext sc = new MockServletContext(); SimpleWebApplicationContext wac = new SimpleWebApplicationContext(); wac.setServletContext(sc); wac.setNamespace("test"); wac.refresh(); MockHttpServletRequest request = new MockHttpServletRequest(sc); MockHttpServletResponse response = new MockHttpServletResponse(); if (inDispatcherServlet()) { request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); LocaleResolver lr = new AcceptHeaderLocaleResolver(); request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, lr); ThemeResolver tr = new FixedThemeResolver(); request.setAttribute(DispatcherServlet.THEME_RESOLVER_ATTRIBUTE, tr); request.setAttribute(DispatcherServlet.THEME_SOURCE_ATTRIBUTE, wac); } else { sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac); } return new MockPageContext(sc, request, response); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch06-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch04-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch05-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch03-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch02-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("*.html"); dispatcher.setLoadOnStartup(1); }
private void useDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // <-- Use DispatcherConfig.java // Define mapping between <servlet> and <servlet-mapping> ServletRegistration.Dynamic dispatcher = container.addServlet("spring-dispatcher", new DispatcherServlet( dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }
private void addDispatcherContext(ServletContext container) { // Create the dispatcher servlet's Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); dispatcherContext.register(SpringDispatcherConfig.class); // Declare <servlet> and <servlet-mapping> for the DispatcherServlet ServletRegistration.Dynamic dispatcher = container.addServlet("ch07-servlet", new DispatcherServlet(dispatcherContext)); dispatcher.addMapping("/"); dispatcher.setLoadOnStartup(1); }