Skip to content

Commit

Permalink
Migrate spring.view properties
Browse files Browse the repository at this point in the history
Migrate `spring.view.prefix` and `spring.view.suffix` to
`spring.mvc.view.prefix` and `spring.mvc.view.suffix` respectively. The
former properties are still handled in a backward compatible way and are
defined as deprecated in the meta-data.

Closes spring-projectsgh-3250
  • Loading branch information
snicoll committed Jun 18, 2015
1 parent 77f303b commit be5e30b
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* view templates
*
* @author Andy Wilkinson
* @author Stephane Nicoll
* @since 1.1.0
*/
public class JspTemplateAvailabilityProvider implements TemplateAvailabilityProvider {
Expand All @@ -34,13 +35,23 @@ public class JspTemplateAvailabilityProvider implements TemplateAvailabilityProv
public boolean isTemplateAvailable(String view, Environment environment,
ClassLoader classLoader, ResourceLoader resourceLoader) {
if (ClassUtils.isPresent("org.apache.jasper.compiler.JspConfig", classLoader)) {
String prefix = environment.getProperty("spring.view.prefix",
String prefix = getProperty(environment, "spring.mvc.view.prefix", "spring.view.prefix",
WebMvcAutoConfiguration.DEFAULT_PREFIX);
String suffix = environment.getProperty("spring.view.suffix",
String suffix = getProperty(environment, "spring.mvc.view.suffix", "spring.view.suffix",
WebMvcAutoConfiguration.DEFAULT_SUFFIX);
return resourceLoader.getResource(prefix + view + suffix).exists();
}
return false;
}

private String getProperty(Environment environment, String key, String deprecatedKey, String defaultValue) {
if (environment.containsProperty(key)) {
return environment.getProperty(key);
}
if (environment.containsProperty(deprecatedKey)) {
return environment.getProperty(deprecatedKey);
}
return defaultValue;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand Down Expand Up @@ -142,12 +141,6 @@ public static class WebMvcAutoConfigurationAdapter extends WebMvcConfigurerAdapt

private static Log logger = LogFactory.getLog(WebMvcConfigurerAdapter.class);

@Value("${spring.view.prefix:}")
private String prefix = "";

@Value("${spring.view.suffix:}")
private String suffix = "";

@Autowired
private ResourceProperties resourceProperties = new ResourceProperties();

Expand Down Expand Up @@ -180,8 +173,8 @@ public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
@ConditionalOnMissingBean(InternalResourceViewResolver.class)
public InternalResourceViewResolver defaultViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix(this.prefix);
resolver.setSuffix(this.suffix);
resolver.setPrefix(this.mvcProperties.getView().getPrefix());
resolver.setSuffix(this.mvcProperties.getView().getSuffix());
return resolver;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.boot.autoconfigure.web;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.DefaultMessageCodesResolver;

Expand All @@ -24,6 +25,7 @@
*
* @author Phillip Webb
* @author Sébastien Deleuze
* @author Stephane Nicoll
* @since 1.1
*/
@ConfigurationProperties("spring.mvc")
Expand Down Expand Up @@ -52,6 +54,8 @@ public class WebMvcProperties {

private final Async async = new Async();

private final View view = new View();

public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
return this.messageCodesResolverFormat;
}
Expand Down Expand Up @@ -89,6 +93,10 @@ public Async getAsync() {
return this.async;
}

public View getView() {
return this.view;
}

public static class Async {

/**
Expand All @@ -107,4 +115,35 @@ public void setRequestTimeout(Long requestTimeout) {
}

}

public static class View {

/**
* Spring MVC view prefix.
*/
@Value("${spring.view.prefix:}")
private String prefix;

/**
* Spring MVC view suffx.
*/
@Value("${spring.view.suffix:}")
private String suffix;

public String getPrefix() {
return prefix;
}

public void setPrefix(String prefix) {
this.prefix = prefix;
}

public String getSuffix() {
return suffix;
}

public void setSuffix(String suffix) {
this.suffix = suffix;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,14 @@
{
"name": "spring.view.prefix",
"type": "java.lang.String",
"description": "Spring MVC view prefix."
"description": "Spring MVC view prefix.",
"deprecated": true
},
{
"name": "spring.view.suffix",
"type": "java.lang.String",
"description": "Spring MVC view suffix."
"description": "Spring MVC view suffix.",
"deprecated": true
}
]}

Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ content into your application; rather pick only the properties that you need.
spring.mvc.message-codes-resolver-format= # PREFIX_ERROR_CODE / POSTFIX_ERROR_CODE
spring.mvc.ignore-default-model-on-redirect=true # if the the content of the "default" model should be ignored redirects
spring.mvc.async.request-timeout= # async request timeout in milliseconds
spring.view.prefix= # MVC view prefix
spring.view.suffix= # ... and suffix
spring.mvc.view.prefix= # MVC view prefix
spring.mvc.view.suffix= # ... and suffix
# SPRING RESOURCES HANDLING ({sc-spring-boot-autoconfigure}/web/ResourceProperties.{sc-ext}[ResourceProperties])
spring.resources.cache-period= # cache timeouts in headers sent to browser
Expand Down
2 changes: 1 addition & 1 deletion spring-boot-docs/src/main/asciidoc/howto.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ added.
resources and JSP pages if you are using those). It applies a prefix and a suffix to the
view name and then looks for a physical resource with that path in the servlet context
(defaults are both empty, but accessible for external configuration via
`spring.view.prefix` and `spring.view.suffix`). It can be overridden by providing a
`spring.mvc.view.prefix` and `spring.mvc.view.suffix`). It can be overridden by providing a
bean of the same type.
* A `BeanNameViewResolver` with id '`beanNameViewResolver`'. This is a useful member of the
view resolver chain and will pick up any beans with the same name as the `View` being
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello Phil
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello Phil
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
application.message: Hello Phil

0 comments on commit be5e30b

Please sign in to comment.