Skip to content

Commit 7f7fd7d

Browse files
committed
Polish view controller MVC config
1 parent 61ec8fd commit 7f7fd7d

File tree

5 files changed

+76
-69
lines changed

5 files changed

+76
-69
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/config/ViewControllerBeanDefinitionParser.java

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020

2121
import org.springframework.beans.factory.config.BeanDefinition;
22-
import org.springframework.beans.factory.config.RuntimeBeanReference;
2322
import org.springframework.beans.factory.parsing.BeanComponentDefinition;
2423
import org.springframework.beans.factory.support.ManagedMap;
2524
import org.springframework.beans.factory.support.RootBeanDefinition;
@@ -36,6 +35,7 @@
3635
*
3736
* @author Keith Donald
3837
* @author Christian Dupuis
38+
* @author Rossen Stoyanchev
3939
* @since 3.0
4040
*/
4141
class ViewControllerBeanDefinitionParser implements BeanDefinitionParser {
@@ -50,48 +50,45 @@ public BeanDefinition parse(Element element, ParserContext parserContext) {
5050
Object source = parserContext.extractSource(element);
5151

5252
// Register SimpleUrlHandlerMapping for view controllers
53-
BeanDefinition handlerMappingDef = registerHandlerMapping(parserContext, source);
53+
BeanDefinition handlerMapping = registerHandlerMapping(parserContext, source);
5454

5555
// Ensure BeanNameUrlHandlerMapping (SPR-8289) and default HandlerAdapters are not "turned off"
5656
MvcNamespaceUtils.registerDefaultComponents(parserContext, source);
5757

5858
// Create view controller bean definition
59-
RootBeanDefinition viewControllerDef = new RootBeanDefinition(ParameterizableViewController.class);
60-
viewControllerDef.setSource(source);
59+
RootBeanDefinition controller = new RootBeanDefinition(ParameterizableViewController.class);
60+
controller.setSource(source);
6161
if (element.hasAttribute("view-name")) {
62-
viewControllerDef.getPropertyValues().add("viewName", element.getAttribute("view-name"));
62+
controller.getPropertyValues().add("viewName", element.getAttribute("view-name"));
6363
}
6464
Map<String, BeanDefinition> urlMap;
65-
if (handlerMappingDef.getPropertyValues().contains("urlMap")) {
66-
urlMap = (Map<String, BeanDefinition>) handlerMappingDef.getPropertyValues().getPropertyValue("urlMap").getValue();
65+
if (handlerMapping.getPropertyValues().contains("urlMap")) {
66+
urlMap = (Map<String, BeanDefinition>) handlerMapping.getPropertyValues().getPropertyValue("urlMap").getValue();
6767
}
6868
else {
6969
urlMap = new ManagedMap<String, BeanDefinition>();
70-
handlerMappingDef.getPropertyValues().add("urlMap", urlMap);
70+
handlerMapping.getPropertyValues().add("urlMap", urlMap);
7171
}
72-
urlMap.put(element.getAttribute("path"), viewControllerDef);
72+
urlMap.put(element.getAttribute("path"), controller);
7373

7474
return null;
7575
}
7676

77-
private BeanDefinition registerHandlerMapping(ParserContext parserContext, Object source) {
78-
if (!parserContext.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
79-
RuntimeBeanReference pathMatcherRef = MvcNamespaceUtils.registerPathMatcher(null, parserContext, source);
80-
RuntimeBeanReference pathHelperRef = MvcNamespaceUtils.registerUrlPathHelper(null, parserContext, source);
81-
82-
RootBeanDefinition handlerMappingDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
83-
handlerMappingDef.setSource(source);
84-
handlerMappingDef.getPropertyValues().add("order", "1");
85-
handlerMappingDef.getPropertyValues().add("pathMatcher", pathMatcherRef).add("urlPathHelper", pathHelperRef);
86-
handlerMappingDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
87-
parserContext.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, handlerMappingDef);
88-
parserContext.registerComponent(new BeanComponentDefinition(handlerMappingDef, HANDLER_MAPPING_BEAN_NAME));
89-
return handlerMappingDef;
90-
}
91-
else {
92-
return parserContext.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
77+
private BeanDefinition registerHandlerMapping(ParserContext context, Object source) {
78+
if (context.getRegistry().containsBeanDefinition(HANDLER_MAPPING_BEAN_NAME)) {
79+
return context.getRegistry().getBeanDefinition(HANDLER_MAPPING_BEAN_NAME);
9380
}
81+
RootBeanDefinition beanDef = new RootBeanDefinition(SimpleUrlHandlerMapping.class);
82+
beanDef.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
83+
context.getRegistry().registerBeanDefinition(HANDLER_MAPPING_BEAN_NAME, beanDef);
84+
context.registerComponent(new BeanComponentDefinition(beanDef, HANDLER_MAPPING_BEAN_NAME));
85+
86+
beanDef.setSource(source);
87+
beanDef.getPropertyValues().add("order", "1");
88+
beanDef.getPropertyValues().add("pathMatcher", MvcNamespaceUtils.registerPathMatcher(null, context, source));
89+
beanDef.getPropertyValues().add("urlPathHelper", MvcNamespaceUtils.registerUrlPathHelper(null, context, source));
9490

91+
return beanDef;
9592
}
9693

9794
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistration.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,7 @@
2121
import org.springframework.web.servlet.mvc.ParameterizableViewController;
2222

2323
/**
24-
* Encapsulates information required to create a view controller.
24+
* Assist with the registration of a single view controller.
2525
*
2626
* @author Rossen Stoyanchev
2727
* @author Keith Donald
@@ -33,37 +33,38 @@ public class ViewControllerRegistration {
3333

3434
private String viewName;
3535

36+
3637
/**
37-
* Creates a {@link ViewControllerRegistration} with the given URL path. When a request matches
38-
* to the given URL path this view controller will process it.
38+
* Creates a registration for the given URL path (or path pattern).
3939
*/
4040
public ViewControllerRegistration(String urlPath) {
41-
Assert.notNull(urlPath, "A URL path is required to create a view controller.");
41+
Assert.notNull(urlPath, "'urlPath' is required.");
4242
this.urlPath = urlPath;
4343
}
4444

45+
4546
/**
46-
* Sets the view name to use for this view controller. This field is optional. If not specified the
47-
* view controller will return a {@code null} view name, which will be resolved through the configured
48-
* {@link RequestToViewNameTranslator}. By default that means "/foo/bar" would resolve to "foo/bar".
47+
* Set the view name to return.
48+
*
49+
* <p>If not specified, the view controller returns {@code null} as the view
50+
* name in which case the configured {@link RequestToViewNameTranslator}
51+
* selects the view. In effect {@code DefaultRequestToViewNameTranslator}
52+
* translates "/foo/bar" to "foo/bar".
53+
*
54+
* @see org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator
4955
*/
5056
public void setViewName(String viewName) {
5157
this.viewName = viewName;
5258
}
5359

54-
/**
55-
* Returns the URL path for the view controller.
56-
*/
60+
5761
protected String getUrlPath() {
58-
return urlPath;
62+
return this.urlPath;
5963
}
6064

61-
/**
62-
* Returns the view controllers.
63-
*/
6465
protected Object getViewController() {
6566
ParameterizableViewController controller = new ParameterizableViewController();
66-
controller.setViewName(viewName);
67+
controller.setViewName(this.viewName);
6768
return controller;
6869
}
6970

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,9 +26,9 @@
2626
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
2727

2828
/**
29-
* Stores registrations of view controllers. A view controller does nothing more than return a specified
30-
* view name. It saves you from having to write a controller when you want to forward the request straight
31-
* through to a view such as a JSP.
29+
* Enables the registration of view controllers that have no logic other than to
30+
* return the view name they're configured with. This is an alternative to
31+
* writing a controller manually to do the same.
3232
*
3333
* @author Rossen Stoyanchev
3434
* @author Keith Donald
@@ -40,36 +40,41 @@ public class ViewControllerRegistry {
4040

4141
private int order = 1;
4242

43+
44+
/**
45+
* Register a view controller mapped to the given URL path or URL path pattern.
46+
*/
4347
public ViewControllerRegistration addViewController(String urlPath) {
4448
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
45-
registrations.add(registration);
49+
this.registrations.add(registration);
4650
return registration;
4751
}
4852

4953
/**
50-
* Specify the order to use for ViewControllers mappings relative to other {@link HandlerMapping}s
51-
* configured in the Spring MVC application context. The default value for view controllers is 1,
52-
* which is 1 higher than the value used for annotated controllers.
54+
* Specify the order to use for the {@code HandlerMapping} used to map view
55+
* controllers relative to other handler mappings configured in Spring MVC.
56+
* <p>By default this is set to 1, i.e. right after annotated controllers,
57+
* which are ordered at 0.
5358
*/
5459
public void setOrder(int order) {
5560
this.order = order;
5661
}
5762

63+
5864
/**
59-
* Returns a handler mapping with the mapped ViewControllers; or {@code null} in case of no registrations.
65+
* Return the {@code HandlerMapping} that contains the registered view
66+
* controller mappings, or {@code null} for no registrations.
6067
*/
6168
protected AbstractHandlerMapping getHandlerMapping() {
62-
if (registrations.isEmpty()) {
69+
if (this.registrations.isEmpty()) {
6370
return null;
6471
}
65-
6672
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
67-
for (ViewControllerRegistration registration : registrations) {
73+
for (ViewControllerRegistration registration : this.registrations) {
6874
urlMap.put(registration.getUrlPath(), registration.getViewController());
6975
}
70-
7176
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
72-
handlerMapping.setOrder(order);
77+
handlerMapping.setOrder(this.order);
7378
handlerMapping.setUrlMap(urlMap);
7479
return handlerMapping;
7580
}

spring-webmvc/src/main/resources/org/springframework/web/servlet/config/spring-mvc-4.1.xsd

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,16 +583,18 @@
583583
<xsd:attribute name="path" type="xsd:string" use="required">
584584
<xsd:annotation>
585585
<xsd:documentation><![CDATA[
586-
The URL path the view is mapped to.
586+
The URL path or (path pattern) the controller is mapped to.
587587
]]></xsd:documentation>
588588
</xsd:annotation>
589589
</xsd:attribute>
590590
<xsd:attribute name="view-name" type="xsd:string">
591591
<xsd:annotation>
592592
<xsd:documentation><![CDATA[
593593
The name of the view to render. Optional.
594-
If not specified, the view name will be determined from the current HttpServletRequest
595-
by the DispatcherServlet's RequestToViewNameTranslator.
594+
595+
If not specified, the view controller will return null as the view name in which case
596+
the configured RequestToViewNameTranslator selects the view.
597+
In effect the DefaultRequestToViewNameTranslator translates "/foo/bar" to "foo/bar".
596598
]]></xsd:documentation>
597599
</xsd:annotation>
598600
</xsd:attribute>

spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistryTests.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,47 +36,49 @@ public class ViewControllerRegistryTests {
3636

3737
private ViewControllerRegistry registry;
3838

39+
3940
@Before
4041
public void setUp() {
41-
registry = new ViewControllerRegistry();
42+
this.registry = new ViewControllerRegistry();
4243
}
4344

4445
@Test
4546
public void noViewControllers() throws Exception {
46-
assertNull(registry.getHandlerMapping());
47+
assertNull(this.registry.getHandlerMapping());
4748
}
4849

4950
@Test
5051
public void addViewController() {
51-
registry.addViewController("/path");
52+
this.registry.addViewController("/path").setViewName("viewName");
5253
Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
5354
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
5455
assertNotNull(controller);
55-
assertNull(controller.getViewName());
56+
assertEquals("viewName", controller.getViewName());
5657
}
5758

5859
@Test
59-
public void addViewControllerWithViewName() {
60-
registry.addViewController("/path").setViewName("viewName");
60+
public void addViewControllerWithDefaultViewName() {
61+
this.registry.addViewController("/path");
6162
Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
6263
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get("/path");
6364
assertNotNull(controller);
64-
assertEquals("viewName", controller.getViewName());
65+
assertNull(controller.getViewName());
6566
}
6667

6768
@Test
6869
public void order() {
69-
registry.addViewController("/path");
70+
this.registry.addViewController("/path");
7071
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
7172
assertEquals(1, handlerMapping.getOrder());
7273

73-
registry.setOrder(2);
74+
this.registry.setOrder(2);
7475
handlerMapping = getHandlerMapping();
7576
assertEquals(2, handlerMapping.getOrder());
7677
}
7778

79+
7880
private SimpleUrlHandlerMapping getHandlerMapping() {
79-
return (SimpleUrlHandlerMapping) registry.getHandlerMapping();
81+
return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
8082
}
8183

8284
}

0 commit comments

Comments
 (0)