From 10a4c2cd8140095777343262ba728e51a5f6c873 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 15 Jul 2014 14:27:01 -0400 Subject: [PATCH] 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 --- .../samples/context/JavaConfigTests.java | 10 +- .../config/annotation/EnableWebMvc.java | 18 +--- .../FreeMarkerWebMvcConfigurer.java | 35 ------- .../annotation/TilesWebMvcConfigurer.java | 35 ------- .../annotation/VelocityWebMvcConfigurer.java | 35 ------- .../ViewConfigurationImportSelector.java | 64 ------------- .../annotation/ViewResolverRegistry.java | 67 ++++++------- .../WebMvcFreeMarkerConfiguration.java | 76 --------------- .../annotation/WebMvcTilesConfiguration.java | 94 ------------------- .../WebMvcVelocityConfiguration.java | 76 --------------- .../ViewResolutionIntegrationTests.java | 84 +++++------------ 11 files changed, 63 insertions(+), 531 deletions(-) delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/FreeMarkerWebMvcConfigurer.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/TilesWebMvcConfigurer.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/VelocityWebMvcConfigurer.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewConfigurationImportSelector.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcFreeMarkerConfiguration.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcTilesConfiguration.java delete mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcVelocityConfiguration.java diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java index fe246232b7..c2eb2391ac 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/context/JavaConfigTests.java @@ -51,7 +51,7 @@ import static org.mockito.Mockito.*; * @author Sebastien Deleuze */ @RunWith(SpringJUnit4ClassRunner.class) -@WebAppConfiguration("src/test/resources/META-INF/web-resources") +@WebAppConfiguration("classpath:META-INF/web-resources") @ContextHierarchy({ @ContextConfiguration(classes = RootConfig.class), @ContextConfiguration(classes = WebConfig.class) @@ -100,7 +100,7 @@ public class JavaConfigTests { @Configuration @EnableWebMvc - static class WebConfig extends WebMvcConfigurerAdapter implements TilesWebMvcConfigurer { + static class WebConfig extends WebMvcConfigurerAdapter { @Autowired private RootConfig rootConfig; @@ -130,9 +130,11 @@ public class JavaConfigTests { registry.tiles(); } - @Override - public void configureTiles(TilesConfigurer configurer) { + @Bean + public TilesConfigurer tilesConfigurer() { + TilesConfigurer configurer = new TilesConfigurer(); configurer.setDefinitions("/WEB-INF/**/tiles.xml"); + return configurer; } } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java index ec699fc621..c3b39b7b5e 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/EnableWebMvc.java @@ -33,10 +33,6 @@ import org.springframework.context.annotation.Import; * } * * - *

As of 4.1 this annotation may also import {@link WebMvcFreeMarkerConfiguration}, - * {@link WebMvcVelocityConfiguration}, or {@link WebMvcTilesConfiguration} if - * those libraries are found on the classpath. - * *

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; * } * * - *

To customize the FreeMarker, Velocity, or Tiles configuration, additionally - * implement {@link FreeMarkerWebMvcConfigurer}, {@link VelocityWebMvcConfigurer}, - * and/or {@link TilesWebMvcConfigurer}. - * *

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; * } * * - *

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 { } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/FreeMarkerWebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/FreeMarkerWebMvcConfigurer.java deleted file mode 100644 index 5ac4b893bc..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/FreeMarkerWebMvcConfigurer.java +++ /dev/null @@ -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}. - * - *

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); - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/TilesWebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/TilesWebMvcConfigurer.java deleted file mode 100644 index f2f9d62ce8..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/TilesWebMvcConfigurer.java +++ /dev/null @@ -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}. - * - *

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); - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/VelocityWebMvcConfigurer.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/VelocityWebMvcConfigurer.java deleted file mode 100644 index 0887e0b966..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/VelocityWebMvcConfigurer.java +++ /dev/null @@ -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}. - * - *

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); - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewConfigurationImportSelector.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewConfigurationImportSelector.java deleted file mode 100644 index 379e92eb30..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewConfigurationImportSelector.java +++ /dev/null @@ -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 classes = new ArrayList(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]); - } - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java index 2e0957933d..389c60c883 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewResolverRegistry.java @@ -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". * - *

This method may be invoked multiple and each call will register a - * separate ViewResolver instance. Note that since it's not easy to determine + *

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. * - *

This method may be invoked multiple and each call will register a - * separate ViewResolver instance. Note that since it's not easy to determine + *

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. * - *

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}. + *

Note 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". * - *

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}. + *

Note 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". * - *

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}. + *

Note 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(); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcFreeMarkerConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcFreeMarkerConfiguration.java deleted file mode 100644 index da1af95545..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcFreeMarkerConfiguration.java +++ /dev/null @@ -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. - * - *

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 webMvcConfigurers = new ArrayList(1); - - - @Autowired(required = false) - public void setWebMvcConfigurers(List 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); - } - } - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcTilesConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcTilesConfiguration.java deleted file mode 100644 index dcc9ed9014..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcTilesConfiguration.java +++ /dev/null @@ -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. - * - *

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 webMvcConfigurers = new ArrayList(1); - - private ResourceLoader resourceLoader; - - - @Autowired(required = false) - public void setWebMvcConfigurers(List 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); - } - } - -} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcVelocityConfiguration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcVelocityConfiguration.java deleted file mode 100644 index 4641850088..0000000000 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/WebMvcVelocityConfiguration.java +++ /dev/null @@ -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. - * - *

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 webMvcConfigurers = new ArrayList(1); - - - @Autowired(required = false) - public void setWebMvcConfigurers(List 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); - } - } - -} diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java index eb50ad4f78..0e89c2f55f 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ViewResolutionIntegrationTests.java @@ -53,24 +53,6 @@ public class ViewResolutionIntegrationTests { public final ExpectedException thrown = ExpectedException.none(); - @Test - public void minimalFreemarkerConfig() throws Exception { - MockHttpServletResponse response = runTest(MinimalFreeMarkerWebConfig.class); - assertEquals("Hello World!", response.getContentAsString()); - } - - @Test - public void minimalVelocityConfig() throws Exception { - MockHttpServletResponse response = runTest(MinimalVelocityWebConfig.class); - assertEquals("Hello World!", response.getContentAsString()); - } - - @Test - public void minimalTilesConfig() throws Exception { - MockHttpServletResponse response = runTest(MinimalTilesWebConfig.class); - assertEquals("/WEB-INF/index.jsp", response.getForwardedUrl()); - } - @Test public void freemarker() throws Exception { MockHttpServletResponse response = runTest(FreeMarkerWebConfig.class); @@ -91,19 +73,19 @@ public class ViewResolutionIntegrationTests { @Test public void freemarkerInvalidConfig() throws Exception { - this.thrown.expectMessage("It looks like you're trying to configure FreeMarker view resolution."); + this.thrown.expectMessage("In addition to a FreeMarker view resolver "); runTest(InvalidFreeMarkerWebConfig.class); } @Test public void velocityInvalidConfig() throws Exception { - this.thrown.expectMessage("It looks like you're trying to configure Velocity view resolution."); + this.thrown.expectMessage("In addition to a Velocity view resolver "); runTest(InvalidVelocityWebConfig.class); } @Test public void tilesInvalidConfig() throws Exception { - this.thrown.expectMessage("It looks like you're trying to configure Tiles view resolution."); + this.thrown.expectMessage("In addition to a Tiles view resolver "); runTest(InvalidTilesWebConfig.class); } @@ -138,6 +120,7 @@ public class ViewResolutionIntegrationTests { @EnableWebMvc static abstract class AbstractWebConfig extends WebMvcConfigurerAdapter { + @Bean public SampleController sampleController() { return new SampleController(); @@ -145,69 +128,56 @@ public class ViewResolutionIntegrationTests { } @Configuration - static class MinimalFreeMarkerWebConfig extends AbstractWebConfig { + static class FreeMarkerWebConfig extends AbstractWebConfig { + @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.freeMarker(); } - } - @Configuration - static class MinimalVelocityWebConfig extends AbstractWebConfig { - @Override - public void configureViewResolvers(ViewResolverRegistry registry) { - registry.velocity(); - } - } - - @Configuration - static class MinimalTilesWebConfig extends AbstractWebConfig { - @Override - public void configureViewResolvers(ViewResolverRegistry registry) { - registry.tiles(); - } - } - - @Configuration - static class FreeMarkerWebConfig extends AbstractWebConfig implements FreeMarkerWebMvcConfigurer { - @Override - public void configureViewResolvers(ViewResolverRegistry registry) { - registry.freeMarker(); - } - @Override - public void configureFreeMarker(FreeMarkerConfigurer configurer) { + @Bean + public FreeMarkerConfigurer freeMarkerConfigurer() { + FreeMarkerConfigurer configurer = new FreeMarkerConfigurer(); configurer.setTemplateLoaderPath("/WEB-INF/"); + return configurer; } } @Configuration - static class VelocityWebConfig extends AbstractWebConfig implements VelocityWebMvcConfigurer { + static class VelocityWebConfig extends AbstractWebConfig { + @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.velocity(); } - @Override - public void configureVelocity(VelocityConfigurer configurer) { + + @Bean + public VelocityConfigurer velocityConfigurer() { + VelocityConfigurer configurer = new VelocityConfigurer(); configurer.setResourceLoaderPath("/WEB-INF/"); + return configurer; } } @Configuration - static class TilesWebConfig extends AbstractWebConfig implements TilesWebMvcConfigurer { + static class TilesWebConfig extends AbstractWebConfig { + @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.tiles(); } - @Override - public void configureTiles(TilesConfigurer configurer) { + + @Bean + public TilesConfigurer tilesConfigurer() { + TilesConfigurer configurer = new TilesConfigurer(); configurer.setDefinitions("/WEB-INF/tiles.xml"); + return configurer; } } + @Configuration static class InvalidFreeMarkerWebConfig extends WebMvcConfigurationSupport { - // No @EnableWebMvc and no FreeMarkerConfigurer bean - @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.freeMarker(); @@ -217,8 +187,6 @@ public class ViewResolutionIntegrationTests { @Configuration static class InvalidVelocityWebConfig extends WebMvcConfigurationSupport { - // No @EnableWebMvc and no VelocityConfigurer bean - @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.velocity(); @@ -228,8 +196,6 @@ public class ViewResolutionIntegrationTests { @Configuration static class InvalidTilesWebConfig extends WebMvcConfigurationSupport { - // No @EnableWebMvc and no TilesConfigurer bean - @Override public void configureViewResolvers(ViewResolverRegistry registry) { registry.tiles();