當前位置: 首頁>>代碼示例>>Java>>正文


Java DispatcherServlet.init方法代碼示例

本文整理匯總了Java中org.springframework.web.servlet.DispatcherServlet.init方法的典型用法代碼示例。如果您正苦於以下問題:Java DispatcherServlet.init方法的具體用法?Java DispatcherServlet.init怎麽用?Java DispatcherServlet.init使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.springframework.web.servlet.DispatcherServlet的用法示例。


在下文中一共展示了DispatcherServlet.init方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initServlet

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@SuppressWarnings("serial")
private void initServlet(final Class<?> controllerclass) throws ServletException {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
				throws BeansException {
			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());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:20,代碼來源:CgLibProxyServletAnnotationTests.java

示例2: doubles

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
@SuppressWarnings("serial")
public void doubles() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
				throws BeansException {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(DoubleController.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", "/lat/1.2/long/3.4");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);

	assertEquals("latitude-1.2-longitude-3.4", response.getContentAsString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:UriTemplateServletAnnotationControllerTests.java

示例3: noDefaultSuffixPattern

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@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());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:UriTemplateServletAnnotationControllerTests.java

示例4: unsupportedRequestBody

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
public void unsupportedRequestBody() 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 ByteArrayHttpMessageConverter());
			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(415, response.getStatus());
	assertNotNull("No Accept response header set", response.getHeader("Accept"));
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:ServletAnnotationControllerTests.java

示例5: binderInitializingCommandProvidingFormController

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
public void binderInitializingCommandProvidingFormController() throws Exception {
	@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller",
					new RootBeanDefinition(MyBinderInitializingCommandProvidingFormController.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());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:24,代碼來源:ServletAnnotationControllerTests.java

示例6: emptyParameterListHandleMethod

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@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());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:26,代碼來源:ServletAnnotationControllerTests.java

示例7: rootContextOnly

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@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);
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:25,代碼來源:AnnotationConfigDispatcherServletInitializerTests.java

示例8: modelFormController

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
public void modelFormController() throws Exception {
	@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(MyModelFormController.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("name", "name1");
	request.addParameter("age", "value2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("myPath-name1-typeMismatch-tb1-myValue-yourValue", response.getContentAsString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:22,代碼來源:ServletAnnotationControllerTests.java

示例9: initServlet

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
private static Servlet initServlet() {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(MockDemoConfig.class);
    context.setServletContext(SERVLET_CONTEXT);
    DispatcherServlet servlet = new DispatcherServlet(context);
    ServletConfig servletConfig = new MockServletConfig();
    try {
        servlet.init(servletConfig);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return servlet;
}
 
開發者ID:intuit,項目名稱:karate,代碼行數:14,代碼來源:MockSpringMvcServlet.java

示例10: parameterCsvAsIntegerSet

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
@SuppressWarnings("serial")
public void parameterCsvAsIntegerSet() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(CsvController.class));
			RootBeanDefinition csDef = new RootBeanDefinition(FormattingConversionServiceFactoryBean.class);
			RootBeanDefinition wbiDef = new RootBeanDefinition(ConfigurableWebBindingInitializer.class);
			wbiDef.getPropertyValues().add("conversionService", csDef);
			RootBeanDefinition adapterDef = new RootBeanDefinition(AnnotationMethodHandlerAdapter.class);
			adapterDef.getPropertyValues().add("webBindingInitializer", wbiDef);
			wac.registerBeanDefinition("handlerAdapter", adapterDef);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/integerSet");
	request.setMethod("POST");
	request.addParameter("content", "1,2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("1-2", response.getContentAsString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:29,代碼來源:ServletAnnotationControllerTests.java

示例11: onStartup

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    if (springProfiles != null) {
        applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0]));
    }
    applicationContext.setServletContext(servletContext);

    dispatcherConfig = new DefaultDispatcherConfig(servletContext);
    applicationContext.setServletConfig(dispatcherConfig);

    // Configure the listener for the request handled events. All we do here is release the latch
    applicationContext.addApplicationListener(new ApplicationListener<ServletRequestHandledEvent>() {
        @Override
        public void onApplicationEvent(ServletRequestHandledEvent servletRequestHandledEvent) {
            try {
                currentResponse.flushBuffer();
            } catch (IOException e) {
                log.error("Could not flush response buffer", e);
                throw new RuntimeException("Could not flush response buffer", e);
            }
        }
    });

    // Manage the lifecycle of the root application context
    this.addListener(new ContextLoaderListener(applicationContext));

    // Register and map the dispatcher servlet
    dispatcherServlet = new DispatcherServlet(applicationContext);

    if (refreshContext) {
        dispatcherServlet.refresh();
    }

    dispatcherServlet.onApplicationEvent(new ContextRefreshedEvent(applicationContext));
    dispatcherServlet.init(dispatcherConfig);

    notifyStartListeners(servletContext);
}
 
開發者ID:awslabs,項目名稱:aws-serverless-java-container,代碼行數:39,代碼來源:LambdaSpringApplicationInitializer.java

示例12: controllerClassName

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
@SuppressWarnings("serial")
public void controllerClassName() throws Exception {
	servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent)
				throws BeansException {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(ControllerClassNameController.class));
			RootBeanDefinition mapping = new RootBeanDefinition(ControllerClassNameHandlerMapping.class);
			mapping.getPropertyValues().add("excludedPackages", null);
			wac.registerBeanDefinition("handlerMapping", mapping);
			wac.refresh();
			return wac;
		}
	};
	servlet.init(new MockServletConfig());

	MockHttpServletRequest request = new MockHttpServletRequest("GET", "/controllerclassname/bar");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("plain-bar", response.getContentAsString());

	request = new MockHttpServletRequest("GET", "/controllerclassname/bar.pdf");
	response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("pdf-bar", response.getContentAsString());

	request = new MockHttpServletRequest("GET", "/controllerclassname/bar.do");
	response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("plain-bar", response.getContentAsString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:34,代碼來源:UriTemplateServletAnnotationControllerTests.java

示例13: createServletContext

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
private ServletContext createServletContext () throws Exception {
  	// Establish the servlet context and config
      final MockServletContext servletContext = new MockServletContext(WEB_APP_PATH, new FileSystemResourceLoader());
      final MockServletConfig servletConfig = new MockServletConfig(servletContext);
      
      // Create a WebApplicationContext and initialize it with the xml and servlet configuration.
      final XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext();
      servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, webApplicationContext);
      webApplicationContext.setServletConfig(servletConfig);
      
      // Create a DispatcherServlet that uses the previously established WebApplicationContext.
      @SuppressWarnings("serial")
final DispatcherServlet dispatcherServlet = new DispatcherServlet() {
              @Override
              protected WebApplicationContext createWebApplicationContext(ApplicationContext parent) {
                      return webApplicationContext;
              }
      };
      
      // Prepare the context.
      webApplicationContext.refresh();
      webApplicationContext.registerShutdownHook();
      
      // Initialize the servlet.
      dispatcherServlet.setContextConfigLocation("");
      dispatcherServlet.init(servletConfig);
      
      return dispatcherServlet.getServletContext();
  }
 
開發者ID:xenit-eu,項目名稱:move2alf,代碼行數:30,代碼來源:RootMap.java

示例14: testFrameworkServletWithDefaultLocation

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
public void testFrameworkServletWithDefaultLocation() throws Exception {
	DispatcherServlet servlet = new DispatcherServlet();
	servlet.setContextClass(XmlWebApplicationContext.class);
	try {
		servlet.init(new MockServletConfig(new MockServletContext(""), "test"));
		fail("Should have thrown BeanDefinitionStoreException");
	}
	catch (BeanDefinitionStoreException ex) {
		// expected
		assertTrue(ex.getCause() instanceof IOException);
		assertTrue(ex.getCause().getMessage().contains("/WEB-INF/test-servlet.xml"));
	}
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:15,代碼來源:ContextLoaderTests.java

示例15: proxiedFormController

import org.springframework.web.servlet.DispatcherServlet; //導入方法依賴的package包/類
@Test
@SuppressWarnings("serial")
public void proxiedFormController() throws Exception {
	DispatcherServlet servlet = new DispatcherServlet() {
		@Override
		protected WebApplicationContext createWebApplicationContext(WebApplicationContext parent) {
			GenericWebApplicationContext wac = new GenericWebApplicationContext();
			wac.registerBeanDefinition("controller", new RootBeanDefinition(MyFormController.class));
			wac.registerBeanDefinition("viewResolver", new RootBeanDefinition(TestViewResolver.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");
	request.addParameter("name", "name1");
	request.addParameter("age", "value2");
	MockHttpServletResponse response = new MockHttpServletResponse();
	servlet.service(request, response);
	assertEquals("myView-name1-typeMismatch-tb1-myValue", response.getContentAsString());
}
 
開發者ID:langtianya,項目名稱:spring4-understanding,代碼行數:28,代碼來源:ServletAnnotationControllerTests.java


注:本文中的org.springframework.web.servlet.DispatcherServlet.init方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。