Consistent configurer access in WebMvcConfigurationSupport
Issue: SPR-16017
(cherry picked from commit 40ba95f)
This commit is contained in:
@@ -186,8 +186,8 @@ public class ContentNegotiationConfigurer {
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}, which
|
||||
* has reverse behavior.
|
||||
* @deprecated as of 5.0, in favor of {@link #useRegisteredExtensionsOnly(boolean)}
|
||||
* which has reverse behavior
|
||||
*/
|
||||
@Deprecated
|
||||
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);
|
||||
return this.factory.build();
|
||||
}
|
||||
|
||||
@@ -16,15 +16,12 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Collections;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
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.resource.DefaultServletHttpRequestHandler;
|
||||
|
||||
@@ -86,23 +83,22 @@ public class DefaultServletHandlerConfigurer {
|
||||
this.handler.setServletContext(this.servletContext);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
|
||||
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
|
||||
* or {@code null} if default servlet handling was not been enabled.
|
||||
* @since 4.3.12
|
||||
*/
|
||||
@Nullable
|
||||
protected AbstractHandlerMapping getHandlerMapping() {
|
||||
protected SimpleUrlHandlerMapping buildHandlerMapping() {
|
||||
if (this.handler == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Map<String, HttpRequestHandler> urlMap = new HashMap<>();
|
||||
urlMap.put("/**", this.handler);
|
||||
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
|
||||
handlerMapping.setOrder(Integer.MAX_VALUE);
|
||||
handlerMapping.setUrlMap(urlMap);
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
|
||||
@@ -84,9 +84,7 @@ public class PathMatchConfigurer {
|
||||
* <p>By default this is set to "false".
|
||||
* @see WebMvcConfigurer#configureContentNegotiation
|
||||
*/
|
||||
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(
|
||||
Boolean registeredSuffixPatternMatch) {
|
||||
|
||||
public PathMatchConfigurer setUseRegisteredSuffixPatternMatch(Boolean registeredSuffixPatternMatch) {
|
||||
this.registeredSuffixPatternMatch = registeredSuffixPatternMatch;
|
||||
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");
|
||||
* 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.http.HttpStatus;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.web.servlet.handler.AbstractHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
|
||||
/**
|
||||
@@ -37,14 +36,23 @@ import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
*/
|
||||
public class ViewControllerRegistry {
|
||||
|
||||
@Nullable
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private final List<ViewControllerRegistration> registrations = new ArrayList<>(4);
|
||||
|
||||
private final List<RedirectViewControllerRegistration> redirectRegistrations = new ArrayList<>(10);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
protected void setApplicationContext(@Nullable ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the {@code HandlerMapping} that contains the registered view
|
||||
* controller mappings, or {@code null} for no registrations.
|
||||
* @since 4.3.12
|
||||
*/
|
||||
@Nullable
|
||||
protected AbstractHandlerMapping getHandlerMapping() {
|
||||
protected SimpleUrlHandlerMapping buildHandlerMapping() {
|
||||
if (this.registrations.isEmpty() && this.redirectRegistrations.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> urlMap = new LinkedHashMap<>();
|
||||
|
||||
Map<String, Object> urlMap = new LinkedHashMap<String, Object>();
|
||||
for (ViewControllerRegistration registration : this.registrations) {
|
||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||
}
|
||||
for (RedirectViewControllerRegistration registration : this.redirectRegistrations) {
|
||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||
}
|
||||
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setOrder(this.order);
|
||||
handlerMapping.setUrlMap(urlMap);
|
||||
handlerMapping.setOrder(this.order);
|
||||
return handlerMapping;
|
||||
}
|
||||
|
||||
|
||||
@@ -54,6 +54,12 @@ import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
*/
|
||||
public class ViewResolverRegistry {
|
||||
|
||||
@Nullable
|
||||
private ContentNegotiationManager contentNegotiationManager;
|
||||
|
||||
@Nullable
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
@Nullable
|
||||
private ContentNegotiatingViewResolver contentNegotiatingResolver;
|
||||
|
||||
@@ -62,20 +68,18 @@ public class ViewResolverRegistry {
|
||||
@Nullable
|
||||
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.applicationContext = context;
|
||||
}
|
||||
|
||||
protected void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether any view resolvers have been registered.
|
||||
@@ -84,7 +88,6 @@ public class ViewResolverRegistry {
|
||||
return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other
|
||||
* configured view resolvers and select among all selected Views based on
|
||||
@@ -305,6 +308,7 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class GroovyMarkupRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
public GroovyMarkupRegistration() {
|
||||
@@ -313,6 +317,7 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class ScriptRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
public ScriptRegistration() {
|
||||
|
||||
@@ -391,12 +391,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
ContentNegotiationConfigurer configurer = new ContentNegotiationConfigurer(this.servletContext);
|
||||
configurer.mediaTypes(getDefaultMediaTypes());
|
||||
configureContentNegotiation(configurer);
|
||||
try {
|
||||
this.contentNegotiationManager = configurer.getContentNegotiationManager();
|
||||
}
|
||||
catch (Throwable ex) {
|
||||
throw new BeanInitializationException("Could not create ContentNegotiationManager", ex);
|
||||
}
|
||||
this.contentNegotiationManager = configurer.buildContentNegotiationManager();
|
||||
}
|
||||
return this.contentNegotiationManager;
|
||||
}
|
||||
@@ -436,11 +431,10 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
*/
|
||||
@Bean
|
||||
public HandlerMapping viewControllerHandlerMapping() {
|
||||
ViewControllerRegistry registry = new ViewControllerRegistry();
|
||||
registry.setApplicationContext(this.applicationContext);
|
||||
ViewControllerRegistry registry = new ViewControllerRegistry(this.applicationContext);
|
||||
addViewControllers(registry);
|
||||
|
||||
AbstractHandlerMapping handlerMapping = registry.getHandlerMapping();
|
||||
AbstractHandlerMapping handlerMapping = registry.buildHandlerMapping();
|
||||
handlerMapping = (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
|
||||
handlerMapping.setPathMatcher(mvcPathMatcher());
|
||||
handlerMapping.setUrlPathHelper(mvcUrlPathHelper());
|
||||
@@ -531,9 +525,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
Assert.state(this.servletContext != null, "No ServletContext set");
|
||||
DefaultServletHandlerConfigurer configurer = new DefaultServletHandlerConfigurer(this.servletContext);
|
||||
configureDefaultServletHandling(configurer);
|
||||
AbstractHandlerMapping handlerMapping = configurer.getHandlerMapping();
|
||||
handlerMapping = handlerMapping != null ? handlerMapping : new EmptyHandlerMapping();
|
||||
return handlerMapping;
|
||||
|
||||
HandlerMapping handlerMapping = configurer.buildHandlerMapping();
|
||||
return (handlerMapping != null ? handlerMapping : new EmptyHandlerMapping());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -975,14 +969,11 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
*/
|
||||
@Bean
|
||||
public ViewResolver mvcViewResolver() {
|
||||
ViewResolverRegistry registry = new ViewResolverRegistry();
|
||||
registry.setContentNegotiationManager(mvcContentNegotiationManager());
|
||||
if (this.applicationContext != null) {
|
||||
registry.setApplicationContext(this.applicationContext);
|
||||
}
|
||||
ViewResolverRegistry registry = new ViewResolverRegistry(
|
||||
mvcContentNegotiationManager(), this.applicationContext);
|
||||
configureViewResolvers(registry);
|
||||
|
||||
if (registry.getViewResolvers().isEmpty()) {
|
||||
if (registry.getViewResolvers().isEmpty() && this.applicationContext != null) {
|
||||
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
this.applicationContext, ViewResolver.class, true, false);
|
||||
if (names.length == 1) {
|
||||
@@ -993,7 +984,9 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
ViewResolverComposite composite = new ViewResolverComposite();
|
||||
composite.setOrder(registry.getOrder());
|
||||
composite.setViewResolvers(registry.getViewResolvers());
|
||||
composite.setApplicationContext(this.applicationContext);
|
||||
if (this.applicationContext != null) {
|
||||
composite.setApplicationContext(this.applicationContext);
|
||||
}
|
||||
if (this.servletContext != null) {
|
||||
composite.setServletContext(this.servletContext);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user