Remove FreeMarker/Velocity/TilesConfigurer MVC config
After some further discussion: The MVC config simplifies ViewResolver configuration especially where content negotiation view resolution is involved. The configuration of the underlying view technology however is kept completely separate. In the case of the MVC namespace, dedicated top-level freemarker, velocity, and tiles namespace elements are provided. In the case of the MVC Java config, applications simply declare FreeMarkerConfigurer, VelocityConfigurer, or TilesConfigurer beans respectively. Issue: SPR-7093
This commit is contained in:
@@ -33,10 +33,6 @@ import org.springframework.context.annotation.Import;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <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.:
|
||||
@@ -61,10 +57,6 @@ import org.springframework.context.annotation.Import;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <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.:
|
||||
@@ -87,23 +79,15 @@ import org.springframework.context.annotation.Import;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <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, ViewConfigurationImportSelector.class})
|
||||
@Import({DelegatingWebMvcConfiguration.class})
|
||||
public @interface EnableWebMvc {
|
||||
}
|
||||
|
||||
@@ -1,35 +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.FreeMarkerConfigurer;
|
||||
|
||||
/**
|
||||
* Defines a callback method to customize the
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
|
||||
* FreeMarkerConfigurer} bean provided when using {@code @EnableWebMvc}.
|
||||
*
|
||||
* <p>An {@code @EnableWebMvc}-annotated configuration classes can implement
|
||||
* this interface to customize the {@code FreeMarkerConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public interface FreeMarkerWebMvcConfigurer {
|
||||
|
||||
void configureFreeMarker(FreeMarkerConfigurer configurer);
|
||||
|
||||
}
|
||||
@@ -1,35 +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.TilesConfigurer;
|
||||
|
||||
/**
|
||||
* Defines a callback method to customize the
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer
|
||||
* TilesConfigurer} bean provided when using {@code @EnableWebMvc}.
|
||||
*
|
||||
* <p>An {@code @EnableWebMvc}-annotated configuration classes can implement
|
||||
* this interface to customize the {@code TilesConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public interface TilesWebMvcConfigurer {
|
||||
|
||||
void configureTiles(TilesConfigurer configurer);
|
||||
|
||||
}
|
||||
@@ -1,35 +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.VelocityConfigurer;
|
||||
|
||||
/**
|
||||
* Defines a callback method to customize the
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer
|
||||
* VelocityConfigurer} bean provided when using {@code @EnableWebMvc}.
|
||||
*
|
||||
* <p>An {@code @EnableWebMvc}-annotated configuration classes can implement
|
||||
* this interface to customize the {@code VelocityConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
public interface VelocityWebMvcConfigurer {
|
||||
|
||||
void configureVelocity(VelocityConfigurer configurer);
|
||||
|
||||
}
|
||||
@@ -1,64 +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.context.annotation.DeferredImportSelector;
|
||||
import org.springframework.core.type.AnnotationMetadata;
|
||||
import org.springframework.util.ClassUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Selectively imports configuration required to configure Tiles, Freemarker, or
|
||||
* Velocity for view resolution depending on whether those 3rd party libraries
|
||||
* are available on the classpath.
|
||||
*
|
||||
* @author Sebastien Deleuze
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*
|
||||
* @see WebMvcFreeMarkerConfiguration
|
||||
*/
|
||||
public class ViewConfigurationImportSelector implements DeferredImportSelector {
|
||||
|
||||
private static final boolean tilesPresent = ClassUtils.isPresent(
|
||||
"org.apache.tiles.startup.TilesInitializer", ViewConfigurationImportSelector.class.getClassLoader());
|
||||
|
||||
private static final boolean velocityPresent = ClassUtils.isPresent(
|
||||
"org.apache.velocity.app.VelocityEngine", ViewConfigurationImportSelector.class.getClassLoader());
|
||||
|
||||
private static final boolean freeMarkerPresent = ClassUtils.isPresent(
|
||||
"freemarker.template.Configuration", ViewConfigurationImportSelector.class.getClassLoader());
|
||||
|
||||
|
||||
@Override
|
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
|
||||
List<String> classes = new ArrayList<String>(3);
|
||||
if (tilesPresent) {
|
||||
classes.add(WebMvcTilesConfiguration.class.getName());
|
||||
}
|
||||
if (velocityPresent) {
|
||||
classes.add(WebMvcVelocityConfiguration.class.getName());
|
||||
}
|
||||
if (freeMarkerPresent) {
|
||||
classes.add(WebMvcFreeMarkerConfiguration.class.getName());
|
||||
}
|
||||
return classes.toArray(new String[0]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -130,28 +130,27 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable view resolution by forwarding to JSP pages with a default view name
|
||||
* prefix of "/WEB-INF/" and a default suffix of ".jsp".
|
||||
* Register JSP view resolver using 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
|
||||
* <p>When this method is invoked more than once, each call will register a
|
||||
* new 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.
|
||||
* on the resolver indicating 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.
|
||||
* Register JSP view resolver 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
|
||||
* <p>When this method is invoked more than once, each call will register a
|
||||
* new 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.
|
||||
* on the resolver indicating which view names are handled by which resolver.
|
||||
*/
|
||||
public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) {
|
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
|
||||
@@ -162,18 +161,16 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable Tiles-based view resolution.
|
||||
* Register Tiles 3.x view resolver.
|
||||
*
|
||||
* <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}.
|
||||
* <p><strong>Note</strong> that you must also configure Tiles by adding a
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} bean.
|
||||
*/
|
||||
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.");
|
||||
throw new BeanInitializationException("In addition to a Tiles view resolver " +
|
||||
"there must also be a single TilesConfigurer bean in this web application context " +
|
||||
"(or its parent).");
|
||||
}
|
||||
TilesRegistration registration = new TilesRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
@@ -181,19 +178,18 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable FreeMarker-based view resolution with an empty default view name
|
||||
* Register a FreeMarker view resolver 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}.
|
||||
* <p><strong>Note</strong> that you must also configure FreeMarker by adding a
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} bean.
|
||||
*/
|
||||
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.");
|
||||
throw new BeanInitializationException("In addition to a FreeMarker view resolver " +
|
||||
"there must also be a single FreeMarkerConfig bean in this web application context " +
|
||||
"(or its parent): FreeMarkerConfigurer is the usual implementation. " +
|
||||
"This bean may be given any name.");
|
||||
}
|
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
@@ -201,19 +197,18 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable Velocity-based view resolution with an empty default view name
|
||||
* Register Velocity view resolver 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}.
|
||||
* <p><strong>Note</strong> that you must also configure Velocity by adding a
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} bean.
|
||||
*/
|
||||
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.");
|
||||
throw new BeanInitializationException("In addition to a Velocity view resolver " +
|
||||
"there must also be a single VelocityConfig bean in this web application context " +
|
||||
"(or its parent): VelocityConfigurer is the usual implementation. " +
|
||||
"This bean may be given any name.");
|
||||
}
|
||||
VelocityRegistration registration = new VelocityRegistration();
|
||||
this.viewResolvers.add(registration.getViewResolver());
|
||||
@@ -221,8 +216,8 @@ public class ViewResolverRegistry {
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable the ability to map view names returned from controllers to
|
||||
* {@link org.springframework.web.servlet.View} beans.
|
||||
* Register a bean name view resolver that interprets view names as the names
|
||||
* of {@link org.springframework.web.servlet.View} beans.
|
||||
*/
|
||||
public void beanName() {
|
||||
BeanNameViewResolver resolver = new BeanNameViewResolver();
|
||||
|
||||
@@ -1,76 +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.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Configuration class that declares a
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer
|
||||
* FreeMarkerConfigurer} bean. The configuration is conditional and applies
|
||||
* only if there is no {@code FreeMarkerConfigurer} bean already declared.
|
||||
*
|
||||
* <p>This configuration is imported when using {@link EnableWebMvc} if
|
||||
* FreeMarker is available on the classpath. It can be customized by
|
||||
* implementing {@link FreeMarkerWebMvcConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
@Configuration
|
||||
@Conditional(WebMvcFreeMarkerConfiguration.FreeMarkerConfigurerNotPresentCondition.class)
|
||||
public class WebMvcFreeMarkerConfiguration {
|
||||
|
||||
private final List<FreeMarkerWebMvcConfigurer> webMvcConfigurers = new ArrayList<FreeMarkerWebMvcConfigurer>(1);
|
||||
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setWebMvcConfigurers(List<FreeMarkerWebMvcConfigurer> webMvcConfigurers) {
|
||||
if (!CollectionUtils.isEmpty(webMvcConfigurers)) {
|
||||
this.webMvcConfigurers.addAll(webMvcConfigurers);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public FreeMarkerConfigurer mvcFreeMarkerConfigurer() {
|
||||
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
|
||||
configurer.setTemplateLoaderPath("/WEB-INF/");
|
||||
for (FreeMarkerWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
webMvcConfigurer.configureFreeMarker(configurer);
|
||||
}
|
||||
return configurer;
|
||||
}
|
||||
|
||||
|
||||
static class FreeMarkerConfigurerNotPresentCondition extends BeanTypeNotPresentCondition {
|
||||
|
||||
private FreeMarkerConfigurerNotPresentCondition() {
|
||||
super(FreeMarkerConfigurer.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +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.annotation.Autowired;
|
||||
import org.springframework.context.ResourceLoaderAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.core.io.ResourceLoader;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Configuration class that declares a
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer
|
||||
* TilesConfigurer} bean. The configuration is conditional and applies
|
||||
* only if there is no {@code TilesConfigurer} bean already declared.
|
||||
*
|
||||
* <p>This configuration is imported when using {@link EnableWebMvc} if Tiles 3
|
||||
* is available on the classpath. It can be customized by implementing
|
||||
* {@link TilesWebMvcConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
@Configuration
|
||||
@Conditional(WebMvcTilesConfiguration.TilesConfigurerNotPresentCondition.class)
|
||||
public class WebMvcTilesConfiguration implements ResourceLoaderAware {
|
||||
|
||||
private final List<TilesWebMvcConfigurer> webMvcConfigurers = new ArrayList<TilesWebMvcConfigurer>(1);
|
||||
|
||||
private ResourceLoader resourceLoader;
|
||||
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setWebMvcConfigurers(List<TilesWebMvcConfigurer> webMvcConfigurers) {
|
||||
if (!CollectionUtils.isEmpty(webMvcConfigurers)) {
|
||||
this.webMvcConfigurers.addAll(webMvcConfigurers);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setResourceLoader(ResourceLoader resourceLoader) {
|
||||
this.resourceLoader = resourceLoader;
|
||||
}
|
||||
|
||||
|
||||
@Bean
|
||||
public TilesConfigurer mvcTilesConfigurer() {
|
||||
TilesConfigurer configurer = new TilesConfigurer();
|
||||
if (!this.webMvcConfigurers.isEmpty()) {
|
||||
for (TilesWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
webMvcConfigurer.configureTiles(configurer);
|
||||
}
|
||||
}
|
||||
else {
|
||||
Resource resource = this.resourceLoader.getResource("/WEB-INF/tiles.xml");
|
||||
if (!resource.exists()) {
|
||||
String[] noTilesDefinitions = new String[0];
|
||||
configurer.setDefinitions(noTilesDefinitions);
|
||||
}
|
||||
}
|
||||
return configurer;
|
||||
}
|
||||
|
||||
|
||||
static class TilesConfigurerNotPresentCondition extends BeanTypeNotPresentCondition {
|
||||
|
||||
private TilesConfigurerNotPresentCondition() {
|
||||
super(TilesConfigurer.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,76 +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.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Configuration class that declares a
|
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer
|
||||
* VelocityConfigurer} bean. The configuration is conditional and applies
|
||||
* only if there is no {@code VelocityConfigurer} bean already declared.
|
||||
*
|
||||
* <p>This configuration is imported when using {@link EnableWebMvc} if
|
||||
* Velocity is available on the classpath. It can be customized by
|
||||
* implementing {@link VelocityWebMvcConfigurer}.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @since 4.1
|
||||
*/
|
||||
@Configuration
|
||||
@Conditional(WebMvcVelocityConfiguration.VelocityConfigurerNotPresentCondition.class)
|
||||
public class WebMvcVelocityConfiguration {
|
||||
|
||||
private final List<VelocityWebMvcConfigurer> webMvcConfigurers = new ArrayList<VelocityWebMvcConfigurer>(1);
|
||||
|
||||
|
||||
@Autowired(required = false)
|
||||
public void setWebMvcConfigurers(List<VelocityWebMvcConfigurer> webMvcConfigurers) {
|
||||
if (!CollectionUtils.isEmpty(webMvcConfigurers)) {
|
||||
this.webMvcConfigurers.addAll(webMvcConfigurers);
|
||||
}
|
||||
}
|
||||
|
||||
@Bean
|
||||
@Lazy
|
||||
public VelocityConfigurer mvcVelocityConfigurer() {
|
||||
VelocityConfigurer configurer = new VelocityConfigurer();
|
||||
configurer.setResourceLoaderPath("/WEB-INF/");
|
||||
for (VelocityWebMvcConfigurer webMvcConfigurer : this.webMvcConfigurers) {
|
||||
webMvcConfigurer.configureVelocity(configurer);
|
||||
}
|
||||
return configurer;
|
||||
}
|
||||
|
||||
|
||||
static class VelocityConfigurerNotPresentCondition extends BeanTypeNotPresentCondition {
|
||||
|
||||
private VelocityConfigurerNotPresentCondition() {
|
||||
super(VelocityConfigurer.class);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user