Browse Source
Following the separation of FreeMarker/Velocity/TilesConfigurer-related configuration via separate interface, simplify and streamline the view registration helper classes which no longer have much difference (most are UrlBasedViewResolver's). Updates to Javadoc and tests. Issue: SPR-7093pull/589/merge
28 changed files with 800 additions and 887 deletions
@ -1,34 +0,0 @@
@@ -1,34 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.BeanNameViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a |
||||
* {@link org.springframework.web.servlet.view.BeanNameViewResolver} bean. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class BeanNameRegistration extends ViewResolutionRegistration<BeanNameViewResolver> { |
||||
|
||||
public BeanNameRegistration(ViewResolutionRegistry registry) { |
||||
super(registry, new BeanNameViewResolver()); |
||||
} |
||||
|
||||
} |
||||
@ -1,71 +0,0 @@
@@ -1,71 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.View; |
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a {@link ContentNegotiatingViewResolver} bean. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class ContentNegotiatingRegistration extends ViewResolutionRegistration<ContentNegotiatingViewResolver> { |
||||
|
||||
private List<View> defaultViews; |
||||
|
||||
public ContentNegotiatingRegistration(ViewResolutionRegistry registry) { |
||||
super(registry, new ContentNegotiatingViewResolver()); |
||||
} |
||||
|
||||
/** |
||||
* Indicate whether a {@link javax.servlet.http.HttpServletResponse#SC_NOT_ACCEPTABLE 406 Not Acceptable} |
||||
* status code should be returned if no suitable view can be found. |
||||
* |
||||
* @see ContentNegotiatingViewResolver#setUseNotAcceptableStatusCode(boolean) |
||||
*/ |
||||
public ContentNegotiatingRegistration useNotAcceptable(boolean useNotAcceptable) { |
||||
this.viewResolver.setUseNotAcceptableStatusCode(useNotAcceptable); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* Set the default views to use when a more specific view can not be obtained |
||||
* from the {@link org.springframework.web.servlet.ViewResolver} chain. |
||||
* |
||||
* @see ContentNegotiatingViewResolver#setDefaultViews(java.util.List) |
||||
*/ |
||||
public ContentNegotiatingRegistration defaultViews(View... defaultViews) { |
||||
if(this.defaultViews == null) { |
||||
this.defaultViews = new ArrayList<View>(); |
||||
} |
||||
this.defaultViews.addAll(Arrays.asList(defaultViews)); |
||||
return this; |
||||
} |
||||
|
||||
@Override |
||||
protected ContentNegotiatingViewResolver getViewResolver() { |
||||
this.viewResolver.setDefaultViews(this.defaultViews); |
||||
return super.getViewResolver(); |
||||
} |
||||
} |
||||
@ -1,69 +0,0 @@
@@ -1,69 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a |
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver} and a |
||||
* {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} beans. |
||||
* Default configuration is "" prefix, ".ftl" suffix and "/WEB-INF/" templateLoaderPath. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class FreeMarkerRegistration extends ViewResolutionRegistration<FreeMarkerViewResolver> { |
||||
|
||||
|
||||
public FreeMarkerRegistration(ViewResolutionRegistry registry) { |
||||
super(registry, new FreeMarkerViewResolver()); |
||||
this.prefix(""); |
||||
this.suffix(".ftl"); |
||||
} |
||||
|
||||
/** |
||||
* Set the prefix that gets prepended to view names when building a URL. |
||||
* |
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setPrefix(String) |
||||
*/ |
||||
public FreeMarkerRegistration prefix(String prefix) { |
||||
this.viewResolver.setPrefix(prefix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the suffix that gets appended to view names when building a URL. |
||||
* |
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setSuffix(String) |
||||
*/ |
||||
public FreeMarkerRegistration suffix(String suffix) { |
||||
this.viewResolver.setSuffix(suffix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Enable or disable caching. |
||||
* |
||||
* @see org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver#setCache(boolean) |
||||
*/ |
||||
public FreeMarkerRegistration cache(boolean cache) { |
||||
this.viewResolver.setCache(cache); |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create an {@link InternalResourceViewResolver} bean. |
||||
* Default configuration is "/WEB-INF/" prefix and ".jsp" suffix. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class JspRegistration extends ViewResolutionRegistration<InternalResourceViewResolver> { |
||||
|
||||
public JspRegistration(ViewResolutionRegistry registry) { |
||||
this(registry, "/WEB-INF/", ".jsp"); |
||||
} |
||||
|
||||
public JspRegistration(ViewResolutionRegistry registry, String prefix, String suffix) { |
||||
super(registry, new InternalResourceViewResolver()); |
||||
this.viewResolver.setPrefix(prefix); |
||||
this.viewResolver.setSuffix(suffix); |
||||
} |
||||
|
||||
} |
||||
@ -1,58 +0,0 @@
@@ -1,58 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a |
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesViewResolver} and a |
||||
* {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} beans. |
||||
* |
||||
* Default definition is "/WEB-INF/tiles.xml" and no Tiles definition check refresh. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class TilesRegistration extends ViewResolutionRegistration<TilesViewResolver> { |
||||
|
||||
|
||||
public TilesRegistration(ViewResolutionRegistry registry) { |
||||
super(registry, new TilesViewResolver()); |
||||
} |
||||
|
||||
/** |
||||
* Set the prefix that gets prepended to view names when building a URL. |
||||
* |
||||
* @see TilesViewResolver#setPrefix(String) |
||||
*/ |
||||
public TilesRegistration prefix(String prefix) { |
||||
this.viewResolver.setPrefix(prefix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the suffix that gets appended to view names when building a URL. |
||||
* |
||||
* @see TilesViewResolver#setSuffix(String) |
||||
*/ |
||||
public TilesRegistration suffix(String suffix) { |
||||
this.viewResolver.setSuffix(suffix); |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,118 @@
@@ -0,0 +1,118 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.UrlBasedViewResolver; |
||||
|
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* Assist with configuring a {@link org.springframework.web.servlet.view.UrlBasedViewResolver}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public class UrlBasedViewResolverRegistration { |
||||
|
||||
protected final UrlBasedViewResolver viewResolver; |
||||
|
||||
|
||||
public UrlBasedViewResolverRegistration(UrlBasedViewResolver viewResolver) { |
||||
this.viewResolver = viewResolver; |
||||
} |
||||
|
||||
|
||||
protected UrlBasedViewResolver getViewResolver() { |
||||
return this.viewResolver; |
||||
} |
||||
|
||||
/** |
||||
* Set the prefix that gets prepended to view names when building a URL. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setPrefix |
||||
*/ |
||||
public UrlBasedViewResolverRegistration prefix(String prefix) { |
||||
this.viewResolver.setPrefix(prefix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the suffix that gets appended to view names when building a URL. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setSuffix |
||||
*/ |
||||
public UrlBasedViewResolverRegistration suffix(String suffix) { |
||||
this.viewResolver.setSuffix(suffix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the view class that should be used to create views. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewClass |
||||
*/ |
||||
public UrlBasedViewResolverRegistration viewClass(Class<?> viewClass) { |
||||
this.viewResolver.setViewClass(viewClass); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the view names (or name patterns) that can be handled by this view |
||||
* resolver. View names can contain simple wildcards such that 'my*', '*Report' |
||||
* and '*Repo*' will all match the view name 'myReport'. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setViewNames |
||||
*/ |
||||
public UrlBasedViewResolverRegistration viewNames(String... viewNames) { |
||||
this.viewResolver.setViewNames(viewNames); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set static attributes to be added to the model of every request for all |
||||
* views resolved by this view resolver. This allows for setting any kind of |
||||
* attribute values, for example bean references. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setAttributesMap |
||||
*/ |
||||
public UrlBasedViewResolverRegistration attributes(Map<String, ?> attributes) { |
||||
this.viewResolver.setAttributesMap(attributes); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Specify the maximum number of entries for the view cache. |
||||
* Default is 1024. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean) |
||||
*/ |
||||
public UrlBasedViewResolverRegistration cacheLimit(int cacheLimit) { |
||||
this.viewResolver.setCacheLimit(cacheLimit); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Enable or disable caching. |
||||
* <p>This is equivalent to setting the {@link #cacheLimit "cacheLimit"} |
||||
* property to the default limit (1024) or to 0, respectively. |
||||
* <p>Default is "true": caching is enabled. |
||||
* Disable this only for debugging and development. |
||||
* @see org.springframework.web.servlet.view.UrlBasedViewResolver#setCache(boolean) |
||||
*/ |
||||
public UrlBasedViewResolverRegistration cache(boolean cache) { |
||||
this.viewResolver.setCache(cache); |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
@ -1,69 +0,0 @@
@@ -1,69 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a |
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityViewResolver} and a |
||||
* {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer beans}. |
||||
* Default configuration is "" prefix, ".vm" suffix and "/WEB-INF/" resourceLoaderPath. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class VelocityRegistration extends ViewResolutionRegistration<VelocityViewResolver> { |
||||
|
||||
|
||||
public VelocityRegistration(ViewResolutionRegistry registry) { |
||||
super(registry, new VelocityViewResolver()); |
||||
this.prefix(""); |
||||
this.suffix(".vm"); |
||||
} |
||||
|
||||
/** |
||||
* Set the prefix that gets prepended to view names when building a URL. |
||||
* |
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setPrefix(String) |
||||
*/ |
||||
public VelocityRegistration prefix(String prefix) { |
||||
this.viewResolver.setPrefix(prefix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Set the suffix that gets appended to view names when building a URL. |
||||
* |
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setSuffix(String) |
||||
*/ |
||||
public VelocityRegistration suffix(String suffix) { |
||||
this.viewResolver.setSuffix(suffix); |
||||
return this; |
||||
} |
||||
|
||||
/** |
||||
* Enable or disable caching. |
||||
* |
||||
* @see org.springframework.web.servlet.view.velocity.VelocityViewResolver#setCache(boolean) |
||||
*/ |
||||
public VelocityRegistration cache(boolean cache) { |
||||
this.viewResolver.setCache(cache); |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
@ -1,48 +0,0 @@
@@ -1,48 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.web.servlet.ViewResolver; |
||||
|
||||
/** |
||||
* Encapsulates information required to create a view resolver. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class ViewResolutionRegistration<T extends ViewResolver> { |
||||
|
||||
protected final ViewResolutionRegistry registry; |
||||
|
||||
protected final T viewResolver; |
||||
|
||||
public ViewResolutionRegistration(ViewResolutionRegistry registry, T viewResolver) { |
||||
this.registry = registry; |
||||
this.viewResolver = viewResolver; |
||||
} |
||||
|
||||
public ViewResolutionRegistry and() { |
||||
return this.registry; |
||||
} |
||||
|
||||
protected T getViewResolver() { |
||||
return this.viewResolver; |
||||
} |
||||
|
||||
} |
||||
|
||||
|
||||
@ -1,172 +0,0 @@
@@ -1,172 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils; |
||||
import org.springframework.beans.factory.BeanInitializationException; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.util.ObjectUtils; |
||||
import org.springframework.web.servlet.View; |
||||
import org.springframework.web.servlet.ViewResolver; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; |
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; |
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Helps with configuring a list of view resolvers. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @since 4.1 |
||||
*/ |
||||
public class ViewResolutionRegistry { |
||||
|
||||
private final List<ViewResolutionRegistration<?>> registrations = new ArrayList<ViewResolutionRegistration<?>>(); |
||||
|
||||
private final ApplicationContext applicationContext; |
||||
|
||||
|
||||
public ViewResolutionRegistry(ApplicationContext context) { |
||||
this.applicationContext = context; |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Register a custom {@link ViewResolver} bean. |
||||
*/ |
||||
public ViewResolutionRegistration<ViewResolver> addViewResolver(ViewResolver viewResolver) { |
||||
ViewResolutionRegistration<ViewResolver> registration = new ViewResolutionRegistration<ViewResolver>(this, viewResolver); |
||||
registrations.add(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register an {@link org.springframework.web.servlet.view.InternalResourceViewResolver} |
||||
* bean with default "/WEB-INF/" prefix and ".jsp" suffix. |
||||
*/ |
||||
public JspRegistration jsp() { |
||||
JspRegistration registration = new JspRegistration(this); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register an {@link org.springframework.web.servlet.view.InternalResourceViewResolver} |
||||
* bean with specified prefix and suffix. |
||||
*/ |
||||
public JspRegistration jsp(String prefix, String suffix) { |
||||
JspRegistration registration = new JspRegistration(this, prefix, suffix); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link org.springframework.web.servlet.view.BeanNameViewResolver} bean. |
||||
*/ |
||||
public BeanNameRegistration beanName() { |
||||
BeanNameRegistration registration = new BeanNameRegistration(this); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link org.springframework.web.servlet.view.tiles3.TilesViewResolver} and |
||||
* a {@link org.springframework.web.servlet.view.tiles3.TilesConfigurer} with |
||||
* default "/WEB-INF/tiles.xml" definition and no Tiles definition check refresh. |
||||
*/ |
||||
public TilesRegistration tiles() { |
||||
if (!hasBeanOfType(TilesConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure Tiles view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " + |
||||
"or declare your own TilesConfigurer bean."); |
||||
} |
||||
TilesRegistration registration = new TilesRegistration(this); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver} |
||||
* and a {@link org.springframework.web.servlet.view.velocity.VelocityConfigurer} beans with |
||||
* default "" prefix, ".vm" suffix and "/WEB-INF/" resourceLoaderPath. |
||||
*/ |
||||
public VelocityRegistration velocity() { |
||||
if (!hasBeanOfType(VelocityConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure Velocity view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " + |
||||
"or declare your own VelocityConfigurer bean."); |
||||
} |
||||
VelocityRegistration registration = new VelocityRegistration(this); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver} |
||||
* and a {@link org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer} beans with |
||||
* "" prefix, ".ftl" suffix and "/WEB-INF/" templateLoaderPath. |
||||
*/ |
||||
public FreeMarkerRegistration freemarker() { |
||||
if (!hasBeanOfType(FreeMarkerConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure FreeMarker view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " + |
||||
"or declare your own FreeMarkerConfigurer bean."); |
||||
} |
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration(this); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
protected boolean hasBeanOfType(Class<?> beanType) { |
||||
return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( |
||||
this.applicationContext, beanType, false, false)); |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link org.springframework.web.servlet.view.ContentNegotiatingViewResolver} bean. |
||||
*/ |
||||
public ContentNegotiatingRegistration contentNegotiating(View... defaultViews) { |
||||
ContentNegotiatingRegistration registration = new ContentNegotiatingRegistration(this); |
||||
registration.defaultViews(defaultViews); |
||||
addAndCheckViewResolution(registration); |
||||
return registration; |
||||
} |
||||
|
||||
protected List<ViewResolver> getViewResolvers() { |
||||
List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(); |
||||
for(ViewResolutionRegistration<?> registration : this.registrations) { |
||||
viewResolvers.add(registration.getViewResolver()); |
||||
} |
||||
return viewResolvers; |
||||
} |
||||
|
||||
private void addAndCheckViewResolution(ViewResolutionRegistration<?> registration) { |
||||
for(ViewResolutionRegistration<?> existingRegistration : this.registrations) { |
||||
if(existingRegistration.getClass().equals(registration.getClass())) { |
||||
throw new IllegalStateException("An instance of " + registration.getClass().getSimpleName() |
||||
+ " is already registered, and multiple view resolvers and configurers beans are not supported by this registry"); |
||||
} |
||||
} |
||||
registrations.add(registration); |
||||
} |
||||
} |
||||
@ -0,0 +1,290 @@
@@ -0,0 +1,290 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.springframework.beans.factory.BeanFactoryUtils; |
||||
import org.springframework.beans.factory.BeanInitializationException; |
||||
import org.springframework.context.ApplicationContext; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.util.CollectionUtils; |
||||
import org.springframework.util.ObjectUtils; |
||||
import org.springframework.web.accept.ContentNegotiationManager; |
||||
import org.springframework.web.servlet.View; |
||||
import org.springframework.web.servlet.ViewResolver; |
||||
import org.springframework.web.servlet.view.BeanNameViewResolver; |
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; |
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; |
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; |
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; |
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; |
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver; |
||||
|
||||
import java.util.ArrayList; |
||||
import java.util.Arrays; |
||||
import java.util.Collections; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* Assist with the configuration of a chain of |
||||
* {@link org.springframework.web.servlet.ViewResolver ViewResolver} instances. |
||||
* This class is expected to be used via {@link WebMvcConfigurer#configureViewResolvers}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @author Rossen Stoyanchev |
||||
* @since 4.1 |
||||
*/ |
||||
public class ViewResolverRegistry { |
||||
|
||||
private ContentNegotiatingViewResolver contentNegotiatingResolver; |
||||
|
||||
private final List<ViewResolver> viewResolvers = new ArrayList<ViewResolver>(4); |
||||
|
||||
private int order = Ordered.LOWEST_PRECEDENCE; |
||||
|
||||
private ContentNegotiationManager contentNegotiationManager; |
||||
|
||||
private ApplicationContext applicationContext; |
||||
|
||||
|
||||
protected void setContentNegotiationManager(ContentNegotiationManager contentNegotiationManager) { |
||||
this.contentNegotiationManager = contentNegotiationManager; |
||||
} |
||||
|
||||
protected void setApplicationContext(ApplicationContext applicationContext) { |
||||
this.applicationContext = applicationContext; |
||||
} |
||||
|
||||
/** |
||||
* Whether any view resolvers have been registered. |
||||
*/ |
||||
public boolean hasRegistrations() { |
||||
return (this.contentNegotiatingResolver != null || !this.viewResolvers.isEmpty()); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other |
||||
* configured view resolvers and select among all selected Views based on |
||||
* media types requested by the client (e.g. in the Accept header). |
||||
* |
||||
* <p>If invoked multiple times the provided default views will be added to |
||||
* any other default views that may have been configured already. |
||||
* |
||||
* @see ContentNegotiatingViewResolver#setDefaultViews |
||||
*/ |
||||
public void enableContentNegotiation(View... defaultViews) { |
||||
initContentNegotiatingViewResolver(defaultViews); |
||||
} |
||||
|
||||
/** |
||||
* Enable use of a {@link ContentNegotiatingViewResolver} to front all other |
||||
* configured view resolvers and select among all selected Views based on |
||||
* media types requested by the client (e.g. in the Accept header). |
||||
* |
||||
* <p>If invoked multiple times the provided default views will be added to |
||||
* any other default views that may have been configured already. |
||||
* |
||||
* @see ContentNegotiatingViewResolver#setDefaultViews |
||||
*/ |
||||
public void enableContentNegotiation(boolean useNotAcceptableStatus, View... defaultViews) { |
||||
initContentNegotiatingViewResolver(defaultViews); |
||||
this.contentNegotiatingResolver.setUseNotAcceptableStatusCode(useNotAcceptableStatus); |
||||
} |
||||
|
||||
private void initContentNegotiatingViewResolver(View[] defaultViews) { |
||||
|
||||
// ContentNegotiatingResolver in the registry: elevate its precedence!
|
||||
this.order = Ordered.HIGHEST_PRECEDENCE; |
||||
|
||||
if (this.contentNegotiatingResolver != null) { |
||||
if (!ObjectUtils.isEmpty(defaultViews)) { |
||||
if (!CollectionUtils.isEmpty(this.contentNegotiatingResolver.getDefaultViews())) { |
||||
List<View> views = new ArrayList<View>(this.contentNegotiatingResolver.getDefaultViews()); |
||||
views.addAll(Arrays.asList(defaultViews)); |
||||
this.contentNegotiatingResolver.setDefaultViews(views); |
||||
} |
||||
} |
||||
} |
||||
else { |
||||
this.contentNegotiatingResolver = new ContentNegotiatingViewResolver(); |
||||
this.contentNegotiatingResolver.setDefaultViews(Arrays.asList(defaultViews)); |
||||
this.contentNegotiatingResolver.setViewResolvers(this.viewResolvers); |
||||
this.contentNegotiatingResolver.setContentNegotiationManager(this.contentNegotiationManager); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Enable view resolution by forwarding to JSP pages with a default view name |
||||
* prefix of "/WEB-INF/" and a default suffix of ".jsp". |
||||
* |
||||
* <p>This method may be invoked multiple and each call will register a |
||||
* separate ViewResolver instance. Note that since it's not easy to determine |
||||
* if a JSP exists without forwarding to it, using multiple JSP-based view |
||||
* resolvers only makes sense in combination with the "viewNames" property |
||||
* that indicates which view names are handled by which resolver. |
||||
*/ |
||||
public UrlBasedViewResolverRegistration jsp() { |
||||
return jsp("/WEB-INF/", ".jsp"); |
||||
} |
||||
|
||||
/** |
||||
* Enable view resolution by forwarding to JSP pages with the specified |
||||
* prefix and suffix. |
||||
* |
||||
* <p>This method may be invoked multiple and each call will register a |
||||
* separate ViewResolver instance. Note that since it's not easy to determine |
||||
* if a JSP exists without forwarding to it, using multiple JSP-based view |
||||
* resolvers only makes sense in combination with the "viewNames" property |
||||
* that indicates which view names are handled by which resolver. |
||||
*/ |
||||
public UrlBasedViewResolverRegistration jsp(String prefix, String suffix) { |
||||
InternalResourceViewResolver resolver = new InternalResourceViewResolver(); |
||||
resolver.setPrefix(prefix); |
||||
resolver.setSuffix(suffix); |
||||
this.viewResolvers.add(resolver); |
||||
return new UrlBasedViewResolverRegistration(resolver); |
||||
} |
||||
|
||||
/** |
||||
* Enable Tiles-based view resolution. |
||||
* |
||||
* <p>By default tiles definitions are expected to be in "/WEB-INF/tiles.xml". |
||||
* To change that and other Tiles-related options please also implement the |
||||
* interface {@link TilesWebMvcConfigurer}. |
||||
*/ |
||||
public UrlBasedViewResolverRegistration tiles() { |
||||
if (this.applicationContext != null && !hasBeanOfType(TilesConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure Tiles view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcTilesConfiguration, " + |
||||
"or declare your own TilesConfigurer bean."); |
||||
} |
||||
TilesRegistration registration = new TilesRegistration(); |
||||
this.viewResolvers.add(registration.getViewResolver()); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Enable FreeMarker-based view resolution with an empty default view name |
||||
* prefix and a default suffix of ".ftl". |
||||
* |
||||
* <p>By default the FreeMarker template loader path is set to "/WEB-INF/". |
||||
* To change that and other FreeMarker-related options please also implement |
||||
* the interface {@link FreeMarkerWebMvcConfigurer}. |
||||
*/ |
||||
public UrlBasedViewResolverRegistration freeMarker() { |
||||
if (this.applicationContext != null && !hasBeanOfType(FreeMarkerConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure FreeMarker view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcFreeMarkerConfiguration, " + |
||||
"or declare your own FreeMarkerConfigurer bean."); |
||||
} |
||||
FreeMarkerRegistration registration = new FreeMarkerRegistration(); |
||||
this.viewResolvers.add(registration.getViewResolver()); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Enable Velocity-based view resolution with an empty default view name |
||||
* prefix, a default suffix of ".vm". |
||||
* |
||||
* <p>By default the Velocity resource loader path is set to "/WEB-INF/". |
||||
* To change that and other Velocity-related options please also implement |
||||
* the interface {@link VelocityWebMvcConfigurer}. |
||||
*/ |
||||
public UrlBasedViewResolverRegistration velocity() { |
||||
if (this.applicationContext != null && !hasBeanOfType(VelocityConfigurer.class)) { |
||||
throw new BeanInitializationException( |
||||
"It looks like you're trying to configure Velocity view resolution. " + |
||||
"If not using @EnableWebMvc you must import WebMvcVelocityConfiguration, " + |
||||
"or declare your own VelocityConfigurer bean."); |
||||
} |
||||
VelocityRegistration registration = new VelocityRegistration(); |
||||
this.viewResolvers.add(registration.getViewResolver()); |
||||
return registration; |
||||
} |
||||
|
||||
/** |
||||
* Enable the ability to map view names returned from controllers to |
||||
* {@link org.springframework.web.servlet.View} beans. |
||||
*/ |
||||
public void beanName() { |
||||
BeanNameViewResolver resolver = new BeanNameViewResolver(); |
||||
this.viewResolvers.add(resolver); |
||||
} |
||||
|
||||
/** |
||||
* Register a {@link ViewResolver} bean instance. This may be useful to |
||||
* configure a custom (or 3rd party) resolver implementation. It may also be |
||||
* used as an alternative to other registration methods in this class when |
||||
* they don't expose some more advanced property that needs to be set. |
||||
*/ |
||||
public void viewResolver(ViewResolver viewResolver) { |
||||
if (viewResolver instanceof ContentNegotiatingViewResolver) { |
||||
throw new BeanInitializationException( |
||||
"addViewResolver cannot be used to configure a ContentNegotiatingViewResolver. " + |
||||
"Please use the method enableContentNegotiation instead."); |
||||
} |
||||
this.viewResolvers.add(viewResolver); |
||||
} |
||||
|
||||
protected boolean hasBeanOfType(Class<?> beanType) { |
||||
return !ObjectUtils.isEmpty(BeanFactoryUtils.beanNamesForTypeIncludingAncestors( |
||||
this.applicationContext, beanType, false, false)); |
||||
} |
||||
|
||||
|
||||
protected int getOrder() { |
||||
return this.order; |
||||
} |
||||
|
||||
protected List<ViewResolver> getViewResolvers() { |
||||
if (this.contentNegotiatingResolver != null) { |
||||
return Collections.<ViewResolver>singletonList(this.contentNegotiatingResolver); |
||||
} |
||||
else { |
||||
return this.viewResolvers; |
||||
} |
||||
} |
||||
|
||||
|
||||
private static class TilesRegistration extends UrlBasedViewResolverRegistration { |
||||
|
||||
private TilesRegistration() { |
||||
super(new TilesViewResolver()); |
||||
} |
||||
} |
||||
|
||||
private static class VelocityRegistration extends UrlBasedViewResolverRegistration { |
||||
|
||||
private VelocityRegistration() { |
||||
super(new VelocityViewResolver()); |
||||
getViewResolver().setSuffix(".vm"); |
||||
} |
||||
} |
||||
|
||||
private static class FreeMarkerRegistration extends UrlBasedViewResolverRegistration { |
||||
|
||||
private FreeMarkerRegistration() { |
||||
super(new FreeMarkerViewResolver()); |
||||
getViewResolver().setSuffix(".ftl"); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -1,172 +0,0 @@
@@ -1,172 +0,0 @@
|
||||
/* |
||||
* Copyright 2002-2014 the original author or authors. |
||||
* |
||||
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||
* you may not use this file except in compliance with the License. |
||||
* You may obtain a copy of the License at |
||||
* |
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
* |
||||
* Unless required by applicable law or agreed to in writing, software |
||||
* distributed under the License is distributed on an "AS IS" BASIS, |
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||
* See the License for the specific language governing permissions and |
||||
* limitations under the License. |
||||
*/ |
||||
|
||||
package org.springframework.web.servlet.config.annotation; |
||||
|
||||
import org.junit.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.beans.DirectFieldAccessor; |
||||
import org.springframework.web.context.support.StaticWebApplicationContext; |
||||
import org.springframework.web.servlet.view.BeanNameViewResolver; |
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; |
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; |
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; |
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; |
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* Test fixture with a {@link ViewResolutionRegistry}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
*/ |
||||
public class ViewResolutionRegistryTests { |
||||
|
||||
private ViewResolutionRegistry registry; |
||||
|
||||
@Before |
||||
public void setUp() { |
||||
registry = new ViewResolutionRegistry(new StaticWebApplicationContext()); |
||||
} |
||||
|
||||
@Test |
||||
public void noViewResolution() { |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(0, registry.getViewResolvers().size()); |
||||
} |
||||
|
||||
@Test |
||||
public void customViewResolution() { |
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); |
||||
viewResolver.setPrefix("/"); |
||||
viewResolver.setSuffix(".jsp"); |
||||
registry.addViewResolver(viewResolver); |
||||
assertEquals(InternalResourceViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
InternalResourceViewResolver resolver = (InternalResourceViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("/", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".jsp", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
} |
||||
|
||||
@Test |
||||
public void beanNameViewResolution() { |
||||
registry.beanName(); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(BeanNameViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
} |
||||
|
||||
@Test |
||||
public void jspViewResolution() { |
||||
registry.jsp("/", ".jsp"); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(InternalResourceViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
InternalResourceViewResolver resolver = (InternalResourceViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("/", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".jsp", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
} |
||||
|
||||
@Test |
||||
public void defaultJspViewResolution() { |
||||
registry.jsp(); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(InternalResourceViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
InternalResourceViewResolver resolver = (InternalResourceViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("/WEB-INF/", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".jsp", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
} |
||||
|
||||
@Test |
||||
public void tilesViewResolution() { |
||||
this.registry.tiles(); |
||||
assertNotNull(this.registry.getViewResolvers()); |
||||
assertEquals(1, this.registry.getViewResolvers().size()); |
||||
assertEquals(TilesViewResolver.class, this.registry.getViewResolvers().get(0).getClass()); |
||||
} |
||||
|
||||
@Test |
||||
public void velocityViewResolution() { |
||||
registry.velocity().prefix("/").suffix(".vm").cache(true); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(VelocityViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
VelocityViewResolver resolver = (VelocityViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("/", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".vm", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
assertEquals(1024, resolverDirectFieldAccessor.getPropertyValue("cacheLimit")); |
||||
} |
||||
|
||||
@Test |
||||
public void defaultVelocityViewResolution() { |
||||
registry.velocity(); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(VelocityViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
VelocityViewResolver resolver = (VelocityViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".vm", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
} |
||||
|
||||
@Test |
||||
public void freeMarkerViewResolution() { |
||||
registry.freemarker().prefix("/").suffix(".fmt").cache(false); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(FreeMarkerViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
FreeMarkerViewResolver resolver = (FreeMarkerViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("/", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".fmt", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
assertEquals(0, resolverDirectFieldAccessor.getPropertyValue("cacheLimit")); |
||||
} |
||||
|
||||
@Test |
||||
public void defaultFreeMarkerViewResolution() { |
||||
registry.freemarker(); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(FreeMarkerViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
FreeMarkerViewResolver resolver = (FreeMarkerViewResolver)registry.getViewResolvers().get(0); |
||||
DirectFieldAccessor resolverDirectFieldAccessor = new DirectFieldAccessor(resolver); |
||||
assertEquals("", resolverDirectFieldAccessor.getPropertyValue("prefix")); |
||||
assertEquals(".ftl", resolverDirectFieldAccessor.getPropertyValue("suffix")); |
||||
} |
||||
|
||||
@Test |
||||
public void contentNegotiatingViewResolution() { |
||||
registry.contentNegotiating().useNotAcceptable(false).defaultViews(new MappingJackson2JsonView()); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(1, registry.getViewResolvers().size()); |
||||
assertEquals(ContentNegotiatingViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
} |
||||
|
||||
@Test |
||||
public void multipleViewResolutions() { |
||||
registry.jsp().and().beanName(); |
||||
assertNotNull(registry.getViewResolvers()); |
||||
assertEquals(2, registry.getViewResolvers().size()); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,211 @@
@@ -0,0 +1,211 @@
|
||||
/* |
||||
* 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.Before; |
||||
import org.junit.Test; |
||||
import org.springframework.beans.DirectFieldAccessor; |
||||
import org.springframework.core.Ordered; |
||||
import org.springframework.web.accept.ContentNegotiationManager; |
||||
import org.springframework.web.context.support.StaticWebApplicationContext; |
||||
import org.springframework.web.servlet.ViewResolver; |
||||
import org.springframework.web.servlet.view.BeanNameViewResolver; |
||||
import org.springframework.web.servlet.view.ContentNegotiatingViewResolver; |
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; |
||||
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; |
||||
import org.springframework.web.servlet.view.json.MappingJackson2JsonView; |
||||
import org.springframework.web.servlet.view.tiles3.TilesConfigurer; |
||||
import org.springframework.web.servlet.view.tiles3.TilesViewResolver; |
||||
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; |
||||
import org.springframework.web.servlet.view.velocity.VelocityViewResolver; |
||||
import org.springframework.web.servlet.view.xml.MarshallingView; |
||||
|
||||
import java.util.Arrays; |
||||
|
||||
import static org.junit.Assert.*; |
||||
import static org.junit.Assert.assertEquals; |
||||
|
||||
/** |
||||
* Test fixture with a {@link ViewResolverRegistry}. |
||||
* |
||||
* @author Sebastien Deleuze |
||||
* @author Rossen Stoyanchev |
||||
*/ |
||||
public class ViewResolverRegistryTests { |
||||
|
||||
private ViewResolverRegistry registry; |
||||
|
||||
|
||||
@Before |
||||
public void setUp() { |
||||
StaticWebApplicationContext context = new StaticWebApplicationContext(); |
||||
context.registerSingleton("freeMarkerConfigurer", FreeMarkerConfigurer.class); |
||||
context.registerSingleton("velocityConfigurer", VelocityConfigurer.class); |
||||
context.registerSingleton("tilesConfigurer", TilesConfigurer.class); |
||||
this.registry = new ViewResolverRegistry(); |
||||
this.registry.setApplicationContext(context); |
||||
this.registry.setContentNegotiationManager(new ContentNegotiationManager()); |
||||
} |
||||
|
||||
|
||||
@Test |
||||
public void order() { |
||||
assertEquals(Ordered.LOWEST_PRECEDENCE, this.registry.getOrder()); |
||||
this.registry.enableContentNegotiation(); |
||||
assertEquals(Ordered.HIGHEST_PRECEDENCE, this.registry.getOrder()); |
||||
} |
||||
|
||||
@Test |
||||
public void hasRegistrations() { |
||||
assertFalse(this.registry.hasRegistrations()); |
||||
this.registry.velocity(); |
||||
assertTrue(this.registry.hasRegistrations()); |
||||
} |
||||
|
||||
@Test |
||||
public void hasRegistrationsWhenContentNegotiationEnabled() { |
||||
assertFalse(this.registry.hasRegistrations()); |
||||
this.registry.enableContentNegotiation(); |
||||
assertTrue(this.registry.hasRegistrations()); |
||||
} |
||||
|
||||
@Test |
||||
public void noResolvers() { |
||||
assertNotNull(this.registry.getViewResolvers()); |
||||
assertEquals(0, this.registry.getViewResolvers().size()); |
||||
assertFalse(this.registry.hasRegistrations()); |
||||
} |
||||
|
||||
@Test |
||||
public void customViewResolver() { |
||||
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); |
||||
viewResolver.setPrefix("/"); |
||||
viewResolver.setSuffix(".jsp"); |
||||
this.registry.viewResolver(viewResolver); |
||||
assertSame(viewResolver, this.registry.getViewResolvers().get(0)); |
||||
} |
||||
|
||||
@Test |
||||
public void beanName() { |
||||
this.registry.beanName(); |
||||
assertEquals(1, this.registry.getViewResolvers().size()); |
||||
assertEquals(BeanNameViewResolver.class, registry.getViewResolvers().get(0).getClass()); |
||||
} |
||||
|
||||
@Test |
||||
public void jspDefaultValues() { |
||||
this.registry.jsp(); |
||||
InternalResourceViewResolver resolver = checkAndGetResolver(InternalResourceViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "/WEB-INF/", "suffix", ".jsp"); |
||||
} |
||||
|
||||
@Test |
||||
public void jsp() { |
||||
this.registry.jsp("/", ".jsp"); |
||||
InternalResourceViewResolver resolver = checkAndGetResolver(InternalResourceViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "/", "suffix", ".jsp"); |
||||
} |
||||
|
||||
@Test |
||||
public void jspMultipleResolvers() { |
||||
this.registry.jsp().viewNames("view1", "view2"); |
||||
this.registry.jsp().viewNames("view3", "view4"); |
||||
assertNotNull(this.registry.getViewResolvers()); |
||||
assertEquals(2, this.registry.getViewResolvers().size()); |
||||
assertEquals(InternalResourceViewResolver.class, this.registry.getViewResolvers().get(0).getClass()); |
||||
assertEquals(InternalResourceViewResolver.class, this.registry.getViewResolvers().get(1).getClass()); |
||||
} |
||||
|
||||
@Test |
||||
public void tiles() { |
||||
this.registry.tiles(); |
||||
checkAndGetResolver(TilesViewResolver.class); |
||||
} |
||||
|
||||
@Test |
||||
public void velocity() { |
||||
this.registry.velocity().prefix("/").suffix(".vm").cache(true); |
||||
VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "/", "suffix", ".vm", "cacheLimit", 1024); |
||||
} |
||||
|
||||
@Test |
||||
public void velocityDefaultValues() { |
||||
this.registry.velocity(); |
||||
VelocityViewResolver resolver = checkAndGetResolver(VelocityViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "", "suffix", ".vm"); |
||||
} |
||||
|
||||
@Test |
||||
public void freeMarker() { |
||||
this.registry.freeMarker().prefix("/").suffix(".fmt").cache(false); |
||||
FreeMarkerViewResolver resolver = checkAndGetResolver(FreeMarkerViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "/", "suffix", ".fmt", "cacheLimit", 0); |
||||
} |
||||
|
||||
@Test |
||||
public void freeMarkerDefaultValues() { |
||||
this.registry.freeMarker(); |
||||
FreeMarkerViewResolver resolver = checkAndGetResolver(FreeMarkerViewResolver.class); |
||||
checkPropertyValues(resolver, "prefix", "", "suffix", ".ftl"); |
||||
} |
||||
|
||||
@Test |
||||
public void contentNegotiation() { |
||||
MappingJackson2JsonView view = new MappingJackson2JsonView(); |
||||
this.registry.enableContentNegotiation(view); |
||||
ContentNegotiatingViewResolver resolver = checkAndGetResolver(ContentNegotiatingViewResolver.class); |
||||
assertEquals(Arrays.asList(view), resolver.getDefaultViews()); |
||||
assertEquals(Ordered.HIGHEST_PRECEDENCE, this.registry.getOrder()); |
||||
} |
||||
|
||||
@Test |
||||
public void contentNegotiationAddsDefaultViewRegistrations() { |
||||
MappingJackson2JsonView view1 = new MappingJackson2JsonView(); |
||||
this.registry.enableContentNegotiation(view1); |
||||
|
||||
ContentNegotiatingViewResolver resolver1 = checkAndGetResolver(ContentNegotiatingViewResolver.class); |
||||
assertEquals(Arrays.asList(view1), resolver1.getDefaultViews()); |
||||
|
||||
MarshallingView view2 = new MarshallingView(); |
||||
this.registry.enableContentNegotiation(view2); |
||||
|
||||
ContentNegotiatingViewResolver resolver2 = checkAndGetResolver(ContentNegotiatingViewResolver.class); |
||||
assertEquals(Arrays.asList(view1, view2), resolver2.getDefaultViews()); |
||||
assertSame(resolver1, resolver2); |
||||
} |
||||
|
||||
|
||||
@SuppressWarnings("unchecked") |
||||
private <T extends ViewResolver> T checkAndGetResolver(Class<T> resolverType) { |
||||
assertNotNull(this.registry.getViewResolvers()); |
||||
assertEquals(1, this.registry.getViewResolvers().size()); |
||||
assertEquals(resolverType, this.registry.getViewResolvers().get(0).getClass()); |
||||
return (T) registry.getViewResolvers().get(0); |
||||
} |
||||
|
||||
private void checkPropertyValues(ViewResolver resolver, Object... nameValuePairs) { |
||||
DirectFieldAccessor accessor = new DirectFieldAccessor(resolver); |
||||
for (int i = 0; i < nameValuePairs.length ; i++, i++) { |
||||
Object expected = nameValuePairs[i + 1]; |
||||
Object actual = accessor.getPropertyValue((String) nameValuePairs[i]); |
||||
assertEquals(expected, actual); |
||||
} |
||||
} |
||||
|
||||
} |
||||
Loading…
Reference in new issue