Configure view controllers with ApplicationContext

Issue: SPR-13762
This commit is contained in:
Rossen Stoyanchev
2015-12-04 13:09:37 -05:00
parent 3d1ae9c604
commit 153a23dbf9
5 changed files with 39 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
package org.springframework.web.servlet.config.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
@@ -81,6 +82,10 @@ public class RedirectViewControllerRegistration {
return this;
}
protected void setApplicationContext(ApplicationContext applicationContext) {
this.controller.setApplicationContext(applicationContext);
this.redirectView.setApplicationContext(applicationContext);
}
protected String getUrlPath() {
return this.urlPath;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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.
@@ -16,6 +16,7 @@
package org.springframework.web.servlet.config.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.util.Assert;
import org.springframework.web.servlet.RequestToViewNameTranslator;
@@ -65,6 +66,9 @@ public class ViewControllerRegistration {
this.controller.setViewName(viewName);
}
protected void setApplicationContext(ApplicationContext applicationContext) {
this.controller.setApplicationContext(applicationContext);
}
protected String getUrlPath() {
return this.urlPath;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2015 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,6 +21,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
@@ -42,6 +43,8 @@ public class ViewControllerRegistry {
private int order = 1;
private ApplicationContext applicationContext;
/**
* Map a view controller to the given URL path (or pattern) in order to render
@@ -49,6 +52,7 @@ public class ViewControllerRegistry {
*/
public ViewControllerRegistration addViewController(String urlPath) {
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
registration.setApplicationContext(this.applicationContext);
this.registrations.add(registration);
return registration;
}
@@ -61,6 +65,7 @@ public class ViewControllerRegistry {
*/
public RedirectViewControllerRegistration addRedirectViewController(String urlPath, String redirectUrl) {
RedirectViewControllerRegistration registration = new RedirectViewControllerRegistration(urlPath, redirectUrl);
registration.setApplicationContext(this.applicationContext);
this.redirectRegistrations.add(registration);
return registration;
}
@@ -72,6 +77,7 @@ public class ViewControllerRegistry {
*/
public void addStatusController(String urlPath, HttpStatus statusCode) {
ViewControllerRegistration registration = new ViewControllerRegistration(urlPath);
registration.setApplicationContext(this.applicationContext);
registration.setStatusCode(statusCode);
registration.getViewController().setStatusOnly(true);
this.registrations.add(registration);
@@ -87,6 +93,10 @@ public class ViewControllerRegistry {
this.order = order;
}
protected void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
/**
* Return the {@code HandlerMapping} that contains the registered view

View File

@@ -364,6 +364,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
@Bean
public HandlerMapping viewControllerHandlerMapping() {
ViewControllerRegistry registry = new ViewControllerRegistry();
registry.setApplicationContext(this.applicationContext);
addViewControllers(registry);
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();