From 7f7fd7d3110c664a255e5ddf52899205d64b68a5 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 17 Jul 2014 11:23:45 -0400 Subject: [PATCH] Polish view controller MVC config --- .../ViewControllerBeanDefinitionParser.java | 47 +++++++++---------- .../ViewControllerRegistration.java | 33 ++++++------- .../annotation/ViewControllerRegistry.java | 33 +++++++------ .../web/servlet/config/spring-mvc-4.1.xsd | 8 ++-- .../ViewControllerRegistryTests.java | 34 +++++++------- 5 files changed, 81 insertions(+), 74 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java index 52181a2875f..1161c85cdd4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java @@ -19,7 +19,6 @@ package org.springframework.web.servlet.config; import java.util.Map; import org.springframework.beans.factory.config.BeanDefinition; -import org.springframework.beans.factory.config.RuntimeBeanReference; import org.springframework.beans.factory.parsing.BeanComponentDefinition; import org.springframework.beans.factory.support.ManagedMap; import org.springframework.beans.factory.support.RootBeanDefinition; @@ -36,6 +35,7 @@ import org.w3c.dom.Element; * * @author Keith Donald * @author Christian Dupuis + * @author Rossen Stoyanchev * @since 3.0 */ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser { @@ -50,48 +50,45 @@ class ViewControllerBeanDefinitionParser implements BeanDefinitionParser { Object source = parserContext.extractSource(element); // Register SimpleUrlHandlerMapping for view controllers - BeanDefinition handlerMappingDef = registerHandlerMapping(parserContext, source); + BeanDefinition handlerMapping = registerHandlerMapping(parserContext, source); // Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off" MvcNamespaceUtils.registerDefaultComponents(parserContext, source); // Create view controller bean definition - RootBeanDefinition viewControllerDef = new RootBeanDefinition(ParameterizableViewController.class); - viewControllerDef.setSource(source); + RootBeanDefinition controller = new RootBeanDefinition(ParameterizableViewController.class); + controller.setSource(source); if (element.hasAttribute("view-name")) { - viewControllerDef.getPropertyValues().add("viewName", element.getAttribute("view-name")); + controller.getPropertyValues().add("viewName", element.getAttribute("view-name")); } Map urlMap; - if (handlerMappingDef.getPropertyValues().contains("urlMap")) { - urlMap = (Map) handlerMappingDef.getPropertyValues().getPropertyValue("urlMap").getValue(); + if (handlerMapping.getPropertyValues().contains("urlMap")) { + urlMap = (Map) handlerMapping.getPropertyValues().getPropertyValue("urlMap").getValue(); } else { urlMap = new ManagedMap(); - handlerMappingDef.getPropertyValues().add("urlMap", urlMap); + handlerMapping.getPropertyValues().add("urlMap", urlMap); } - urlMap.put(element.getAttribute("path"), viewControllerDef); + urlMap.put(element.getAttribute("path"), controller); return null; } - private BeanDefinition registerHandlerMapping(ParserContext parserContext, Object source) { - if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) { - RuntimeBeanReference pathMatcherRef = MvcNamespaceUtils.registerPathMatcher(null, parserContext, source); - RuntimeBeanReference pathHelperRef = MvcNamespaceUtils.registerUrlPathHelper(null, parserContext, source); - - RootBeanDefinition handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class); - handlerMappingDef.setSource(source); - handlerMappingDef.getPropertyValues().add("order", "1"); - handlerMappingDef.getPropertyValues().add("pathMatcher", pathMatcherRef).add("urlPathHelper", pathHelperRef); - handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); - parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, handlerMappingDef); - parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, HANDLER_MAPPING_BEAN_NAME)); - return handlerMappingDef; - } - else { - return parserContext.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME); + private BeanDefinition registerHandlerMapping(ParserContext context, Object source) { + if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) { + return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME); } + RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class); + beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); + context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef); + context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME)); + beanDef.setSource(source); + beanDef.getPropertyValues().add("order", "1"); + beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source)); + beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source)); + + return beanDef; } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java index bc3b1738ce6..5b1f0694417 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ import org.springframework.web.servlet.RequestToViewNameTranslator; import org.springframework.web.servlet.mvc.ParameterizableViewController; /** - * Encapsulates information required to create a view controller. + * Assist with the registration of a single view controller. * * @author Rossen Stoyanchev * @author Keith Donald @@ -33,37 +33,38 @@ public class ViewControllerRegistration { private String viewName; + /** - * Creates a {@link ViewControllerRegistration} with the given URL path. When a request matches - * to the given URL path this view controller will process it. + * Creates a registration for the given URL path (or path pattern). */ public ViewControllerRegistration(String urlPath) { - Assert.notNull(urlPath, "A URL path is required to create a view controller."); + Assert.notNull(urlPath, "'urlPath' is required."); this.urlPath = urlPath; } + /** - * Sets the view name to use for this view controller. This field is optional. If not specified the - * view controller will return a {@code null} view name, which will be resolved through the configured - * {@link RequestToViewNameTranslator}. By default that means "/foo/bar" would resolve to "foo/bar". + * Set the view name to return. + * + *

If not specified, the view controller returns {@code null} as the view + * name in which case the configured {@link RequestToViewNameTranslator} + * selects the view. In effect {@code DefaultRequestToViewNameTranslator} + * translates "/foo/bar" to "foo/bar". + * + * @see org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator */ public void setViewName(String viewName) { this.viewName = viewName; } - /** - * Returns the URL path for the view controller. - */ + protected String getUrlPath() { - return urlPath; + return this.urlPath; } - /** - * Returns the view controllers. - */ protected Object getViewController() { ParameterizableViewController controller = new ParameterizableViewController(); - controller.setViewName(viewName); + controller.setViewName(this.viewName); return controller; } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java index adfd5cf1632..11535f7c008 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -26,9 +26,9 @@ import org.springframework.web.servlet.handler.AbstractHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; /** - * Stores registrations of view controllers. A view controller does nothing more than return a specified - * view name. It saves you from having to write a controller when you want to forward the request straight - * through to a view such as a JSP. + * Enables the registration of view controllers that have no logic other than to + * return the view name they're configured with. This is an alternative to + * writing a controller manually to do the same. * * @author Rossen Stoyanchev * @author Keith Donald @@ -40,36 +40,41 @@ public class ViewControllerRegistry { private int order = 1; + + /** + * Register a view controller mapped to the given URL path or URL path pattern. + */ public ViewControllerRegistration addViewController(String urlPath) { ViewControllerRegistration registration = new ViewControllerRegistration(urlPath); - registrations.add(registration); + this.registrations.add(registration); return registration; } /** - * Specify the order to use for ViewControllers mappings relative to other {@link HandlerMapping}s - * configured in the Spring MVC application context. The default value for view controllers is 1, - * which is 1 higher than the value used for annotated controllers. + * Specify the order to use for the {@code HandlerMapping} used to map view + * controllers relative to other handler mappings configured in Spring MVC. + *

By default this is set to 1, i.e. right after annotated controllers, + * which are ordered at 0. */ public void setOrder(int order) { this.order = order; } + /** - * Returns a handler mapping with the mapped ViewControllers; or {@code null} in case of no registrations. + * Return the {@code HandlerMapping} that contains the registered view + * controller mappings, or {@code null} for no registrations. */ protected AbstractHandlerMapping getHandlerMapping() { - if (registrations.isEmpty()) { + if (this.registrations.isEmpty()) { return null; } - Map urlMap = new LinkedHashMap(); - for (ViewControllerRegistration registration : registrations) { + for (ViewControllerRegistration registration : this.registrations) { urlMap.put(registration.getUrlPath(), registration.getViewController()); } - SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(); - handlerMapping.setOrder(order); + handlerMapping.setOrder(this.order); handlerMapping.setUrlMap(urlMap); return handlerMapping; } diff --git a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd index 83b1c4e91cc..6d2758ae77f 100644 --- a/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd +++ b/spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd @@ -583,7 +583,7 @@ @@ -591,8 +591,10 @@ diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java index 0695958fa74..cb76073a98e 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,47 +36,49 @@ public class ViewControllerRegistryTests { private ViewControllerRegistry registry; + @Before public void setUp() { - registry = new ViewControllerRegistry(); + this.registry = new ViewControllerRegistry(); } @Test public void noViewControllers() throws Exception { - assertNull(registry.getHandlerMapping()); + assertNull(this.registry.getHandlerMapping()); } @Test public void addViewController() { - registry.addViewController("/path"); - Map urlMap = getHandlerMapping().getUrlMap(); - ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path"); - assertNotNull(controller); - assertNull(controller.getViewName()); - } - - @Test - public void addViewControllerWithViewName() { - registry.addViewController("/path").setViewName("viewName"); + this.registry.addViewController("/path").setViewName("viewName"); Map urlMap = getHandlerMapping().getUrlMap(); ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path"); assertNotNull(controller); assertEquals("viewName", controller.getViewName()); } + @Test + public void addViewControllerWithDefaultViewName() { + this.registry.addViewController("/path"); + Map urlMap = getHandlerMapping().getUrlMap(); + ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path"); + assertNotNull(controller); + assertNull(controller.getViewName()); + } + @Test public void order() { - registry.addViewController("/path"); + this.registry.addViewController("/path"); SimpleUrlHandlerMapping handlerMapping = getHandlerMapping(); assertEquals(1, handlerMapping.getOrder()); - registry.setOrder(2); + this.registry.setOrder(2); handlerMapping = getHandlerMapping(); assertEquals(2, handlerMapping.getOrder()); } + private SimpleUrlHandlerMapping getHandlerMapping() { - return (SimpleUrlHandlerMapping) registry.getHandlerMapping(); + return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping(); } }