Consistent configurer access in WebMvcConfigurationSupport
Issue: SPR-16017
(cherry picked from commit 40ba95f)
This commit is contained in:
parent
00c0d7847f
commit
a1a7c62127
|
|
@ -40,6 +40,7 @@ import org.springframework.web.context.request.NativeWebRequest;
|
||||||
* {@code MediaTypeFileExtensionResolver} instances.
|
* {@code MediaTypeFileExtensionResolver} instances.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
|
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
|
||||||
|
|
@ -66,6 +67,7 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
|
||||||
* A collection-based alternative to
|
* A collection-based alternative to
|
||||||
* {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}.
|
* {@link #ContentNegotiationManager(ContentNegotiationStrategy...)}.
|
||||||
* @param strategies the strategies to use
|
* @param strategies the strategies to use
|
||||||
|
* @since 3.2.2
|
||||||
*/
|
*/
|
||||||
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
|
public ContentNegotiationManager(Collection<ContentNegotiationStrategy> strategies) {
|
||||||
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
|
Assert.notEmpty(strategies, "At least one ContentNegotiationStrategy is expected");
|
||||||
|
|
@ -96,7 +98,7 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
|
||||||
/**
|
/**
|
||||||
* Find a {@code ContentNegotiationStrategy} of the given type.
|
* Find a {@code ContentNegotiationStrategy} of the given type.
|
||||||
* @param strategyType the strategy type
|
* @param strategyType the strategy type
|
||||||
* @return the first matching strategy or {@code null}.
|
* @return the first matching strategy, or {@code null} if none
|
||||||
* @since 4.3
|
* @since 4.3
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,7 @@ import org.springframework.web.context.ServletContextAware;
|
||||||
* {@link #setStrategies(List)}.
|
* {@link #setStrategies(List)}.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Brian Clozel
|
||||||
* @since 3.2
|
* @since 3.2
|
||||||
*/
|
*/
|
||||||
public class ContentNegotiationManagerFactoryBean
|
public class ContentNegotiationManagerFactoryBean
|
||||||
|
|
@ -293,6 +294,10 @@ public class ContentNegotiationManagerFactoryBean
|
||||||
build();
|
build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Actually build the {@link ContentNegotiationManager}.
|
||||||
|
* @since 5.0
|
||||||
|
*/
|
||||||
public ContentNegotiationManager build() {
|
public ContentNegotiationManager build() {
|
||||||
List<ContentNegotiationStrategy> strategies = new ArrayList<>();
|
List<ContentNegotiationStrategy> strategies = new ArrayList<>();
|
||||||
|
|
||||||
|
|
@ -303,8 +308,7 @@ public class ContentNegotiationManagerFactoryBean
|
||||||
if (this.favorPathExtension) {
|
if (this.favorPathExtension) {
|
||||||
PathExtensionContentNegotiationStrategy strategy;
|
PathExtensionContentNegotiationStrategy strategy;
|
||||||
if (this.servletContext != null && !useRegisteredExtensionsOnly()) {
|
if (this.servletContext != null && !useRegisteredExtensionsOnly()) {
|
||||||
strategy = new ServletPathExtensionContentNegotiationStrategy(
|
strategy = new ServletPathExtensionContentNegotiationStrategy(this.servletContext, this.mediaTypes);
|
||||||
this.servletContext, this.mediaTypes);
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
|
strategy = new PathExtensionContentNegotiationStrategy(this.mediaTypes);
|
||||||
|
|
@ -317,14 +321,13 @@ public class ContentNegotiationManagerFactoryBean
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.favorParameter) {
|
if (this.favorParameter) {
|
||||||
ParameterContentNegotiationStrategy strategy =
|
ParameterContentNegotiationStrategy strategy = new ParameterContentNegotiationStrategy(this.mediaTypes);
|
||||||
new ParameterContentNegotiationStrategy(this.mediaTypes);
|
|
||||||
strategy.setParameterName(this.parameterName);
|
strategy.setParameterName(this.parameterName);
|
||||||
if (this.useRegisteredExtensionsOnly != null) {
|
if (this.useRegisteredExtensionsOnly != null) {
|
||||||
strategy.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
|
strategy.setUseRegisteredExtensionsOnly(this.useRegisteredExtensionsOnly);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
strategy.setUseRegisteredExtensionsOnly(true); // backwards compatibility
|
strategy.setUseRegisteredExtensionsOnly(true); // backwards compatibility
|
||||||
}
|
}
|
||||||
strategies.add(strategy);
|
strategies.add(strategy);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -186,8 +186,8 @@ public class ContentNegotiationConfigurer {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}, which
|
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}
|
||||||
* has reverse behavior.
|
* which has reverse behavior
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public ContentNegotiationConfigurer useJaf(boolean useJaf) {
|
public ContentNegotiationConfigurer useJaf(boolean useJaf) {
|
||||||
|
|
@ -262,7 +262,12 @@ public class ContentNegotiationConfigurer {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
protected ContentNegotiationManager getContentNegotiationManager() throws Exception {
|
/**
|
||||||
|
* Build a {@link ContentNegotiationManager} based on this configurer's settings.
|
||||||
|
* @since 4.3.12
|
||||||
|
* @see ContentNegotiationManagerFactoryBean#getObject()
|
||||||
|
*/
|
||||||
|
protected ContentNegotiationManager buildContentNegotiationManager() {
|
||||||
this.factory.addMediaTypes(this.mediaTypes);
|
this.factory.addMediaTypes(this.mediaTypes);
|
||||||
return this.factory.build();
|
return this.factory.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,15 +16,12 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.config.annotation;
|
package org.springframework.web.servlet.config.annotation;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.HttpRequestHandler;
|
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
|
|
||||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||||
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
|
import org.springframework.web.servlet.resource.DefaultServletHttpRequestHandler;
|
||||||
|
|
||||||
|
|
@ -86,23 +83,22 @@ public class DefaultServletHandlerConfigurer {
|
||||||
this.handler.setServletContext(this.servletContext);
|
this.handler.setServletContext(this.servletContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
|
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
|
||||||
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
|
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
|
||||||
* or {@code null} if default servlet handling was not been enabled.
|
* or {@code null} if default servlet handling was not been enabled.
|
||||||
|
* @since 4.3.12
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
protected AbstractHandlerMapping getHandlerMapping() {
|
protected SimpleUrlHandlerMapping buildHandlerMapping() {
|
||||||
if (this.handler == null) {
|
if (this.handler == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, HttpRequestHandler> urlMap = new HashMap<>();
|
|
||||||
urlMap.put("/**", this.handler);
|
|
||||||
|
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||||
|
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
|
||||||
handlerMapping.setOrder(Integer.MAX_VALUE);
|
handlerMapping.setOrder(Integer.MAX_VALUE);
|
||||||
handlerMapping.setUrlMap(urlMap);
|
|
||||||
return handlerMapping;
|
return handlerMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,9 +84,7 @@ public class PathMatchConfigurer {
|
||||||
* <p>By default this is set to "false".
|
* <p>By default this is set to "false".
|
||||||
* @see WebMvcConfigurer#configureContentNegotiation
|
* @see WebMvcConfigurer#configureContentNegotiation
|
||||||
*/
|
*/
|
||||||
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(
|
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
|
||||||
Boolean registeredSuffixPatternMatch) {
|
|
||||||
|
|
||||||
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
|
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -24,7 +24,6 @@ import java.util.Map;
|
||||||
import org.springframework.context.ApplicationContext;
|
import org.springframework.context.ApplicationContext;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
|
|
||||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -37,14 +36,23 @@ import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||||
*/
|
*/
|
||||||
public class ViewControllerRegistry {
|
public class ViewControllerRegistry {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
private final List<ViewControllerRegistration> registrations = new ArrayList<>(4);
|
private final List<ViewControllerRegistration> registrations = new ArrayList<>(4);
|
||||||
|
|
||||||
private final List<RedirectViewControllerRegistration> redirectRegistrations = new ArrayList<>(10);
|
private final List<RedirectViewControllerRegistration> redirectRegistrations = new ArrayList<>(10);
|
||||||
|
|
||||||
private int order = 1;
|
private int order = 1;
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private ApplicationContext applicationContext;
|
/**
|
||||||
|
* Class constructor with {@link ApplicationContext}.
|
||||||
|
* @since 4.3.12
|
||||||
|
*/
|
||||||
|
public ViewControllerRegistry(@Nullable ApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -97,30 +105,29 @@ public class ViewControllerRegistry {
|
||||||
this.order = order;
|
this.order = order;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setApplicationContext(@Nullable ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the {@code HandlerMapping} that contains the registered view
|
* Return the {@code HandlerMapping} that contains the registered view
|
||||||
* controller mappings, or {@code null} for no registrations.
|
* controller mappings, or {@code null} for no registrations.
|
||||||
|
* @since 4.3.12
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
protected AbstractHandlerMapping getHandlerMapping() {
|
protected SimpleUrlHandlerMapping buildHandlerMapping() {
|
||||||
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
|
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Map<String, Object> urlMap = new LinkedHashMap<>();
|
|
||||||
|
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
|
||||||
for (ViewControllerRegistration registration : this.registrations) {
|
for (ViewControllerRegistration registration : this.registrations) {
|
||||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||||
}
|
}
|
||||||
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
|
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
|
||||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||||
handlerMapping.setOrder(this.order);
|
|
||||||
handlerMapping.setUrlMap(urlMap);
|
handlerMapping.setUrlMap(urlMap);
|
||||||
|
handlerMapping.setOrder(this.order);
|
||||||
return handlerMapping;
|
return handlerMapping;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,12 @@ import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||||
*/
|
*/
|
||||||
public class ViewResolverRegistry {
|
public class ViewResolverRegistry {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ContentNegotiationManager contentNegotiationManager;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ApplicationContext applicationContext;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private ContentNegotiatingViewResolver contentNegotiatingResolver;
|
private ContentNegotiatingViewResolver contentNegotiatingResolver;
|
||||||
|
|
||||||
|
|
@ -62,20 +68,18 @@ public class ViewResolverRegistry {
|
||||||
@Nullable
|
@Nullable
|
||||||
private Integer order;
|
private Integer order;
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private ContentNegotiationManager contentNegotiationManager;
|
|
||||||
|
|
||||||
@Nullable
|
/**
|
||||||
private ApplicationContext applicationContext;
|
* Class constructor with {@link ContentNegotiationManager} and {@link ApplicationContext}.
|
||||||
|
* @since 4.3.12
|
||||||
|
*/
|
||||||
|
public ViewResolverRegistry(
|
||||||
|
ContentNegotiationManager contentNegotiationManager, @Nullable ApplicationContext context) {
|
||||||
|
|
||||||
|
|
||||||
protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
|
|
||||||
this.contentNegotiationManager = contentNegotiationManager;
|
this.contentNegotiationManager = contentNegotiationManager;
|
||||||
|
this.applicationContext = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void setApplicationContext(ApplicationContext applicationContext) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Whether any view resolvers have been registered.
|
* Whether any view resolvers have been registered.
|
||||||
|
|
@ -84,7 +88,6 @@ public class ViewResolverRegistry {
|
||||||
return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty());
|
return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other
|
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other
|
||||||
* configured view resolvers and select among all selected Views based on
|
* configured view resolvers and select among all selected Views based on
|
||||||
|
|
@ -305,6 +308,7 @@ public class ViewResolverRegistry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration {
|
private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration {
|
||||||
|
|
||||||
public GroovyMarkupRegistration() {
|
public GroovyMarkupRegistration() {
|
||||||
|
|
@ -313,6 +317,7 @@ public class ViewResolverRegistry {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class ScriptRegistration extends UrlBasedViewResolverRegistration {
|
private static class ScriptRegistration extends UrlBasedViewResolverRegistration {
|
||||||
|
|
||||||
public ScriptRegistration() {
|
public ScriptRegistration() {
|
||||||
|
|
|
||||||
|
|
@ -391,12 +391,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||||
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
|
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
|
||||||
configurer.mediaTypes(getDefaultMediaTypes());
|
configurer.mediaTypes(getDefaultMediaTypes());
|
||||||
configureContentNegotiation(configurer);
|
configureContentNegotiation(configurer);
|
||||||
try {
|
this.contentNegotiationManager = configurer.buildContentNegotiationManager();
|
||||||
this.contentNegotiationManager = configurer.getContentNegotiationManager();
|
|
||||||
}
|
|
||||||
catch (Throwable ex) {
|
|
||||||
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return this.contentNegotiationManager;
|
return this.contentNegotiationManager;
|
||||||
}
|
}
|
||||||
|
|
@ -436,11 +431,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public HandlerMapping viewControllerHandlerMapping() {
|
public HandlerMapping viewControllerHandlerMapping() {
|
||||||
ViewControllerRegistry registry = new ViewControllerRegistry();
|
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
|
||||||
registry.setApplicationContext(this.applicationContext);
|
|
||||||
addViewControllers(registry);
|
addViewControllers(registry);
|
||||||
|
|
||||||
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
|
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
|
||||||
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
|
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
|
||||||
handlerMapping.setPathMatcher(mvcPathMatcher());
|
handlerMapping.setPathMatcher(mvcPathMatcher());
|
||||||
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
|
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
|
||||||
|
|
@ -531,9 +525,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||||
Assert.state(this.servletContext != null, "No ServletContext set");
|
Assert.state(this.servletContext != null, "No ServletContext set");
|
||||||
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
|
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
|
||||||
configureDefaultServletHandling(configurer);
|
configureDefaultServletHandling(configurer);
|
||||||
AbstractHandlerMapping handlerMapping = configurer.getHandlerMapping();
|
|
||||||
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
|
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
|
||||||
return handlerMapping;
|
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -975,14 +969,11 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public ViewResolver mvcViewResolver() {
|
public ViewResolver mvcViewResolver() {
|
||||||
ViewResolverRegistry registry = new ViewResolverRegistry();
|
ViewResolverRegistry registry = new ViewResolverRegistry(
|
||||||
registry.setContentNegotiationManager(mvcContentNegotiationManager());
|
mvcContentNegotiationManager(), this.applicationContext);
|
||||||
if (this.applicationContext != null) {
|
|
||||||
registry.setApplicationContext(this.applicationContext);
|
|
||||||
}
|
|
||||||
configureViewResolvers(registry);
|
configureViewResolvers(registry);
|
||||||
|
|
||||||
if (registry.getViewResolvers().isEmpty()) {
|
if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
|
||||||
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||||
this.applicationContext, ViewResolver.class, true, false);
|
this.applicationContext, ViewResolver.class, true, false);
|
||||||
if (names.length == 1) {
|
if (names.length == 1) {
|
||||||
|
|
@ -993,7 +984,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||||
ViewResolverComposite composite = new ViewResolverComposite();
|
ViewResolverComposite composite = new ViewResolverComposite();
|
||||||
composite.setOrder(registry.getOrder());
|
composite.setOrder(registry.getOrder());
|
||||||
composite.setViewResolvers(registry.getViewResolvers());
|
composite.setViewResolvers(registry.getViewResolvers());
|
||||||
composite.setApplicationContext(this.applicationContext);
|
if (this.applicationContext != null) {
|
||||||
|
composite.setApplicationContext(this.applicationContext);
|
||||||
|
}
|
||||||
if (this.servletContext != null) {
|
if (this.servletContext != null) {
|
||||||
composite.setServletContext(this.servletContext);
|
composite.setServletContext(this.servletContext);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
package org.springframework.web.servlet.config.annotation;
|
package org.springframework.web.servlet.config.annotation;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -28,7 +29,7 @@ import org.springframework.web.accept.FixedContentNegotiationStrategy;
|
||||||
import org.springframework.web.context.request.NativeWebRequest;
|
import org.springframework.web.context.request.NativeWebRequest;
|
||||||
import org.springframework.web.context.request.ServletWebRequest;
|
import org.springframework.web.context.request.ServletWebRequest;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test fixture for {@link ContentNegotiationConfigurer} tests.
|
* Test fixture for {@link ContentNegotiationConfigurer} tests.
|
||||||
|
|
@ -42,6 +43,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
|
|
||||||
private MockHttpServletRequest servletRequest;
|
private MockHttpServletRequest servletRequest;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setup() {
|
public void setup() {
|
||||||
this.servletRequest = new MockHttpServletRequest();
|
this.servletRequest = new MockHttpServletRequest();
|
||||||
|
|
@ -49,9 +51,10 @@ public class ContentNegotiationConfigurerTests {
|
||||||
this.configurer = new ContentNegotiationConfigurer(this.servletRequest.getServletContext());
|
this.configurer = new ContentNegotiationConfigurer(this.servletRequest.getServletContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void defaultSettings() throws Exception {
|
public void defaultSettings() throws Exception {
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
this.servletRequest.setRequestURI("/flower.gif");
|
this.servletRequest.setRequestURI("/flower.gif");
|
||||||
|
|
||||||
|
|
@ -74,7 +77,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void addMediaTypes() throws Exception {
|
public void addMediaTypes() throws Exception {
|
||||||
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
|
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
this.servletRequest.setRequestURI("/flower.json");
|
this.servletRequest.setRequestURI("/flower.json");
|
||||||
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
||||||
|
|
@ -85,7 +88,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
this.configurer.favorParameter(true);
|
this.configurer.favorParameter(true);
|
||||||
this.configurer.parameterName("f");
|
this.configurer.parameterName("f");
|
||||||
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
|
this.configurer.mediaTypes(Collections.singletonMap("json", MediaType.APPLICATION_JSON));
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
this.servletRequest.setRequestURI("/flower");
|
this.servletRequest.setRequestURI("/flower");
|
||||||
this.servletRequest.addParameter("f", "json");
|
this.servletRequest.addParameter("f", "json");
|
||||||
|
|
@ -96,7 +99,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void ignoreAcceptHeader() throws Exception {
|
public void ignoreAcceptHeader() throws Exception {
|
||||||
this.configurer.ignoreAcceptHeader(true);
|
this.configurer.ignoreAcceptHeader(true);
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
this.servletRequest.setRequestURI("/flower");
|
this.servletRequest.setRequestURI("/flower");
|
||||||
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
|
this.servletRequest.addHeader("Accept", MediaType.IMAGE_GIF_VALUE);
|
||||||
|
|
@ -107,7 +110,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void setDefaultContentType() throws Exception {
|
public void setDefaultContentType() throws Exception {
|
||||||
this.configurer.defaultContentType(MediaType.APPLICATION_JSON);
|
this.configurer.defaultContentType(MediaType.APPLICATION_JSON);
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
||||||
}
|
}
|
||||||
|
|
@ -115,7 +118,7 @@ public class ContentNegotiationConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void setMultipleDefaultContentTypes() throws Exception {
|
public void setMultipleDefaultContentTypes() throws Exception {
|
||||||
this.configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.ALL);
|
this.configurer.defaultContentType(MediaType.APPLICATION_JSON, MediaType.ALL);
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL), manager.resolveMediaTypes(this.webRequest));
|
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON, MediaType.ALL), manager.resolveMediaTypes(this.webRequest));
|
||||||
}
|
}
|
||||||
|
|
@ -123,8 +126,9 @@ public class ContentNegotiationConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void setDefaultContentTypeStrategy() throws Exception {
|
public void setDefaultContentTypeStrategy() throws Exception {
|
||||||
this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
|
this.configurer.defaultContentTypeStrategy(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
|
||||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
ContentNegotiationManager manager = this.configurer.buildContentNegotiationManager();
|
||||||
|
|
||||||
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
assertEquals(MediaType.APPLICATION_JSON, manager.resolveMediaTypes(this.webRequest).get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2012 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -43,23 +43,25 @@ public class DefaultServletHandlerConfigurerTests {
|
||||||
|
|
||||||
private MockHttpServletResponse response;
|
private MockHttpServletResponse response;
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setup() {
|
||||||
response = new MockHttpServletResponse();
|
response = new MockHttpServletResponse();
|
||||||
servletContext = new DispatchingMockServletContext();
|
servletContext = new DispatchingMockServletContext();
|
||||||
configurer = new DefaultServletHandlerConfigurer(servletContext);
|
configurer = new DefaultServletHandlerConfigurer(servletContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void notEnabled() {
|
public void notEnabled() {
|
||||||
assertNull(configurer.getHandlerMapping());
|
assertNull(configurer.buildHandlerMapping());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void enable() throws Exception {
|
public void enable() throws Exception {
|
||||||
configurer.enable();
|
configurer.enable();
|
||||||
SimpleUrlHandlerMapping getHandlerMapping = getHandlerMapping();
|
SimpleUrlHandlerMapping getHandlerMapping = configurer.buildHandlerMapping();
|
||||||
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping;
|
SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
|
||||||
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
|
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
|
||||||
|
|
||||||
assertNotNull(handler);
|
assertNotNull(handler);
|
||||||
|
|
@ -75,7 +77,7 @@ public class DefaultServletHandlerConfigurerTests {
|
||||||
@Test
|
@Test
|
||||||
public void enableWithServletName() throws Exception {
|
public void enableWithServletName() throws Exception {
|
||||||
configurer.enable("defaultServlet");
|
configurer.enable("defaultServlet");
|
||||||
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
|
SimpleUrlHandlerMapping handlerMapping = configurer.buildHandlerMapping();
|
||||||
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
|
DefaultServletHttpRequestHandler handler = (DefaultServletHttpRequestHandler) handlerMapping.getUrlMap().get("/**");
|
||||||
|
|
||||||
assertNotNull(handler);
|
assertNotNull(handler);
|
||||||
|
|
@ -88,6 +90,7 @@ public class DefaultServletHandlerConfigurerTests {
|
||||||
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
|
assertEquals("The request was not forwarded", expected, response.getForwardedUrl());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private static class DispatchingMockServletContext extends MockServletContext {
|
private static class DispatchingMockServletContext extends MockServletContext {
|
||||||
|
|
||||||
private String url;
|
private String url;
|
||||||
|
|
@ -99,8 +102,4 @@ public class DefaultServletHandlerConfigurerTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private SimpleUrlHandlerMapping getHandlerMapping() {
|
|
||||||
return (SimpleUrlHandlerMapping) configurer.getHandlerMapping();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -47,16 +47,16 @@ public class ViewControllerRegistryTests {
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setup() {
|
||||||
this.registry = new ViewControllerRegistry();
|
this.registry = new ViewControllerRegistry(new StaticApplicationContext());
|
||||||
this.registry.setApplicationContext(new StaticApplicationContext());
|
|
||||||
this.request = new MockHttpServletRequest("GET", "/");
|
this.request = new MockHttpServletRequest("GET", "/");
|
||||||
this.response = new MockHttpServletResponse();
|
this.response = new MockHttpServletResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void noViewControllers() throws Exception {
|
public void noViewControllers() {
|
||||||
assertNull(this.registry.getHandlerMapping());
|
assertNull(this.registry.buildHandlerMapping());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
@ -125,17 +125,17 @@ public class ViewControllerRegistryTests {
|
||||||
@Test
|
@Test
|
||||||
public void order() {
|
public void order() {
|
||||||
this.registry.addViewController("/path");
|
this.registry.addViewController("/path");
|
||||||
SimpleUrlHandlerMapping handlerMapping = getHandlerMapping();
|
SimpleUrlHandlerMapping handlerMapping = this.registry.buildHandlerMapping();
|
||||||
assertEquals(1, handlerMapping.getOrder());
|
assertEquals(1, handlerMapping.getOrder());
|
||||||
|
|
||||||
this.registry.setOrder(2);
|
this.registry.setOrder(2);
|
||||||
handlerMapping = getHandlerMapping();
|
handlerMapping = this.registry.buildHandlerMapping();
|
||||||
assertEquals(2, handlerMapping.getOrder());
|
assertEquals(2, handlerMapping.getOrder());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private ParameterizableViewController getController(String path) {
|
private ParameterizableViewController getController(String path) {
|
||||||
Map<String, ?> urlMap = getHandlerMapping().getUrlMap();
|
Map<String, ?> urlMap = this.registry.buildHandlerMapping().getUrlMap();
|
||||||
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get(path);
|
ParameterizableViewController controller = (ParameterizableViewController) urlMap.get(path);
|
||||||
assertNotNull(controller);
|
assertNotNull(controller);
|
||||||
return controller;
|
return controller;
|
||||||
|
|
@ -149,8 +149,4 @@ public class ViewControllerRegistryTests {
|
||||||
return (RedirectView) controller.getView();
|
return (RedirectView) controller.getView();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SimpleUrlHandlerMapping getHandlerMapping() {
|
|
||||||
return (SimpleUrlHandlerMapping) this.registry.getHandlerMapping();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2002-2015 the original author or authors.
|
* Copyright 2002-2017 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
|
@ -54,15 +54,14 @@ public class ViewResolverRegistryTests {
|
||||||
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() {
|
public void setup() {
|
||||||
StaticWebApplicationContext context = new StaticWebApplicationContext();
|
StaticWebApplicationContext context = new StaticWebApplicationContext();
|
||||||
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
|
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class);
|
||||||
context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
|
context.registerSingleton("tilesConfigurer", TilesConfigurer.class);
|
||||||
context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
|
context.registerSingleton("groovyMarkupConfigurer", GroovyMarkupConfigurer.class);
|
||||||
context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
|
context.registerSingleton("scriptTemplateConfigurer", ScriptTemplateConfigurer.class);
|
||||||
this.registry = new ViewResolverRegistry();
|
|
||||||
this.registry.setApplicationContext(context);
|
this.registry = new ViewResolverRegistry(new ContentNegotiationManager(), context);
|
||||||
this.registry.setContentNegotiationManager(new ContentNegotiationManager());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue