org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping Java Examples

The following examples show how to use org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SimpleWebApplicationContext.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void refresh() throws BeansException {
	registerSingleton("/locale.do", LocaleChecker.class);

	addMessage("test", Locale.ENGLISH, "test message");
	addMessage("test", Locale.CANADA, "Canadian & test message");
	addMessage("testArgs", Locale.ENGLISH, "test {0} message {1}");
	addMessage("testArgsFormat", Locale.ENGLISH, "test {0} message {1,number,#.##} X");

	registerSingleton(UiApplicationContextUtils.THEME_SOURCE_BEAN_NAME, DummyThemeSource.class);

	registerSingleton("handlerMapping", BeanNameUrlHandlerMapping.class);
	registerSingleton("viewResolver", InternalResourceViewResolver.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("location", "org/springframework/web/context/WEB-INF/sessionContext.xml");
	registerSingleton("viewResolver2", XmlViewResolver.class, pvs);

	super.refresh();
}
 
Example #2
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml");

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example #3
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml", 7);

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example #4
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testViewControllersDefaultConfig() {
	loadBeanDefinitions("mvc-config-view-controllers-minimal.xml");

	SimpleUrlHandlerMapping hm = this.appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(hm);
	ParameterizableViewController viewController = (ParameterizableViewController) hm.getUrlMap().get("/path");
	assertNotNull(viewController);
	assertEquals("home", viewController.getViewName());

	ParameterizableViewController redirectViewController = (ParameterizableViewController) hm.getUrlMap().get("/old");
	assertNotNull(redirectViewController);
	assertThat(redirectViewController.getView(), Matchers.instanceOf(RedirectView.class));

	ParameterizableViewController statusViewController = (ParameterizableViewController) hm.getUrlMap().get("/bad");
	assertNotNull(statusViewController);
	assertEquals(404, statusViewController.getStatusCode().value());

	BeanNameUrlHandlerMapping beanNameMapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());
}
 
Example #5
Source File: WebMvcConfigurationSupport.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
 * paths to controller bean names.
 */
@Bean
public BeanNameUrlHandlerMapping beanNameHandlerMapping(FormattingConversionService mvcConversionService,
		ResourceUrlProvider mvcResourceUrlProvider) {
	BeanNameUrlHandlerMapping mapping = new BeanNameUrlHandlerMapping();
	mapping.setOrder(2);
	mapping.setInterceptors(getInterceptors(mvcConversionService, mvcResourceUrlProvider));
	mapping.setCorsConfigurations(getCorsConfigurations());
	return mapping;
}
 
Example #6
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanNameUrlHandlerMapping() {
	loadBeanDefinitions("mvc-config.xml");
	BeanNameUrlHandlerMapping mapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(2, mapping.getOrder());
}
 
Example #7
Source File: WebMvcConfigurationSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #8
Source File: MvcNamespaceTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml", 10);

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);

	SimpleUrlHandlerMapping mapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, mapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	MappedInterceptor mappedInterceptor = appContext.getBean(MappedInterceptor.class);
	assertNotNull(urlProvider);
	assertEquals(ResourceUrlProviderExposingInterceptor.class, mappedInterceptor.getInterceptor().getClass());

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/resources/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = mapping.getHandler(request);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #9
Source File: WebMvcConfigurationSupport.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
 * paths to controller bean names.
 */
@Bean
public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
	BeanNameUrlHandlerMapping mapping = new BeanNameUrlHandlerMapping();
	mapping.setOrder(2);
	mapping.setInterceptors(getInterceptors());
	mapping.setCorsConfigurations(getCorsConfigurations());
	return mapping;
}
 
Example #10
Source File: MvcNamespaceUtils.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
		RootBeanDefinition beanNameMappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		beanNameMappingDef.setSource(source);
		beanNameMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		beanNameMappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, parserContext, source);
		beanNameMappingDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);
		parserContext.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, beanNameMappingDef);
		parserContext.registerComponent(new BeanComponentDefinition(beanNameMappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example #11
Source File: WebMvcConfigurationSupport.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
 * paths to controller bean names.
 */
@Bean
public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
	BeanNameUrlHandlerMapping mapping = new BeanNameUrlHandlerMapping();
	mapping.setOrder(2);
	mapping.setInterceptors(getInterceptors());
	mapping.setCorsConfigurations(getCorsConfigurations());
	return mapping;
}
 
Example #12
Source File: MvcNamespaceUtils.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext parserContext, Object source) {
	if (!parserContext.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
		RootBeanDefinition beanNameMappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		beanNameMappingDef.setSource(source);
		beanNameMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		beanNameMappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsConfigurationsRef = MvcNamespaceUtils.registerCorsConfigurations(null, parserContext, source);
		beanNameMappingDef.getPropertyValues().add("corsConfigurations", corsConfigurationsRef);
		parserContext.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, beanNameMappingDef);
		parserContext.registerComponent(new BeanComponentDefinition(beanNameMappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example #13
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanNameUrlHandlerMapping() {
	loadBeanDefinitions("mvc-config.xml");
	BeanNameUrlHandlerMapping mapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(2, mapping.getOrder());
}
 
Example #14
Source File: WebMvcConfigurationSupportTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #15
Source File: WebMvcConfigurationSupport.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
 * paths to controller bean names.
 */
@Bean
public BeanNameUrlHandlerMapping beanNameHandlerMapping() {
	BeanNameUrlHandlerMapping mapping = new BeanNameUrlHandlerMapping();
	mapping.setOrder(2);
	mapping.setInterceptors(getInterceptors());
	mapping.setCorsConfigurations(getCorsConfigurations());
	return mapping;
}
 
Example #16
Source File: MvcNamespaceUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)){
		RootBeanDefinition mappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		mappingDef.setSource(source);
		mappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
		mappingDef.getPropertyValues().add("corsConfigurations", corsRef);
		context.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, mappingDef);
		context.registerComponent(new BeanComponentDefinition(mappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example #17
Source File: AnnotationDrivenBeanDefinitionParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanNameUrlHandlerMapping() {
	loadBeanDefinitions("mvc-config.xml");
	BeanNameUrlHandlerMapping mapping = this.appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(mapping);
	assertEquals(2, mapping.getOrder());
}
 
Example #18
Source File: WebMvcConfigurationSupportTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void beanNameHandlerMapping() throws Exception {
	ApplicationContext context = initContext(WebConfig.class);
	BeanNameUrlHandlerMapping handlerMapping = context.getBean(BeanNameUrlHandlerMapping.class);
	assertEquals(2, handlerMapping.getOrder());

	HttpServletRequest request = new MockHttpServletRequest("GET", "/testController");
	HandlerExecutionChain chain = handlerMapping.getHandler(request);

	assertNotNull(chain);
	assertNotNull(chain.getInterceptors());
	assertEquals(3, chain.getInterceptors().length);
	assertEquals(ConversionServiceExposingInterceptor.class, chain.getInterceptors()[1].getClass());
	assertEquals(ResourceUrlProviderExposingInterceptor.class, chain.getInterceptors()[2].getClass());
}
 
Example #19
Source File: MvcNamespaceUtils.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Registers  an {@link HttpRequestHandlerAdapter} under a well-known
 * name unless already registered.
 */
private static void registerBeanNameUrlHandlerMapping(ParserContext context, @Nullable Object source) {
	if (!context.getRegistry().containsBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME)) {
		RootBeanDefinition mappingDef = new RootBeanDefinition(BeanNameUrlHandlerMapping.class);
		mappingDef.setSource(source);
		mappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
		mappingDef.getPropertyValues().add("order", 2);	// consistent with WebMvcConfigurationSupport
		RuntimeBeanReference corsRef = MvcNamespaceUtils.registerCorsConfigurations(null, context, source);
		mappingDef.getPropertyValues().add("corsConfigurations", corsRef);
		context.getRegistry().registerBeanDefinition(BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME, mappingDef);
		context.registerComponent(new BeanComponentDefinition(mappingDef, BEAN_NAME_URL_HANDLER_MAPPING_BEAN_NAME));
	}
}
 
Example #20
Source File: MvcNamespaceTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());

	SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(resourceMapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
	List<Class<?>> interceptors = beans.values().stream()
			.map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass())
			.collect(Collectors.toList());
	assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class,
			ResourceUrlProviderExposingInterceptor.class));

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/resources/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = resourceMapping.getHandler(request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #21
Source File: MvcNamespaceTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testResources() throws Exception {
	loadBeanDefinitions("mvc-config-resources.xml");

	HttpRequestHandlerAdapter adapter = appContext.getBean(HttpRequestHandlerAdapter.class);
	assertNotNull(adapter);

	RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
	ContentNegotiationManager manager = mapping.getContentNegotiationManager();

	ResourceHttpRequestHandler handler = appContext.getBean(ResourceHttpRequestHandler.class);
	assertNotNull(handler);
	assertSame(manager, handler.getContentNegotiationManager());

	SimpleUrlHandlerMapping resourceMapping = appContext.getBean(SimpleUrlHandlerMapping.class);
	assertNotNull(resourceMapping);
	assertEquals(Ordered.LOWEST_PRECEDENCE - 1, resourceMapping.getOrder());

	BeanNameUrlHandlerMapping beanNameMapping = appContext.getBean(BeanNameUrlHandlerMapping.class);
	assertNotNull(beanNameMapping);
	assertEquals(2, beanNameMapping.getOrder());

	ResourceUrlProvider urlProvider = appContext.getBean(ResourceUrlProvider.class);
	assertNotNull(urlProvider);

	Map<String, MappedInterceptor> beans = appContext.getBeansOfType(MappedInterceptor.class);
	List<Class<?>> interceptors = beans.values().stream()
			.map(mappedInterceptor -> mappedInterceptor.getInterceptor().getClass())
			.collect(Collectors.toList());
	assertThat(interceptors, containsInAnyOrder(ConversionServiceExposingInterceptor.class,
			ResourceUrlProviderExposingInterceptor.class));

	MockHttpServletRequest request = new MockHttpServletRequest();
	request.setRequestURI("/resources/foo.css");
	request.setMethod("GET");

	HandlerExecutionChain chain = resourceMapping.getHandler(request);
	assertNotNull(chain);
	assertTrue(chain.getHandler() instanceof ResourceHttpRequestHandler);

	MockHttpServletResponse response = new MockHttpServletResponse();
	for (HandlerInterceptor interceptor : chain.getInterceptors()) {
		interceptor.preHandle(request, response, chain.getHandler());
	}
	ModelAndView mv = adapter.handle(request, response, chain.getHandler());
	assertNull(mv);
}
 
Example #22
Source File: BeanNameUrlHandlerMappingConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
    return new BeanNameUrlHandlerMapping();
}
 
Example #23
Source File: HandlerMappingPrioritiesConfig.java    From tutorials with MIT License 4 votes vote down vote up
@Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMappingOrder1() {
    BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
    beanNameUrlHandlerMapping.setOrder(1);
    return beanNameUrlHandlerMapping;
}