Introduce Freemarker/Velocity/TilesWebMvcConfigurer
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-7093
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -90,6 +90,6 @@ import org.springframework.context.annotation.Import;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
@Documented
|
||||
@Import({DelegatingWebMvcConfiguration.class, ViewConfigurationsImportSelector.class})
|
||||
@Import({DelegatingWebMvcConfiguration.class, ViewResolutionImportSelector.class})
|
||||
public @interface EnableWebMvc {
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -16,12 +16,8 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver} and a
|
||||
@@ -33,15 +29,11 @@ import java.util.List;
|
||||
*/
|
||||
public class FreeMarkerRegistration extends ViewResolutionRegistration<FreeMarkerViewResolver> {
|
||||
|
||||
private final FreeMarkerConfigurer configurer;
|
||||
private List<String> templateLoaderPaths;
|
||||
|
||||
public FreeMarkerRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new FreeMarkerViewResolver());
|
||||
this.configurer = new FreeMarkerConfigurer();
|
||||
this.prefix("");
|
||||
this.suffix(".ftl");
|
||||
this.templateLoaderPath("/WEB-INF/");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,21 +66,4 @@ public class FreeMarkerRegistration extends ViewResolutionRegistration<FreeMarke
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a List of {@code TemplateLoader}s that will be used to search
|
||||
* for templates.
|
||||
*
|
||||
* @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactory#setTemplateLoaderPaths(String...)
|
||||
*/
|
||||
public FreeMarkerRegistration templateLoaderPath(String... templateLoaderPath) {
|
||||
this.templateLoaderPaths= Arrays.asList(templateLoaderPath);
|
||||
return this;
|
||||
}
|
||||
|
||||
protected FreeMarkerConfigurer getConfigurer() {
|
||||
if(this.templateLoaderPaths != null && !this.templateLoaderPaths.isEmpty()) {
|
||||
this.configurer.setTemplateLoaderPaths(this.templateLoaderPaths.toArray(new String[0]));
|
||||
}
|
||||
return this.configurer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
@@ -16,13 +16,8 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Encapsulates information required to create a
|
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesViewResolver} and a
|
||||
@@ -35,8 +30,6 @@ import java.util.List;
|
||||
*/
|
||||
public class TilesRegistration extends ViewResolutionRegistration<TilesViewResolver> {
|
||||
|
||||
private List<String> definitions;
|
||||
private Boolean checkRefresh;
|
||||
|
||||
public TilesRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new TilesViewResolver());
|
||||
@@ -62,38 +55,4 @@ public class TilesRegistration extends ViewResolutionRegistration<TilesViewResol
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Tiles definitions, i.e. a single value or a list of files containing the definitions.
|
||||
*
|
||||
* @see TilesConfigurer#setDefinitions(String...)
|
||||
*/
|
||||
public TilesRegistration definition(String... definitions) {
|
||||
if(this.definitions == null) {
|
||||
this.definitions = new ArrayList<String>();
|
||||
}
|
||||
this.definitions.addAll(Arrays.asList(definitions));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether to check Tiles definition files for a refresh at runtime.
|
||||
*
|
||||
* @see TilesConfigurer#setCheckRefresh(boolean)
|
||||
*/
|
||||
public TilesRegistration checkRefresh(boolean checkRefresh) {
|
||||
this.checkRefresh = checkRefresh;
|
||||
return this;
|
||||
}
|
||||
|
||||
protected TilesConfigurer getTilesConfigurer() {
|
||||
TilesConfigurer tilesConfigurer = new TilesConfigurer();
|
||||
if(this.definitions != null && !this.definitions.isEmpty()) {
|
||||
tilesConfigurer.setDefinitions(this.definitions.toArray(new String[0]));
|
||||
}
|
||||
if(this.checkRefresh != null) {
|
||||
tilesConfigurer.setCheckRefresh(this.checkRefresh);
|
||||
}
|
||||
return tilesConfigurer;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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 @@
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
|
||||
/**
|
||||
@@ -30,24 +29,11 @@ import org.springframework.web.servlet.view.velocity.VelocityViewResolver;
|
||||
*/
|
||||
public class VelocityRegistration extends ViewResolutionRegistration<VelocityViewResolver> {
|
||||
|
||||
private final VelocityConfigurer configurer;
|
||||
|
||||
public VelocityRegistration(ViewResolutionRegistry registry) {
|
||||
super(registry, new VelocityViewResolver());
|
||||
this.configurer = new VelocityConfigurer();
|
||||
this.prefix("");
|
||||
this.suffix(".vm");
|
||||
this.resourceLoaderPath("/WEB-INF/");
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Velocity resource loader path via a Spring resource location.
|
||||
*
|
||||
* @see org.springframework.web.servlet.view.velocity.VelocityConfigurer#setResourceLoaderPath(String)
|
||||
*/
|
||||
public VelocityRegistration resourceLoaderPath(String resourceLoaderPath) {
|
||||
this.configurer.setResourceLoaderPath(resourceLoaderPath);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +66,4 @@ public class VelocityRegistration extends ViewResolutionRegistration<VelocityVie
|
||||
return this;
|
||||
}
|
||||
|
||||
protected VelocityConfigurer getConfigurer() {
|
||||
return this.configurer;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 @@
|
||||
/*
|
||||
* 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 @@
|
||||
/*
|
||||
* 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]);
|
||||
}
|
||||
}
|
||||
@@ -16,8 +16,13 @@
|
||||
|
||||
package org.springframework.web.servlet.config.annotation;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||
import org.springframework.beans.factory.BeanInitializationException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.web.servlet.View;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer;
|
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
|
||||
@@ -35,6 +40,14 @@ public class ViewResolutionRegistry {
|
||||
|
||||
private final List<ViewResolutionRegistration<?>> registrations = new ArrayList<ViewResolutionRegistration<?>>();
|
||||
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
|
||||
public ViewResolutionRegistry(ApplicationContext context) {
|
||||
this.applicationContext = context;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register a custom {@link ViewResolver} bean.
|
||||
*/
|
||||
@@ -79,6 +92,12 @@ public class ViewResolutionRegistry {
|
||||
* default "/WEB-INF/tiles.xml" definition and no Tiles definition check refresh.
|
||||
*/
|
||||
public TilesRegistration tiles() {
|
||||
if (!hasBeanOfType(TilesConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Tiles view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " +
|
||||
"or declare your own TilesConfigurer bean.");
|
||||
}
|
||||
TilesRegistration registration = new TilesRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
@@ -90,6 +109,12 @@ public class ViewResolutionRegistry {
|
||||
* default "" prefix, ".vm" suffix and "/WEB-INF/" resourceLoaderPath.
|
||||
*/
|
||||
public VelocityRegistration velocity() {
|
||||
if (!hasBeanOfType(VelocityConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure Velocity view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " +
|
||||
"or declare your own VelocityConfigurer bean.");
|
||||
}
|
||||
VelocityRegistration registration = new VelocityRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
@@ -101,11 +126,22 @@ public class ViewResolutionRegistry {
|
||||
* "" prefix, ".ftl" suffix and "/WEB-INF/" templateLoaderPath.
|
||||
*/
|
||||
public FreeMarkerRegistration freemarker() {
|
||||
if (!hasBeanOfType(FreeMarkerConfigurer.class)) {
|
||||
throw new BeanInitializationException(
|
||||
"It looks like you're trying to configure FreeMarker view resolution. " +
|
||||
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " +
|
||||
"or declare your own FreeMarkerConfigurer bean.");
|
||||
}
|
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration(this);
|
||||
addAndCheckViewResolution(registration);
|
||||
return registration;
|
||||
}
|
||||
|
||||
protected boolean hasBeanOfType(Class<?> beanType) {
|
||||
return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(
|
||||
this.applicationContext, beanType, false, false));
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a {@link org.springframework.web.servlet.view.ContentNegotiatingViewResolver} bean.
|
||||
*/
|
||||
@@ -118,40 +154,12 @@ public class ViewResolutionRegistry {
|
||||
|
||||
protected List<ViewResolver> getViewResolvers() {
|
||||
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>();
|
||||
|
||||
for(ViewResolutionRegistration<?> registration : this.registrations) {
|
||||
viewResolvers.add(registration.getViewResolver());
|
||||
}
|
||||
return viewResolvers;
|
||||
}
|
||||
|
||||
protected TilesConfigurer getTilesConfigurer() {
|
||||
for(ViewResolutionRegistration<?> registration : this.registrations) {
|
||||
if(registration instanceof TilesRegistration) {
|
||||
return ((TilesRegistration) registration).getTilesConfigurer();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected FreeMarkerConfigurer getFreeMarkerConfigurer() {
|
||||
for(ViewResolutionRegistration<?> registration : this.registrations) {
|
||||
if(registration instanceof FreeMarkerRegistration) {
|
||||
return ((FreeMarkerRegistration) registration).getConfigurer();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
protected VelocityConfigurer getVelocityConfigurer() {
|
||||
for(ViewResolutionRegistration<?> registration : this.registrations) {
|
||||
if(registration instanceof VelocityRegistration) {
|
||||
return ((VelocityRegistration) registration).getConfigurer();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void addAndCheckViewResolution(ViewResolutionRegistration<?> registration) {
|
||||
for(ViewResolutionRegistration<?> existingRegistration : this.registrations) {
|
||||
if(existingRegistration.getClass().equals(registration.getClass())) {
|
||||
|
||||
@@ -152,8 +152,7 @@ import org.springframework.web.util.UrlPathHelper;
|
||||
* <p>When extending directly from this class instead of using
|
||||
* {@link EnableWebMvc @EnableWebMvc}, an extra step is needed if you want to use Tiles, FreeMarker
|
||||
* or Velocity view resolution configuration. Since view configurer beans are registered in their own
|
||||
* {@link org.springframework.web.servlet.config.annotation.TilesConfigurerConfigurationSupport},
|
||||
* {@link org.springframework.web.servlet.config.annotation.FreeMarkerConfigurerConfigurationSupport}
|
||||
* {@link org.springframework.web.servlet.config.annotation.TilesConfigurerConfigurationSupport}
|
||||
* and {@link org.springframework.web.servlet.config.annotation.VelocityConfigurerConfigurationSupport}
|
||||
* classes, you should also extend those configuration classes (only the ones
|
||||
* related to the view technology you are using), or register your own
|
||||
@@ -825,7 +824,7 @@ public class WebMvcConfigurationSupport implements ApplicationContextAware, Serv
|
||||
|
||||
protected ViewResolutionRegistry getViewResolutionRegistry() {
|
||||
if(this.viewResolutionRegistry == null) {
|
||||
this.viewResolutionRegistry = new ViewResolutionRegistry();
|
||||
this.viewResolutionRegistry = new ViewResolutionRegistry(this.applicationContext);
|
||||
configureViewResolution(this.viewResolutionRegistry);
|
||||
}
|
||||
return this.viewResolutionRegistry;
|
||||
|
||||
@@ -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 @@
|
||||
/*
|
||||
* 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 @@
|
||||
/*
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user