Skip to content

Commit f7f801c

Browse files
committed
Configure view controllers with ApplicationContext
Issue: SPR-13762
1 parent 83c9ec4 commit f7f801c

File tree

5 files changed

+39
-10
lines changed

5 files changed

+39
-10
lines changed

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.config.annotation;
1818

19+
import org.springframework.context.ApplicationContext;
1920
import org.springframework.http.HttpStatus;
2021
import org.springframework.util.Assert;
2122
import org.springframework.web.servlet.mvc.ParameterizableViewController;
@@ -81,6 +82,10 @@ public RedirectViewControllerRegistration setKeepQueryParams(boolean propagate)
8182
return this;
8283
}
8384

85+
protected void setApplicationContext(ApplicationContext applicationContext) {
86+
this.controller.setApplicationContext(applicationContext);
87+
this.redirectView.setApplicationContext(applicationContext);
88+
}
8489

8590
protected String getUrlPath() {
8691
return this.urlPath;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.web.servlet.config.annotation;
1818

19+
import org.springframework.context.ApplicationContext;
1920
import org.springframework.http.HttpStatus;
2021
import org.springframework.util.Assert;
2122
import org.springframework.web.servlet.RequestToViewNameTranslator;
@@ -65,6 +66,9 @@ public void setViewName(String viewName) {
6566
this.controller.setViewName(viewName);
6667
}
6768

69+
protected void setApplicationContext(ApplicationContext applicationContext) {
70+
this.controller.setApplicationContext(applicationContext);
71+
}
6872

6973
protected String getUrlPath() {
7074
return this.urlPath;

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.springframework.context.ApplicationContext;
2425
import org.springframework.http.HttpStatus;
2526
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
2627
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
@@ -42,13 +43,16 @@ public class ViewControllerRegistry {
4243

4344
private int order = 1;
4445

46+
private ApplicationContext applicationContext;
47+
4548

4649
/**
4750
* Map a view controller to the given URL path (or pattern) in order to render
4851
* a response with a pre-configured status code and view.
4952
*/
5053
public ViewControllerRegistration addViewController(String urlPath) {
5154
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
55+
registration.setApplicationContext(this.applicationContext);
5256
this.registrations.add(registration);
5357
return registration;
5458
}
@@ -61,6 +65,7 @@ public ViewControllerRegistration addViewController(String urlPath) {
6165
*/
6266
public RedirectViewControllerRegistration addRedirectViewController(String urlPath, String redirectUrl) {
6367
RedirectViewControllerRegistration registration = new RedirectViewControllerRegistration(urlPath, redirectUrl);
68+
registration.setApplicationContext(this.applicationContext);
6469
this.redirectRegistrations.add(registration);
6570
return registration;
6671
}
@@ -72,6 +77,7 @@ public RedirectViewControllerRegistration addRedirectViewController(String urlPa
7277
*/
7378
public void addStatusController(String urlPath, HttpStatus statusCode) {
7479
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
80+
registration.setApplicationContext(this.applicationContext);
7581
registration.setStatusCode(statusCode);
7682
registration.getViewController().setStatusOnly(true);
7783
this.registrations.add(registration);
@@ -87,6 +93,10 @@ public void setOrder(int order) {
8793
this.order = order;
8894
}
8995

96+
protected void setApplicationContext(ApplicationContext applicationContext) {
97+
this.applicationContext = applicationContext;
98+
}
99+
90100

91101
/**
92102
* Return the {@code HandlerMapping} that contains the registered view

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

+1
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ protected void configureContentNegotiation(ContentNegotiationConfigurer configur
343343
@Bean
344344
public HandlerMapping viewControllerHandlerMapping() {
345345
ViewControllerRegistry registry = new ViewControllerRegistry();
346+
registry.setApplicationContext(this.applicationContext);
346347
addViewControllers(registry);
347348

348349
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();

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

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -22,6 +22,7 @@
2222
import org.junit.Before;
2323
import org.junit.Test;
2424

25+
import org.springframework.context.support.StaticApplicationContext;
2526
import org.springframework.http.HttpStatus;
2627
import org.springframework.mock.web.test.MockHttpServletRequest;
2728
import org.springframework.mock.web.test.MockHttpServletResponse;
@@ -48,6 +49,7 @@ public class ViewControllerRegistryTests {
4849
@Before
4950
public void setUp() {
5051
this.registry = new ViewControllerRegistry();
52+
this.registry.setApplicationContext(new StaticApplicationContext());
5153
this.request = new MockHttpServletRequest("GET", "/");
5254
this.response = new MockHttpServletResponse();
5355
}
@@ -61,18 +63,22 @@ public void noViewControllers() throws Exception {
6163
public void addViewController() {
6264
this.registry.addViewController("/path").setViewName("viewName");
6365
ParameterizableViewController controller = getController("/path");
66+
6467
assertEquals("viewName", controller.getViewName());
6568
assertNull(controller.getStatusCode());
6669
assertFalse(controller.isStatusOnly());
70+
assertNotNull(controller.getApplicationContext());
6771
}
6872

6973
@Test
7074
public void addViewControllerWithDefaultViewName() {
7175
this.registry.addViewController("/path");
7276
ParameterizableViewController controller = getController("/path");
77+
7378
assertNull(controller.getViewName());
7479
assertNull(controller.getStatusCode());
7580
assertFalse(controller.isStatusOnly());
81+
assertNotNull(controller.getApplicationContext());
7682
}
7783

7884
@Test
@@ -85,6 +91,7 @@ public void addRedirectViewController() throws Exception {
8591

8692
assertEquals(302, this.response.getStatus());
8793
assertEquals("/context/redirectTo", this.response.getRedirectedUrl());
94+
assertNotNull(redirectView.getApplicationContext());
8895
}
8996

9097
@Test
@@ -101,18 +108,20 @@ public void addRedirectViewControllerWithCustomSettings() throws Exception {
101108

102109
assertEquals(308, this.response.getStatus());
103110
assertEquals("/redirectTo?a=b", response.getRedirectedUrl());
111+
assertNotNull(redirectView.getApplicationContext());
104112
}
105113

106114
@Test
107115
public void addStatusController() {
108116
this.registry.addStatusController("/path", HttpStatus.NOT_FOUND);
109117
ParameterizableViewController controller = getController("/path");
118+
110119
assertNull(controller.getViewName());
111120
assertEquals(HttpStatus.NOT_FOUND, controller.getStatusCode());
112121
assertTrue(controller.isStatusOnly());
122+
assertNotNull(controller.getApplicationContext());
113123
}
114124

115-
116125
@Test
117126
public void order() {
118127
this.registry.addViewController("/path");
@@ -124,24 +133,24 @@ public void order() {
124133
assertEquals(2, handlerMapping.getOrder());
125134
}
126135

136+
127137
private ParameterizableViewController getController(String path) {
128138
Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
129139
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get(path);
130140
assertNotNull(controller);
131141
return controller;
132142
}
133143

134-
private SimpleUrlHandlerMapping getHandlerMapping() {
135-
return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
136-
}
137-
138144
private RedirectView getRedirectView(String path) {
139-
ParameterizableViewController controller = getController("/path");
145+
ParameterizableViewController controller = getController(path);
140146
assertNull(controller.getViewName());
141147
assertNotNull(controller.getView());
142148
assertEquals(RedirectView.class, controller.getView().getClass());
143149
return (RedirectView) controller.getView();
144150
}
145151

152+
private SimpleUrlHandlerMapping getHandlerMapping() {
153+
return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
154+
}
146155

147156
}

0 commit comments

Comments
 (0)