Update ViewResolver registration classes
Following the separation of FreeMarker/Velocity/TilesConfigurer-related configuration via separate interface, simplify and streamline the view registration helper classes which no longer have much difference (most are UrlBasedViewResolver's). Updates to Javadoc and tests. Issue: SPR-7093
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.BeanNameViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.BeanNameViewResolver} bean.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class BeanNameRegistration extends ViewResolutionRegistration<BeanNameViewResolver> {
|
||||
|
||||
public BeanNameRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new BeanNameViewResolver());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,71 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a {@link ContentNegotiatingViewResolver} bean.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ContentNegotiatingRegistration extends ViewResolutionRegistration<ContentNegotiatingViewResolver> {
|
||||
|
||||
private List<View> defaultViews;
|
||||
|
||||
public ContentNegotiatingRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new ContentNegotiatingViewResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate whether a {@link javax.servlet.http.HttpServletResponse#SC_NOT_ACCEPTABLE 406 Not Acceptable}
|
||||
* status code should be returned if no suitable view can be found.
|
||||
*
|
||||
* @see ContentNegotiatingViewResolver#setUseNotAcceptableStatusCode(boolean)
|
||||
*/
|
||||
public ContentNegotiatingRegistration useNotAcceptable(boolean useNotAcceptable) {
|
||||
this.viewResolver.setUseNotAcceptableStatusCode(useNotAcceptable);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Set the default views to use when a more specific view can not be obtained
|
||||
* from the {@link org.springframework.web.servlet.ViewResolver} chain.
|
||||
*
|
||||
* @see ContentNegotiatingViewResolver#setDefaultViews(java.util.List)
|
||||
*/
|
||||
public ContentNegotiatingRegistration defaultViews(View... defaultViews) {
|
||||
if(this.defaultViews == null) {
|
||||
this.defaultViews = new ArrayList<View>();
|
||||
}
|
||||
this.defaultViews.addAll(Arrays.asList(defaultViews));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ContentNegotiatingViewResolver getViewResolver() {
|
||||
this.viewResolver.setDefaultViews(this.defaultViews);
|
||||
return super.getViewResolver();
|
||||
}
|
||||
}
|
||||
@@ -76,8 +76,8 @@ public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void configureViewResolution(ViewResolutionRegistry registry) {
|
||||
this.configurers.configureViewResolution(registry);
|
||||
protected void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
this.configurers.configureViewResolvers(registry);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -21,8 +21,8 @@ import java.lang.annotation.Target;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* Add this annotation to an {@code @Configuration} class to have the Spring MVC
|
||||
* configuration defined in {@link WebMvcConfigurationSupport} imported:
|
||||
* Adding this annotation to an {@code @Configuration} class imports the Spring MVC
|
||||
* configuration from {@link WebMvcConfigurationSupport}, e.g.:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
@@ -32,9 +32,14 @@ import org.springframework.context.annotation.Import;
|
||||
*
|
||||
* }
|
||||
* </pre>
|
||||
* <p>Customize the imported configuration by implementing the
|
||||
* {@link WebMvcConfigurer} interface or more likely by extending the
|
||||
* {@link WebMvcConfigurerAdapter} base class and overriding individual methods:
|
||||
*
|
||||
* <p>As of 4.1 this annotation may also import {@link WebMvcFreeMarkerConfiguration},
|
||||
* {@link WebMvcVelocityConfiguration}, or {@link WebMvcTilesConfiguration} if
|
||||
* those libraries are found on the classpath.
|
||||
*
|
||||
* <p>To customize the imported configuration, implement the interface
|
||||
* {@link WebMvcConfigurer} or more likely extend the empty method base class
|
||||
* {@link WebMvcConfigurerAdapter} and override individual methods, e.g.:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
@@ -56,10 +61,13 @@ import org.springframework.context.annotation.Import;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>If the customization options of {@link WebMvcConfigurer} do not expose
|
||||
* something you need to configure, consider removing the {@code @EnableWebMvc}
|
||||
* annotation and extending directly from {@link WebMvcConfigurationSupport}
|
||||
* overriding selected {@code @Bean} methods:
|
||||
* <p>To customize the FreeMarker, Velocity, or Tiles configuration, additionally
|
||||
* implement {@link FreeMarkerWebMvcConfigurer}, {@link VelocityWebMvcConfigurer},
|
||||
* and/or {@link TilesWebMvcConfigurer}.
|
||||
*
|
||||
* <p>If {@link WebMvcConfigurer} does not expose some advanced setting that
|
||||
* needs to be configured, consider removing the {@code @EnableWebMvc}
|
||||
* annotation and extending directly from {@link WebMvcConfigurationSupport}, e.g.:
|
||||
*
|
||||
* <pre class="code">
|
||||
* @Configuration
|
||||
@@ -79,17 +87,23 @@ import org.springframework.context.annotation.Import;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* @see WebMvcConfigurer
|
||||
* @see WebMvcConfigurerAdapter
|
||||
* <p>When the {@code @EnableWebMvc} annotation is removed, the FreeMarker,
|
||||
* Velocity, and Tiles configuration is no longer automatically imported and need
|
||||
* to be imported explicitly.
|
||||
*
|
||||
* @author Dave Syer
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Sebastien Deleuze
|
||||
* @since 3.1
|
||||
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
||||
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
|
||||
* @see org.springframework.web.servlet.config.annotation.FreeMarkerWebMvcConfigurer
|
||||
* @see org.springframework.web.servlet.config.annotation.VelocityWebMvcConfigurer
|
||||
* @see org.springframework.web.servlet.config.annotation.TilesWebMvcConfigurer
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Import({DelegatingWebMvcConfiguration.class, ViewResolutionImportSelector.class})
|
||||
@Import({DelegatingWebMvcConfiguration.class, ViewConfigurationImportSelector.class})
|
||||
public @interface EnableWebMvc {
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver} and a
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} beans.
|
||||
* Default configuration is "" prefix, ".ftl" suffix and "/WEB-INF/" templateLoaderPath.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class FreeMarkerRegistration extends ViewResolutionRegistration<FreeMarkerViewResolver> {
|
||||
|
||||
|
||||
public FreeMarkerRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new FreeMarkerViewResolver());
|
||||
this.prefix("");
|
||||
this.suffix(".ftl");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setPrefix(String)
|
||||
*/
|
||||
public FreeMarkerRegistration prefix(String prefix) {
|
||||
this.viewResolver.setPrefix(prefix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that gets appended to view names when building a URL.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setSuffix(String)
|
||||
*/
|
||||
public FreeMarkerRegistration suffix(String suffix) {
|
||||
this.viewResolver.setSuffix(suffix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable caching.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setCache(boolean)
|
||||
*/
|
||||
public FreeMarkerRegistration cache(boolean cache) {
|
||||
this.viewResolver.setCache(cache);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create an {@link InternalResourceViewResolver} bean.
|
||||
* Default configuration is "/WEB-INF/" prefix and ".jsp" suffix.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class JspRegistration extends ViewResolutionRegistration<InternalResourceViewResolver> {
|
||||
|
||||
public JspRegistration(ViewResolutionRegistry registry) {
|
||||
this(registry, "/WEB-INF/", ".jsp");
|
||||
}
|
||||
|
||||
public JspRegistration(ViewResolutionRegistry registry, String prefix, String suffix) {
|
||||
super(registry, new InternalResourceViewResolver());
|
||||
this.viewResolver.setPrefix(prefix);
|
||||
this.viewResolver.setSuffix(suffix);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesViewResolver} and a
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} beans.
|
||||
*
|
||||
* Default definition is "/WEB-INF/tiles.xml" and no Tiles definition check refresh.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class TilesRegistration extends ViewResolutionRegistration<TilesViewResolver> {
|
||||
|
||||
|
||||
public TilesRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new TilesViewResolver());
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
*
|
||||
* @see TilesViewResolver#setPrefix(String)
|
||||
*/
|
||||
public TilesRegistration prefix(String prefix) {
|
||||
this.viewResolver.setPrefix(prefix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that gets appended to view names when building a URL.
|
||||
*
|
||||
* @see TilesViewResolver#setSuffix(String)
|
||||
*/
|
||||
public TilesRegistration suffix(String suffix) {
|
||||
this.viewResolver.setSuffix(suffix);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Assist with configuring a {@link org.springframework.web.servlet.view.UrlBasedViewResolver}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class UrlBasedViewResolverRegistration {
|
||||
|
||||
protected final UrlBasedViewResolver viewResolver;
|
||||
|
||||
|
||||
public UrlBasedViewResolverRegistration(UrlBasedViewResolver viewResolver) {
|
||||
this.viewResolver = viewResolver;
|
||||
}
|
||||
|
||||
|
||||
protected UrlBasedViewResolver getViewResolver() {
|
||||
return this.viewResolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setPrefix
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration prefix(String prefix) {
|
||||
this.viewResolver.setPrefix(prefix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that gets appended to view names when building a URL.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setSuffix
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration suffix(String suffix) {
|
||||
this.viewResolver.setSuffix(suffix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the view class that should be used to create views.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewClass
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration viewClass(Class<?> viewClass) {
|
||||
this.viewResolver.setViewClass(viewClass);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the view names (or name patterns) that can be handled by this view
|
||||
* resolver. View names can contain simple wildcards such that 'my*', '*Report'
|
||||
* and '*Repo*' will all match the view name 'myReport'.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewNames
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration viewNames(String... viewNames) {
|
||||
this.viewResolver.setViewNames(viewNames);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set static attributes to be added to the model of every request for all
|
||||
* views resolved by this view resolver. This allows for setting any kind of
|
||||
* attribute values, for example bean references.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setAttributesMap
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration attributes(Map<String, ?> attributes) {
|
||||
this.viewResolver.setAttributesMap(attributes);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify the maximum number of entries for the view cache.
|
||||
* Default is 1024.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean)
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration cacheLimit(int cacheLimit) {
|
||||
this.viewResolver.setCacheLimit(cacheLimit);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable caching.
|
||||
* <p>This is equivalent to setting the {@link #cacheLimit "cacheLimit"}
|
||||
* property to the default limit (1024) or to 0, respectively.
|
||||
* <p>Default is "true": caching is enabled.
|
||||
* Disable this only for debugging and development.
|
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean)
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration cache(boolean cache) {
|
||||
this.viewResolver.setCache(cache);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityViewResolver} and a
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer beans}.
|
||||
* Default configuration is "" prefix, ".vm" suffix and "/WEB-INF/" resourceLoaderPath.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class VelocityRegistration extends ViewResolutionRegistration<VelocityViewResolver> {
|
||||
|
||||
|
||||
public VelocityRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new VelocityViewResolver());
|
||||
this.prefix("");
|
||||
this.suffix(".vm");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the prefix that gets prepended to view names when building a URL.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setPrefix(String)
|
||||
*/
|
||||
public VelocityRegistration prefix(String prefix) {
|
||||
this.viewResolver.setPrefix(prefix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the suffix that gets appended to view names when building a URL.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setSuffix(String)
|
||||
*/
|
||||
public VelocityRegistration suffix(String suffix) {
|
||||
this.viewResolver.setSuffix(suffix);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable or disable caching.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setCache(boolean)
|
||||
*/
|
||||
public VelocityRegistration cache(boolean cache) {
|
||||
this.viewResolver.setCache(cache);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a view resolver.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ViewResolutionRegistration<T extends ViewResolver> {
|
||||
|
||||
protected final ViewResolutionRegistry registry;
|
||||
|
||||
protected final T viewResolver;
|
||||
|
||||
public ViewResolutionRegistration(ViewResolutionRegistry registry, T viewResolver) {
|
||||
this.registry = registry;
|
||||
this.viewResolver = viewResolver;
|
||||
}
|
||||
|
||||
public ViewResolutionRegistry and() {
|
||||
return this.registry;
|
||||
}
|
||||
|
||||
protected T getViewResolver() {
|
||||
return this.viewResolver;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -1,172 +0,0 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Helps with configuring a list of view resolvers.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ViewResolutionRegistry {
|
||||
|
||||
private final List<ViewResolutionRegistration<?>> registrations = new ArrayList<ViewResolutionRegistration<?>>();
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
|
||||
public ViewResolutionRegistry(ApplicationContext context) {
|
||||
this.applicationContext = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register a custom {@link ViewResolver} bean.
|
||||
*/
|
||||
public ViewResolutionRegistration<ViewResolver> addViewResolver(ViewResolver viewResolver) {
|
||||
ViewResolutionRegistration<ViewResolver> registration = new ViewResolutionRegistration<ViewResolver>(this, viewResolver);
|
||||
registrations.add(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an {@link org.springframework.web.servlet.view.InternalResourceViewResolver}
|
||||
* bean with default "/WEB-INF/" prefix and ".jsp" suffix.
|
||||
*/
|
||||
public JspRegistration jsp() {
|
||||
JspRegistration registration = new JspRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register an {@link org.springframework.web.servlet.view.InternalResourceViewResolver}
|
||||
* bean with specified prefix and suffix.
|
||||
*/
|
||||
public JspRegistration jsp(String prefix, String suffix) {
|
||||
JspRegistration registration = new JspRegistration(this, prefix, suffix);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.BeanNameViewResolver} bean.
|
||||
*/
|
||||
public BeanNameRegistration beanName() {
|
||||
BeanNameRegistration registration = new BeanNameRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.tiles3.TilesViewResolver} and
|
||||
* a {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} with
|
||||
* default "/WEB-INF/tiles.xml" definition and no Tiles definition check refresh.
|
||||
*/
|
||||
public TilesRegistration tiles() {
|
||||
if (!hasBeanOfType(TilesConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Tiles view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " +
|
||||
"or declare your own TilesConfigurer bean.");
|
||||
}
|
||||
TilesRegistration registration = new TilesRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver}
|
||||
* and a {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} beans with
|
||||
* default "" prefix, ".vm" suffix and "/WEB-INF/" resourceLoaderPath.
|
||||
*/
|
||||
public VelocityRegistration velocity() {
|
||||
if (!hasBeanOfType(VelocityConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Velocity view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " +
|
||||
"or declare your own VelocityConfigurer bean.");
|
||||
}
|
||||
VelocityRegistration registration = new VelocityRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver}
|
||||
* and a {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} beans with
|
||||
* "" prefix, ".ftl" suffix and "/WEB-INF/" templateLoaderPath.
|
||||
*/
|
||||
public FreeMarkerRegistration freemarker() {
|
||||
if (!hasBeanOfType(FreeMarkerConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure FreeMarker view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " +
|
||||
"or declare your own FreeMarkerConfigurer bean.");
|
||||
}
|
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
protected boolean hasBeanOfType(Class<?> beanType) {
|
||||
return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
this.applicationContext, beanType, false, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.ContentNegotiatingViewResolver} bean.
|
||||
*/
|
||||
public ContentNegotiatingRegistration contentNegotiating(View... defaultViews) {
|
||||
ContentNegotiatingRegistration registration = new ContentNegotiatingRegistration(this);
|
||||
registration.defaultViews(defaultViews);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
protected List<ViewResolver> getViewResolvers() {
|
||||
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
|
||||
for(ViewResolutionRegistration<?> registration : this.registrations) {
|
||||
viewResolvers.add(registration.getViewResolver());
|
||||
}
|
||||
return viewResolvers;
|
||||
}
|
||||
|
||||
private void addAndCheckViewResolution(ViewResolutionRegistration<?> registration) {
|
||||
for(ViewResolutionRegistration<?> existingRegistration : this.registrations) {
|
||||
if(existingRegistration.getClass().equals(registration.getClass())) {
|
||||
throw new IllegalStateException("An instance of " + registration.getClass().getSimpleName()
|
||||
+ " is already registered, and multiple view resolvers and configurers beans are not supported by this registry");
|
||||
}
|
||||
}
|
||||
registrations.add(registration);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,290 @@
|
||||
/*
|
||||
* Copyright 2002-2014 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.view.BeanNameViewResolver;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Assist with the configuration of a chain of
|
||||
* {@link org.springframework.web.servlet.ViewResolver ViewResolver} instances.
|
||||
* This class is expected to be used via {@link WebMvcConfigurer#configureViewResolvers}.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ViewResolverRegistry {
|
||||
|
||||
private ContentNegotiatingViewResolver contentNegotiatingResolver;
|
||||
|
||||
private final List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(4);
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
private ContentNegotiationManager contentNegotiationManager;
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
|
||||
protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) {
|
||||
this.contentNegotiationManager = contentNegotiationManager;
|
||||
}
|
||||
|
||||
protected void setApplicationContext(ApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether any view resolvers have been registered.
|
||||
*/
|
||||
public boolean hasRegistrations() {
|
||||
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
|
||||
* media types requested by the client (e.g. in the Accept header).
|
||||
*
|
||||
* <p>If invoked multiple times the provided default views will be added to
|
||||
* any other default views that may have been configured already.
|
||||
*
|
||||
* @see ContentNegotiatingViewResolver#setDefaultViews
|
||||
*/
|
||||
public void enableContentNegotiation(View... defaultViews) {
|
||||
initContentNegotiatingViewResolver(defaultViews);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other
|
||||
* configured view resolvers and select among all selected Views based on
|
||||
* media types requested by the client (e.g. in the Accept header).
|
||||
*
|
||||
* <p>If invoked multiple times the provided default views will be added to
|
||||
* any other default views that may have been configured already.
|
||||
*
|
||||
* @see ContentNegotiatingViewResolver#setDefaultViews
|
||||
*/
|
||||
public void enableContentNegotiation(boolean useNotAcceptableStatus, View... defaultViews) {
|
||||
initContentNegotiatingViewResolver(defaultViews);
|
||||
this.contentNegotiatingResolver.setUseNotAcceptableStatusCode(useNotAcceptableStatus);
|
||||
}
|
||||
|
||||
private void initContentNegotiatingViewResolver(View[] defaultViews) {
|
||||
|
||||
// ContentNegotiatingResolver in the registry: elevate its precedence!
|
||||
this.order = Ordered.HIGHEST_PRECEDENCE;
|
||||
|
||||
if (this.contentNegotiatingResolver != null) {
|
||||
if (!ObjectUtils.isEmpty(defaultViews)) {
|
||||
if (!CollectionUtils.isEmpty(this.contentNegotiatingResolver.getDefaultViews())) {
|
||||
List<View> views = new ArrayList<View>(this.contentNegotiatingResolver.getDefaultViews());
|
||||
views.addAll(Arrays.asList(defaultViews));
|
||||
this.contentNegotiatingResolver.setDefaultViews(views);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
this.contentNegotiatingResolver = new ContentNegotiatingViewResolver();
|
||||
this.contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews));
|
||||
this.contentNegotiatingResolver.setViewResolvers(this.viewResolvers);
|
||||
this.contentNegotiatingResolver.setContentNegotiationManager(this.contentNegotiationManager);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable view resolution by forwarding to JSP pages with a default view name
|
||||
* prefix of "/WEB-INF/" and a default suffix of ".jsp".
|
||||
*
|
||||
* <p>This method may be invoked multiple and each call will register a
|
||||
* separate ViewResolver instance. Note that since it's not easy to determine
|
||||
* if a JSP exists without forwarding to it, using multiple JSP-based view
|
||||
* resolvers only makes sense in combination with the "viewNames" property
|
||||
* that indicates which view names are handled by which resolver.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration jsp() {
|
||||
return jsp("/WEB-INF/", ".jsp");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable view resolution by forwarding to JSP pages with the specified
|
||||
* prefix and suffix.
|
||||
*
|
||||
* <p>This method may be invoked multiple and each call will register a
|
||||
* separate ViewResolver instance. Note that since it's not easy to determine
|
||||
* if a JSP exists without forwarding to it, using multiple JSP-based view
|
||||
* resolvers only makes sense in combination with the "viewNames" property
|
||||
* that indicates which view names are handled by which resolver.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) {
|
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
resolver.setPrefix(prefix);
|
||||
resolver.setSuffix(suffix);
|
||||
this.viewResolvers.add(resolver);
|
||||
return new UrlBasedViewResolverRegistration(resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable Tiles-based view resolution.
|
||||
*
|
||||
* <p>By default tiles definitions are expected to be in "/WEB-INF/tiles.xml".
|
||||
* To change that and other Tiles-related options please also implement the
|
||||
* interface {@link TilesWebMvcConfigurer}.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration tiles() {
|
||||
if (this.applicationContext != null && !hasBeanOfType(TilesConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Tiles view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " +
|
||||
"or declare your own TilesConfigurer bean.");
|
||||
}
|
||||
TilesRegistration registration = new TilesRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable FreeMarker-based view resolution with an empty default view name
|
||||
* prefix and a default suffix of ".ftl".
|
||||
*
|
||||
* <p>By default the FreeMarker template loader path is set to "/WEB-INF/".
|
||||
* To change that and other FreeMarker-related options please also implement
|
||||
* the interface {@link FreeMarkerWebMvcConfigurer}.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration freeMarker() {
|
||||
if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure FreeMarker view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " +
|
||||
"or declare your own FreeMarkerConfigurer bean.");
|
||||
}
|
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable Velocity-based view resolution with an empty default view name
|
||||
* prefix, a default suffix of ".vm".
|
||||
*
|
||||
* <p>By default the Velocity resource loader path is set to "/WEB-INF/".
|
||||
* To change that and other Velocity-related options please also implement
|
||||
* the interface {@link VelocityWebMvcConfigurer}.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration velocity() {
|
||||
if (this.applicationContext != null && !hasBeanOfType(VelocityConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Velocity view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " +
|
||||
"or declare your own VelocityConfigurer bean.");
|
||||
}
|
||||
VelocityRegistration registration = new VelocityRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
return registration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the ability to map view names returned from controllers to
|
||||
* {@link org.springframework.web.servlet.View} beans.
|
||||
*/
|
||||
public void beanName() {
|
||||
BeanNameViewResolver resolver = new BeanNameViewResolver();
|
||||
this.viewResolvers.add(resolver);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link ViewResolver} bean instance. This may be useful to
|
||||
* configure a custom (or 3rd party) resolver implementation. It may also be
|
||||
* used as an alternative to other registration methods in this class when
|
||||
* they don't expose some more advanced property that needs to be set.
|
||||
*/
|
||||
public void viewResolver(ViewResolver viewResolver) {
|
||||
if (viewResolver instanceof ContentNegotiatingViewResolver) {
|
||||
throw new BeanInitializationException(
|
||||
"addViewResolver cannot be used to configure a ContentNegotiatingViewResolver. " +
|
||||
"Please use the method enableContentNegotiation instead.");
|
||||
}
|
||||
this.viewResolvers.add(viewResolver);
|
||||
}
|
||||
|
||||
protected boolean hasBeanOfType(Class<?> beanType) {
|
||||
return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
this.applicationContext, beanType, false, false));
|
||||
}
|
||||
|
||||
|
||||
protected int getOrder() {
|
||||
return this.order;
|
||||
}
|
||||
|
||||
protected List<ViewResolver> getViewResolvers() {
|
||||
if (this.contentNegotiatingResolver != null) {
|
||||
return Collections.<ViewResolver>singletonList(this.contentNegotiatingResolver);
|
||||
}
|
||||
else {
|
||||
return this.viewResolvers;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private static class TilesRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
private TilesRegistration() {
|
||||
super(new TilesViewResolver());
|
||||
}
|
||||
}
|
||||
|
||||
private static class VelocityRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
private VelocityRegistration() {
|
||||
super(new VelocityViewResolver());
|
||||
getViewResolver().setSuffix(".vm");
|
||||
}
|
||||
}
|
||||
|
||||
private static class FreeMarkerRegistration extends UrlBasedViewResolverRegistration {
|
||||
|
||||
private FreeMarkerRegistration() {
|
||||
super(new FreeMarkerViewResolver());
|
||||
getViewResolver().setSuffix(".ftl");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -81,7 +81,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProvider;
|
||||
import org.springframework.web.servlet.resource.ResourceUrlProviderExposingInterceptor;
|
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver;
|
||||
import org.springframework.web.servlet.view.ViewResolverComposite;
|
||||
import org.springframework.web.util.UrlPathHelper;
|
||||
|
||||
/**
|
||||
@@ -197,7 +197,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
|
||||
private PathMatchConfigurer pathMatchConfigurer;
|
||||
|
||||
private ViewResolutionRegistry viewResolutionRegistry;
|
||||
|
||||
/**
|
||||
* Set the {@link javax.servlet.ServletContext}, e.g. for resource handling,
|
||||
@@ -354,13 +353,6 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
protected void addViewControllers(ViewControllerRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Override this method to configure view resolution.
|
||||
* @see ViewResolutionRegistry
|
||||
*/
|
||||
protected void configureViewResolution(ViewResolutionRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a {@link BeanNameUrlHandlerMapping} ordered at 2 to map URL
|
||||
* paths to controller bean names.
|
||||
@@ -791,45 +783,33 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link ViewResolverComposite} that contains an ordered list of
|
||||
* Register a {@link org.springframework.web.servlet.view.ViewResolverComposite} that contains an ordered list of
|
||||
* view resolvers obtained either through
|
||||
* {@link #configureViewResolution(ViewResolutionRegistry)}.
|
||||
* {@link #configureViewResolvers(ViewResolverRegistry)}.
|
||||
*/
|
||||
@Bean
|
||||
public ViewResolverComposite viewResolverComposite() {
|
||||
ViewResolutionRegistry registry = getViewResolutionRegistry();
|
||||
ViewResolverComposite compositeViewResolver = new ViewResolverComposite();
|
||||
List<ViewResolver> viewResolvers = registry.getViewResolvers();
|
||||
ContentNegotiatingViewResolver contentNegotiatingViewResolver = null;
|
||||
List<ViewResolver> filteredViewResolvers = new ArrayList<ViewResolver>();
|
||||
for(ViewResolver viewResolver : viewResolvers) {
|
||||
if(viewResolver instanceof ContentNegotiatingViewResolver) {
|
||||
contentNegotiatingViewResolver = (ContentNegotiatingViewResolver)viewResolver;
|
||||
contentNegotiatingViewResolver.setContentNegotiationManager(this.mvcContentNegotiationManager());
|
||||
} else {
|
||||
filteredViewResolvers.add(viewResolver);
|
||||
}
|
||||
}
|
||||
if(contentNegotiatingViewResolver != null) {
|
||||
contentNegotiatingViewResolver.setViewResolvers(filteredViewResolvers);
|
||||
viewResolvers = new ArrayList<ViewResolver>();
|
||||
viewResolvers.add(contentNegotiatingViewResolver);
|
||||
}
|
||||
compositeViewResolver.setViewResolvers(viewResolvers);
|
||||
compositeViewResolver.setApplicationContext(this.applicationContext);
|
||||
compositeViewResolver.setServletContext(this.servletContext);
|
||||
public ViewResolver mvcViewResolver() {
|
||||
ViewResolverRegistry registry = new ViewResolverRegistry();
|
||||
registry.setContentNegotiationManager(mvcContentNegotiationManager());
|
||||
registry.setApplicationContext(this.applicationContext);
|
||||
configureViewResolvers(registry);
|
||||
|
||||
return compositeViewResolver;
|
||||
ViewResolverComposite composite = new ViewResolverComposite();
|
||||
composite.setOrder(registry.getOrder());
|
||||
composite.setViewResolvers(registry.getViewResolvers());
|
||||
composite.setApplicationContext(this.applicationContext);
|
||||
composite.setServletContext(this.servletContext);
|
||||
return composite;
|
||||
}
|
||||
|
||||
protected ViewResolutionRegistry getViewResolutionRegistry() {
|
||||
if(this.viewResolutionRegistry == null) {
|
||||
this.viewResolutionRegistry = new ViewResolutionRegistry(this.applicationContext);
|
||||
configureViewResolution(this.viewResolutionRegistry);
|
||||
}
|
||||
return this.viewResolutionRegistry;
|
||||
/**
|
||||
* Override this method to configure view resolution.
|
||||
* @see ViewResolverRegistry
|
||||
*/
|
||||
protected void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
}
|
||||
|
||||
|
||||
private final static class EmptyHandlerMapping extends AbstractHandlerMapping {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -139,9 +139,11 @@ public interface WebMvcConfigurer {
|
||||
void addViewControllers(ViewControllerRegistry registry);
|
||||
|
||||
/**
|
||||
* Configure view resolution to translate view names to view implementations.
|
||||
* Configure view resolvers to translate String-based view names returned from
|
||||
* controllers into concrete {@link org.springframework.web.servlet.View}
|
||||
* implementations to perform rendering with.
|
||||
*/
|
||||
void configureViewResolution(ViewResolutionRegistry registry);
|
||||
void configureViewResolvers(ViewResolverRegistry registry);
|
||||
|
||||
/**
|
||||
* Add handlers to serve static resources such as images, js, and, css
|
||||
|
||||
@@ -138,7 +138,7 @@ public abstract class WebMvcConfigurerAdapter implements WebMvcConfigurer {
|
||||
* <p>This implementation is empty.
|
||||
*/
|
||||
@Override
|
||||
public void configureViewResolution(ViewResolutionRegistry registry) {
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -114,9 +114,9 @@ class WebMvcConfigurerComposite implements WebMvcConfigurer {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void configureViewResolution(ViewResolutionRegistry registry) {
|
||||
public void configureViewResolvers(ViewResolverRegistry registry) {
|
||||
for (WebMvcConfigurer delegate : this.delegates) {
|
||||
delegate.configureViewResolution(registry);
|
||||
delegate.configureViewResolvers(registry);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -56,7 +56,7 @@ public class WebMvcFreeMarkerConfiguration {
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public FreeMarkerConfigurer freeMarkerConfigurer() {
|
||||
public FreeMarkerConfigurer mvcFreeMarkerConfigurer() {
|
||||
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
|
||||
configurer.setTemplateLoaderPath("/WEB-INF/");
|
||||
for (FreeMarkerWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
|
||||
@@ -66,7 +66,7 @@ public class WebMvcTilesConfiguration implements ResourceLoaderAware {
|
||||
|
||||
|
||||
@Bean
|
||||
public TilesConfigurer tilesConfigurer() {
|
||||
public TilesConfigurer mvcTilesConfigurer() {
|
||||
TilesConfigurer configurer = new TilesConfigurer();
|
||||
if (!this.webMvcConfigurers.isEmpty()) {
|
||||
for (TilesWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
|
||||
@@ -56,7 +56,7 @@ public class WebMvcVelocityConfiguration {
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public VelocityConfigurer velocityConfigurer() {
|
||||
public VelocityConfigurer mvcVelocityConfigurer() {
|
||||
VelocityConfigurer configurer = new VelocityConfigurer();
|
||||
configurer.setResourceLoaderPath("/WEB-INF/");
|
||||
for (VelocityWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
|
||||
@@ -228,6 +228,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||
this.defaultViews = defaultViews;
|
||||
}
|
||||
|
||||
public List<View> getDefaultViews() {
|
||||
return Collections.unmodifiableList(this.defaultViews);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the view resolvers to be wrapped by this view resolver.
|
||||
* <p>If this property is not set, view resolvers will be detected automatically.
|
||||
@@ -236,6 +240,10 @@ public class ContentNegotiatingViewResolver extends WebApplicationObjectSupport
|
||||
this.viewResolvers = viewResolvers;
|
||||
}
|
||||
|
||||
public List<ViewResolver> getViewResolvers() {
|
||||
return Collections.unmodifiableList(this.viewResolvers);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void initServletContext(ServletContext servletContext) {
|
||||
|
||||
@@ -14,31 +14,56 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.web.servlet;
|
||||
package org.springframework.web.servlet.view;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.context.ServletContextAware;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
/**
|
||||
* A {@link ViewResolverComposite} that delegates to a list of other {@link ViewResolver}s.
|
||||
* A {@link org.springframework.web.servlet.ViewResolver} that delegates to others.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public class ViewResolverComposite implements ApplicationContextAware, ServletContextAware, ViewResolver, Ordered {
|
||||
public class ViewResolverComposite implements ViewResolver, Ordered, InitializingBean,
|
||||
ApplicationContextAware, ServletContextAware {
|
||||
|
||||
private List<ViewResolver> viewResolvers;
|
||||
private final List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
|
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE;
|
||||
|
||||
|
||||
/**
|
||||
* Set the list of view viewResolvers to delegate to.
|
||||
*/
|
||||
public void setViewResolvers(List<ViewResolver> viewResolvers) {
|
||||
this.viewResolvers.clear();
|
||||
if (!CollectionUtils.isEmpty(viewResolvers)) {
|
||||
this.viewResolvers.addAll(viewResolvers);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of view viewResolvers to delegate to.
|
||||
*/
|
||||
public List<ViewResolver> getViewResolvers() {
|
||||
return Collections.unmodifiableList(this.viewResolvers);
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
@@ -48,53 +73,42 @@ public class ViewResolverComposite implements ApplicationContextAware, ServletCo
|
||||
return this.order;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the list of view viewResolvers to delegate to.
|
||||
*/
|
||||
public void setViewResolvers(List<ViewResolver> viewResolvers) {
|
||||
this.viewResolvers = viewResolvers;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the list of view viewResolvers to delegate to.
|
||||
*/
|
||||
public List<ViewResolver> getViewResolvers() {
|
||||
return Collections.unmodifiableList(viewResolvers);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String viewName, Locale locale) throws Exception {
|
||||
if (viewResolvers != null) {
|
||||
for (ViewResolver viewResolver : viewResolvers) {
|
||||
View v = viewResolver.resolveViewName(viewName, locale);
|
||||
if (v != null) {
|
||||
return v;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
if (viewResolvers != null) {
|
||||
for (ViewResolver viewResolver : viewResolvers) {
|
||||
if(viewResolver instanceof ApplicationContextAware) {
|
||||
((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext);
|
||||
}
|
||||
for (ViewResolver viewResolver : this.viewResolvers) {
|
||||
if (viewResolver instanceof ApplicationContextAware) {
|
||||
((ApplicationContextAware)viewResolver).setApplicationContext(applicationContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setServletContext(ServletContext servletContext) {
|
||||
if (viewResolvers != null) {
|
||||
for (ViewResolver viewResolver : viewResolvers) {
|
||||
if(viewResolver instanceof ServletContextAware) {
|
||||
((ServletContextAware)viewResolver).setServletContext(servletContext);
|
||||
}
|
||||
for (ViewResolver viewResolver : this.viewResolvers) {
|
||||
if (viewResolver instanceof ServletContextAware) {
|
||||
((ServletContextAware)viewResolver).setServletContext(servletContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
for (ViewResolver viewResolver : this.viewResolvers) {
|
||||
if (viewResolver instanceof InitializingBean) {
|
||||
((InitializingBean) viewResolver).afterPropertiesSet();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public View resolveViewName(String viewName, Locale locale) throws Exception {
|
||||
for (ViewResolver viewResolver : this.viewResolvers) {
|
||||
View view = viewResolver.resolveViewName(viewName, locale);
|
||||
if (view != null) {
|
||||
return view;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user