diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java index 4390875bed3..0a31cb5aaf3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -94,9 +94,12 @@ public class UrlBasedViewResolver extends ViewResolverSupport /** - * Set the view class to instantiate through {@link #createView(String)}. + * Set the view class that should be used to create views. * @param viewClass a class that is assignable to the required view class - * which by default is AbstractUrlBasedView + * (by default: AbstractUrlBasedView) + * @see #requiredViewClass() + * @see #instantiateView() + * @see AbstractUrlBasedView */ public void setViewClass(@Nullable Class viewClass) { if (viewClass != null && !requiredViewClass().isAssignableFrom(viewClass)) { @@ -109,21 +112,13 @@ public class UrlBasedViewResolver extends ViewResolverSupport /** * Return the view class to be used to create views. + * @see #setViewClass */ @Nullable protected Class getViewClass() { return this.viewClass; } - /** - * Return the required type of view for this resolver. - * This implementation returns {@link AbstractUrlBasedView}. - * @see AbstractUrlBasedView - */ - protected Class requiredViewClass() { - return AbstractUrlBasedView.class; - } - /** * Set the prefix that gets prepended to view names when building a URL. */ @@ -265,6 +260,29 @@ public class UrlBasedViewResolver extends ViewResolverSupport return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName)); } + /** + * Return the required type of view for this resolver. + * This implementation returns {@link AbstractUrlBasedView}. + * @see #instantiateView() + * @see AbstractUrlBasedView + */ + protected Class requiredViewClass() { + return AbstractUrlBasedView.class; + } + + /** + * Instantiate the specified view class. + *

The default implementation uses reflection to instantiate the class. + * @return a new instance of the view class + * @since 5.3 + * @see #setViewClass + */ + protected AbstractUrlBasedView instantiateView() { + Class viewClass = getViewClass(); + Assert.state(viewClass != null, "No view class"); + return (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass); + } + /** * Creates a new View instance of the specified view class and configures it. *

Does not perform any lookup for pre-defined View instances. @@ -277,10 +295,7 @@ public class UrlBasedViewResolver extends ViewResolverSupport * @see #applyLifecycleMethods */ protected AbstractUrlBasedView createView(String viewName) { - Class viewClass = getViewClass(); - Assert.state(viewClass != null, "No view class"); - - AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass); + AbstractUrlBasedView view = instantiateView(); view.setSupportedMediaTypes(getSupportedMediaTypes()); view.setDefaultCharset(getDefaultCharset()); view.setUrl(getPrefix() + viewName + getSuffix()); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewResolver.java index 006bef990d6..89e021229e7 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/freemarker/FreeMarkerViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -16,6 +16,7 @@ package org.springframework.web.reactive.result.view.freemarker; +import org.springframework.web.reactive.result.view.AbstractUrlBasedView; import org.springframework.web.reactive.result.view.UrlBasedViewResolver; /** @@ -57,4 +58,9 @@ public class FreeMarkerViewResolver extends UrlBasedViewResolver { return FreeMarkerView.class; } + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == FreeMarkerView.class ? new FreeMarkerView() : super.instantiateView()); + } + } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateViewResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateViewResolver.java index e6524e965da..0da37e04ae9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateViewResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/script/ScriptTemplateViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -16,6 +16,7 @@ package org.springframework.web.reactive.result.view.script; +import org.springframework.web.reactive.result.view.AbstractUrlBasedView; import org.springframework.web.reactive.result.view.UrlBasedViewResolver; /** @@ -61,4 +62,9 @@ public class ScriptTemplateViewResolver extends UrlBasedViewResolver { return ScriptTemplateView.class; } + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == ScriptTemplateView.class ? new ScriptTemplateView() : super.instantiateView()); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java index 9750405d0e1..e89ba2dcff7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/InternalResourceViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -82,14 +82,6 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver { } - /** - * This resolver requires {@link InternalResourceView}. - */ - @Override - protected Class requiredViewClass() { - return InternalResourceView.class; - } - /** * Specify whether to always include the view rather than forward to it. *

Default is "false". Switch this flag on to enforce the use of a @@ -101,6 +93,17 @@ public class InternalResourceViewResolver extends UrlBasedViewResolver { } + @Override + protected Class requiredViewClass() { + return InternalResourceView.class; + } + + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == InternalResourceView.class ? new InternalResourceView() : + (getViewClass() == JstlView.class ? new JstlView() : super.instantiateView())); + } + @Override protected AbstractUrlBasedView buildView(String viewName) throws Exception { InternalResourceView view = (InternalResourceView) super.buildView(viewName); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/UrlBasedViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/UrlBasedViewResolver.java index 9d3bfe8af22..d25f426d9e4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/UrlBasedViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/UrlBasedViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2020 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. @@ -144,8 +144,10 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements /** * Set the view class that should be used to create views. - * @param viewClass class that is assignable to the required view class - * (by default, AbstractUrlBasedView) + * @param viewClass a class that is assignable to the required view class + * (by default: AbstractUrlBasedView) + * @see #requiredViewClass() + * @see #instantiateView() * @see AbstractUrlBasedView */ public void setViewClass(@Nullable Class viewClass) { @@ -158,21 +160,13 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements /** * Return the view class to be used to create views. + * @see #setViewClass */ @Nullable protected Class getViewClass() { return this.viewClass; } - /** - * Return the required type of view for this resolver. - * This implementation returns AbstractUrlBasedView. - * @see AbstractUrlBasedView - */ - protected Class requiredViewClass() { - return AbstractUrlBasedView.class; - } - /** * Set the prefix that gets prepended to view names when building a URL. */ @@ -510,6 +504,29 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements return (viewNames == null || PatternMatchUtils.simpleMatch(viewNames, viewName)); } + /** + * Return the required type of view for this resolver. + * This implementation returns {@link AbstractUrlBasedView}. + * @see #instantiateView() + * @see AbstractUrlBasedView + */ + protected Class requiredViewClass() { + return AbstractUrlBasedView.class; + } + + /** + * Instantiate the specified view class. + *

The default implementation uses reflection to instantiate the class. + * @return a new instance of the view class + * @since 5.3 + * @see #setViewClass + */ + protected AbstractUrlBasedView instantiateView() { + Class viewClass = getViewClass(); + Assert.state(viewClass != null, "No view class"); + return (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass); + } + /** * Delegates to {@code buildView} for creating a new instance of the * specified view class. Applies the following Spring lifecycle methods @@ -547,10 +564,7 @@ public class UrlBasedViewResolver extends AbstractCachingViewResolver implements * @see #loadView(String, java.util.Locale) */ protected AbstractUrlBasedView buildView(String viewName) throws Exception { - Class viewClass = getViewClass(); - Assert.state(viewClass != null, "No view class"); - - AbstractUrlBasedView view = (AbstractUrlBasedView) BeanUtils.instantiateClass(viewClass); + AbstractUrlBasedView view = instantiateView(); view.setUrl(getPrefix() + viewName + getSuffix()); view.setAttributesMap(getAttributesMap()); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java index 7c304be0094..b3fd7b7ffdc 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/freemarker/FreeMarkerViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2009 the original author or authors. + * Copyright 2002-2020 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. @@ -17,6 +17,7 @@ package org.springframework.web.servlet.view.freemarker; import org.springframework.web.servlet.view.AbstractTemplateViewResolver; +import org.springframework.web.servlet.view.AbstractUrlBasedView; /** * Convenience subclass of {@link org.springframework.web.servlet.view.UrlBasedViewResolver} @@ -70,4 +71,9 @@ public class FreeMarkerViewResolver extends AbstractTemplateViewResolver { return FreeMarkerView.class; } + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == FreeMarkerView.class ? new FreeMarkerView() : super.instantiateView()); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java index 6fba48b8e78..c1e582261a7 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/groovy/GroovyMarkupViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2015 the original author or authors. + * Copyright 2002-2020 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. @@ -19,6 +19,7 @@ package org.springframework.web.servlet.view.groovy; import java.util.Locale; import org.springframework.web.servlet.view.AbstractTemplateViewResolver; +import org.springframework.web.servlet.view.AbstractUrlBasedView; /** * Convenience subclass of @link AbstractTemplateViewResolver} that supports @@ -65,6 +66,11 @@ public class GroovyMarkupViewResolver extends AbstractTemplateViewResolver { return GroovyMarkupView.class; } + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == GroovyMarkupView.class ? new GroovyMarkupView() : super.instantiateView()); + } + /** * This resolver supports i18n, so cache keys should contain the locale. */ diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java index abc1d5ef050..cb454eeb189 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -16,6 +16,7 @@ package org.springframework.web.servlet.view.script; +import org.springframework.web.servlet.view.AbstractUrlBasedView; import org.springframework.web.servlet.view.UrlBasedViewResolver; /** @@ -62,4 +63,9 @@ public class ScriptTemplateViewResolver extends UrlBasedViewResolver { return ScriptTemplateView.class; } + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == ScriptTemplateView.class ? new ScriptTemplateView() : super.instantiateView()); + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesViewResolver.java index 8c4aa82b5c5..33ba0451417 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/tiles3/TilesViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -19,6 +19,7 @@ package org.springframework.web.servlet.view.tiles3; import org.apache.tiles.request.render.Renderer; import org.springframework.lang.Nullable; +import org.springframework.web.servlet.view.AbstractUrlBasedView; import org.springframework.web.servlet.view.UrlBasedViewResolver; /** @@ -40,19 +41,14 @@ public class TilesViewResolver extends UrlBasedViewResolver { private Boolean alwaysInclude; - public TilesViewResolver() { - setViewClass(requiredViewClass()); - } - - /** * This resolver requires {@link TilesView}. */ - @Override - protected Class requiredViewClass() { - return TilesView.class; + public TilesViewResolver() { + setViewClass(requiredViewClass()); } + /** * Set the {@link Renderer} to use. If not specified, a default * {@link org.apache.tiles.renderer.DefinitionRenderer} will be used. @@ -74,6 +70,16 @@ public class TilesViewResolver extends UrlBasedViewResolver { } + @Override + protected Class requiredViewClass() { + return TilesView.class; + } + + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == TilesView.class ? new TilesView() : super.instantiateView()); + } + @Override protected TilesView buildView(String viewName) throws Exception { TilesView view = (TilesView) super.buildView(viewName); diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xslt/XsltViewResolver.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xslt/XsltViewResolver.java index 49ddb4270aa..e004651fe62 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xslt/XsltViewResolver.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/xslt/XsltViewResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2020 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. @@ -53,16 +53,14 @@ public class XsltViewResolver extends UrlBasedViewResolver { private boolean cacheTemplates = true; + /** + * This resolver requires {@link XsltView}. + */ public XsltViewResolver() { setViewClass(requiredViewClass()); } - @Override - protected Class requiredViewClass() { - return XsltView.class; - } - /** * Set the name of the model attribute that represents the XSLT Source. * If not specified, the model map will be searched for a matching value type. @@ -127,6 +125,16 @@ public class XsltViewResolver extends UrlBasedViewResolver { } + @Override + protected Class requiredViewClass() { + return XsltView.class; + } + + @Override + protected AbstractUrlBasedView instantiateView() { + return (getViewClass() == XsltView.class ? new XsltView() : super.instantiateView()); + } + @Override protected AbstractUrlBasedView buildView(String viewName) throws Exception { XsltView view = (XsltView) super.buildView(viewName);