Browse Source
This change improves the support for auto-registration of FreeMarker, Velocity, and Tiles configuration. The configuration is now conditional not only based on the classpath but also based on whether a FreeMarkerConfigurer for example is already present in the configuration. This change also introduces FreeMarker~, Velocity~, and TilesWebMvcConfigurer interfaces for customizing each view technology. The WebMvcConfigurer can still be used to configure all view resolvers centrally (including FreeMarker, Velocity, and Tiles) without some default conifguration, i.e. without the need to use the new ~WebMvcConfigurer interfaces until customizations are required. Issue: SPR-7093pull/589/merge
26 changed files with 796 additions and 394 deletions
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
/* |
||||
* 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.apache.commons.logging.Log; |
||||
import org.apache.commons.logging.LogFactory; |
||||
import org.springframework.beans.factory.BeanFactoryUtils; |
||||
import org.springframework.beans.factory.ListableBeanFactory; |
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
||||
import org.springframework.context.annotation.ConditionContext; |
||||
import org.springframework.context.annotation.ConfigurationCondition; |
||||
import org.springframework.core.type.AnnotatedTypeMetadata; |
||||
import org.springframework.util.ObjectUtils; |
||||
|
||||
/** |
||||
* A simple configuration condition that checks for the absence of any beans |
||||
* of a given type. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
class BeanTypeNotPresentCondition implements ConfigurationCondition { |
||||
|
||||
private static final Log logger = |
||||
LogFactory.getLog("org.springframework.web.servlet.config.annotation.ViewResolution"); |
||||
|
||||
private final Class<?> beanType; |
||||
|
||||
|
||||
BeanTypeNotPresentCondition(Class<?> beanType) { |
||||
this.beanType = beanType; |
||||
} |
||||
|
||||
|
||||
@Override |
||||
public ConfigurationPhase getConfigurationPhase() { |
||||
return ConfigurationPhase.PARSE_CONFIGURATION; |
||||
} |
||||
|
||||
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { |
||||
ListableBeanFactory factory = context.getBeanFactory(); |
||||
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, this.beanType, false, false); |
||||
if (ObjectUtils.isEmpty(names)) { |
||||
logger.debug("No bean of type [" + this.beanType + "]. Conditional configuration applies."); |
||||
return true; |
||||
} |
||||
else { |
||||
logger.debug("Found bean of type [" + this.beanType + "]. Conditional configuration does not apply."); |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,59 +0,0 @@
@@ -1,59 +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.Configuration; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* This class creates a FreeMarkerConfigurer bean. |
||||
* It is typically imported by adding {@link EnableWebMvc @EnableWebMvc} to an |
||||
* application {@link Configuration @Configuration} class when FreeMarker is |
||||
* in the classpath. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
* @see org.springframework.web.servlet.config.annotation.ViewConfigurationsImportSelector |
||||
*/ |
||||
@Configuration |
||||
public class FreeMarkerConfigurerConfigurationSupport { |
||||
|
||||
private List<WebMvcConfigurationSupport> webMvcConfigurationSupports; |
||||
|
||||
@Autowired(required = false) |
||||
public void setWebMvcConfigurationSupports(List<WebMvcConfigurationSupport> webMvcConfigurationSupports) { |
||||
this.webMvcConfigurationSupports = webMvcConfigurationSupports; |
||||
} |
||||
|
||||
@Bean |
||||
public FreeMarkerConfigurer freeMarkerConfigurer() { |
||||
FreeMarkerConfigurer configurer = null; |
||||
if(webMvcConfigurationSupports != null) { |
||||
for(WebMvcConfigurationSupport configurationSupport : webMvcConfigurationSupports) { |
||||
configurer = configurationSupport.getViewResolutionRegistry().getFreeMarkerConfigurer(); |
||||
if(configurer != null) { |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return configurer; |
||||
} |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* 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,59 +0,0 @@
@@ -1,59 +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.Configuration; |
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* This class creates a TilesConfigurer bean. |
||||
* It is typically imported by adding {@link EnableWebMvc @EnableWebMvc} to an |
||||
* application {@link Configuration @Configuration} class when Tiles 3 is |
||||
* in the classpath. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
* @see org.springframework.web.servlet.config.annotation.ViewConfigurationsImportSelector |
||||
*/ |
||||
@Configuration |
||||
public class TilesConfigurerConfigurationSupport { |
||||
|
||||
private List<WebMvcConfigurationSupport> webMvcConfigurationSupports; |
||||
|
||||
@Autowired(required = false) |
||||
public void setWebMvcConfigurationSupports(List<WebMvcConfigurationSupport> webMvcConfigurationSupports) { |
||||
this.webMvcConfigurationSupports = webMvcConfigurationSupports; |
||||
} |
||||
|
||||
@Bean |
||||
public TilesConfigurer tilesConfigurer() { |
||||
TilesConfigurer configurer = null; |
||||
if(webMvcConfigurationSupports != null) { |
||||
for(WebMvcConfigurationSupport configurationSupport : webMvcConfigurationSupports) { |
||||
configurer = configurationSupport.getViewResolutionRegistry().getTilesConfigurer(); |
||||
if(configurer != null) { |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return configurer; |
||||
} |
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* 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,59 +0,0 @@
@@ -1,59 +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.Configuration; |
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* This class creates the VelocityConfigurer bean. |
||||
* It is typically imported by adding {@link EnableWebMvc @EnableWebMvc} to an |
||||
* application {@link Configuration @Configuration} class when Velocity in the classpath. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
* @see org.springframework.web.servlet.config.annotation.ViewConfigurationsImportSelector |
||||
*/ |
||||
@Configuration |
||||
public class VelocityConfigurerConfigurationSupport { |
||||
|
||||
private List<WebMvcConfigurationSupport> webMvcConfigurationSupports; |
||||
|
||||
@Autowired(required = false) |
||||
public void setWebMvcConfigurationSupports(List<WebMvcConfigurationSupport> webMvcConfigurationSupports) { |
||||
this.webMvcConfigurationSupports = webMvcConfigurationSupports; |
||||
} |
||||
|
||||
@Bean |
||||
public VelocityConfigurer velocityConfigurer() { |
||||
VelocityConfigurer configurer = null; |
||||
if(webMvcConfigurationSupports != null) { |
||||
for(WebMvcConfigurationSupport configurationSupport : webMvcConfigurationSupports) { |
||||
configurer = configurationSupport.getViewResolutionRegistry().getVelocityConfigurer(); |
||||
if(configurer != null) { |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return configurer; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,35 @@
@@ -0,0 +1,35 @@
|
||||
/* |
||||
* 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); |
||||
|
||||
} |
@ -0,0 +1,64 @@
@@ -0,0 +1,64 @@
|
||||
/* |
||||
* 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]); |
||||
} |
||||
|
||||
} |
@ -1,58 +0,0 @@
@@ -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.context.annotation.ImportSelector; |
||||
import org.springframework.core.type.AnnotationMetadata; |
||||
import org.springframework.util.ClassUtils; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* This class imports @{@link org.springframework.context.annotation.Configuration} |
||||
* classes for view configurers based on a classpath criteria. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class ViewConfigurationsImportSelector implements ImportSelector { |
||||
|
||||
private static final boolean tilesPresent = |
||||
ClassUtils.isPresent("org.apache.tiles.startup.TilesInitializer", ViewConfigurationsImportSelector.class.getClassLoader()); |
||||
|
||||
private static final boolean velocityPresent = |
||||
ClassUtils.isPresent("org.apache.velocity.app.VelocityEngine", ViewConfigurationsImportSelector.class.getClassLoader()); |
||||
|
||||
private static final boolean freeMarkerPresent = |
||||
ClassUtils.isPresent("freemarker.template.Configuration", ViewConfigurationsImportSelector.class.getClassLoader()); |
||||
|
||||
@Override |
||||
public String[] selectImports(AnnotationMetadata importingClassMetadata) { |
||||
List<String> configurationClasses = new ArrayList<String>(); |
||||
if(tilesPresent) { |
||||
configurationClasses.add(TilesConfigurerConfigurationSupport.class.getName()); |
||||
} |
||||
if(velocityPresent) { |
||||
configurationClasses.add(VelocityConfigurerConfigurationSupport.class.getName()); |
||||
} |
||||
if(freeMarkerPresent) { |
||||
configurationClasses.add(FreeMarkerConfigurerConfigurationSupport.class.getName()); |
||||
} |
||||
return configurationClasses.toArray(new String[0]); |
||||
} |
||||
} |
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* 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 freeMarkerConfigurer() { |
||||
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); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
/* |
||||
* 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 tilesConfigurer() { |
||||
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); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,76 @@
@@ -0,0 +1,76 @@
|
||||
/* |
||||
* 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 velocityConfigurer() { |
||||
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); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,239 @@
@@ -0,0 +1,239 @@
|
||||
/* |
||||
* 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.junit.Rule; |
||||
import org.junit.Test; |
||||
import org.junit.rules.ExpectedException; |
||||
import org.springframework.context.annotation.Bean; |
||||
import org.springframework.context.annotation.Configuration; |
||||
import org.springframework.mock.web.test.MockHttpServletRequest; |
||||
import org.springframework.mock.web.test.MockHttpServletResponse; |
||||
import org.springframework.mock.web.test.MockServletConfig; |
||||
import org.springframework.mock.web.test.MockServletContext; |
||||
import org.springframework.stereotype.Controller; |
||||
import org.springframework.ui.ModelMap; |
||||
import org.springframework.web.bind.annotation.ModelAttribute; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RequestMethod; |
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; |
||||
import org.springframework.web.servlet.DispatcherServlet; |
||||
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 javax.servlet.ServletException; |
||||
import java.io.IOException; |
||||
|
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* Integration tests for view resolution with {@code @EnableWebMvc}. |
||||
* |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public class ViewResolutionIntegrationTests { |
||||
|
||||
@Rule |
||||
public final ExpectedException thrown = ExpectedException.none(); |
||||
|
||||
|
||||
@Test |
||||
public void minimalFreemarkerConfig() throws Exception { |
||||
MockHttpServletResponse response = runTest(MinimalFreeMarkerWebConfig.class); |
||||
assertEquals("<html><body>Hello World!</body></html>", response.getContentAsString()); |
||||
} |
||||
|
||||
@Test |
||||
public void minimalVelocityConfig() throws Exception { |
||||
MockHttpServletResponse response = runTest(MinimalVelocityWebConfig.class); |
||||
assertEquals("<html><body>Hello World!</body></html>", 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); |
||||
assertEquals("<html><body>Hello World!</body></html>", response.getContentAsString()); |
||||
} |
||||
|
||||
@Test |
||||
public void velocity() throws Exception { |
||||
MockHttpServletResponse response = runTest(VelocityWebConfig.class); |
||||
assertEquals("<html><body>Hello World!</body></html>", response.getContentAsString()); |
||||
} |
||||
|
||||
@Test |
||||
public void tiles() throws Exception { |
||||
MockHttpServletResponse response = runTest(TilesWebConfig.class); |
||||
assertEquals("/WEB-INF/index.jsp", response.getForwardedUrl()); |
||||
} |
||||
|
||||
@Test |
||||
public void freemarkerInvalidConfig() throws Exception { |
||||
this.thrown.expectMessage("It looks like you're trying to configure FreeMarker view resolution."); |
||||
runTest(InvalidFreeMarkerWebConfig.class); |
||||
} |
||||
|
||||
@Test |
||||
public void velocityInvalidConfig() throws Exception { |
||||
this.thrown.expectMessage("It looks like you're trying to configure Velocity view resolution."); |
||||
runTest(InvalidVelocityWebConfig.class); |
||||
} |
||||
|
||||
@Test |
||||
public void tilesInvalidConfig() throws Exception { |
||||
this.thrown.expectMessage("It looks like you're trying to configure Tiles view resolution."); |
||||
runTest(InvalidTilesWebConfig.class); |
||||
} |
||||
|
||||
|
||||
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException { |
||||
String basePath = "org/springframework/web/servlet/config/annotation"; |
||||
MockServletContext servletContext = new MockServletContext(basePath); |
||||
MockServletConfig servletConfig = new MockServletConfig(servletContext); |
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); |
||||
MockHttpServletResponse response = new MockHttpServletResponse(); |
||||
|
||||
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); |
||||
context.register(configClass); |
||||
context.setServletContext(servletContext); |
||||
context.refresh(); |
||||
DispatcherServlet servlet = new DispatcherServlet(context); |
||||
servlet.init(servletConfig); |
||||
servlet.service(request, response); |
||||
return response; |
||||
} |
||||
|
||||
|
||||
@Controller |
||||
static class SampleController { |
||||
|
||||
@RequestMapping(value = "/", method = RequestMethod.GET) |
||||
public String tiles(@ModelAttribute("model") ModelMap model) { |
||||
model.addAttribute("hello", "Hello World!"); |
||||
return "index"; |
||||
} |
||||
} |
||||
|
||||
@EnableWebMvc |
||||
static abstract class AbstractWebConfig extends WebMvcConfigurerAdapter { |
||||
@Bean |
||||
public SampleController sampleController() { |
||||
return new SampleController(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class MinimalFreeMarkerWebConfig extends AbstractWebConfig { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.freemarker(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class MinimalVelocityWebConfig extends AbstractWebConfig { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.velocity(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class MinimalTilesWebConfig extends AbstractWebConfig { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.tiles(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class FreeMarkerWebConfig extends AbstractWebConfig implements FreeMarkerWebMvcConfigurer { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.freemarker(); |
||||
} |
||||
@Override |
||||
public void configureFreeMarker(FreeMarkerConfigurer configurer) { |
||||
configurer.setTemplateLoaderPath("/WEB-INF/"); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class VelocityWebConfig extends AbstractWebConfig implements VelocityWebMvcConfigurer { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.velocity(); |
||||
} |
||||
@Override |
||||
public void configureVelocity(VelocityConfigurer configurer) { |
||||
configurer.setResourceLoaderPath("/WEB-INF/"); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class TilesWebConfig extends AbstractWebConfig implements TilesWebMvcConfigurer { |
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.tiles(); |
||||
} |
||||
@Override |
||||
public void configureTiles(TilesConfigurer configurer) { |
||||
configurer.setDefinitions("/WEB-INF/tiles.xml"); |
||||
} |
||||
} |
||||
@Configuration |
||||
static class InvalidFreeMarkerWebConfig extends WebMvcConfigurationSupport { |
||||
|
||||
// No @EnableWebMvc and no FreeMarkerConfigurer bean
|
||||
|
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.freemarker(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class InvalidVelocityWebConfig extends WebMvcConfigurationSupport { |
||||
|
||||
// No @EnableWebMvc and no VelocityConfigurer bean
|
||||
|
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.velocity(); |
||||
} |
||||
} |
||||
|
||||
@Configuration |
||||
static class InvalidTilesWebConfig extends WebMvcConfigurationSupport { |
||||
|
||||
// No @EnableWebMvc and no TilesConfigurer bean
|
||||
|
||||
@Override |
||||
public void configureViewResolution(ViewResolutionRegistry registry) { |
||||
registry.tiles(); |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
<html><body>${model.hello}</body></html> |
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> |
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> |
||||
<html> |
||||
<head> |
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
||||
<title>My First Web Application Using Spring MVC</title> |
||||
</head> |
||||
<body> |
||||
${model.hello} |
||||
</body> |
||||
</html> |
||||
|
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
<html><body>${model.hello}</body></html> |
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
<!DOCTYPE tiles-definitions PUBLIC "-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN" "http://tiles.apache.org/dtds/tiles-config_3_0.dtd"> |
||||
|
||||
<tiles-definitions> |
||||
|
||||
<definition name="index" template="/WEB-INF/index.jsp" /> |
||||
|
||||
</tiles-definitions> |
Loading…
Reference in new issue