Browse Source

Use @ConfigurationProperties for Thymeleaf

Add missing properties to complement 6568495

Fixes gh-1250
pull/1253/head
Stephane Nicoll 12 years ago
parent
commit
8f886e22ce
  1. 57
      spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

57
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java

@ -30,14 +30,11 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.env.Environment;
import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.ResourceLoader;
@ -58,6 +55,7 @@ import org.thymeleaf.templateresolver.TemplateResolver;
* @author Stephane Nicoll * @author Stephane Nicoll
*/ */
@Configuration @Configuration
@EnableConfigurationProperties(ThymeleafAutoConfiguration.ThymeleafProperties.class)
@ConditionalOnClass(SpringTemplateEngine.class) @ConditionalOnClass(SpringTemplateEngine.class)
@AutoConfigureAfter(WebMvcAutoConfiguration.class) @AutoConfigureAfter(WebMvcAutoConfiguration.class)
public class ThymeleafAutoConfiguration { public class ThymeleafAutoConfiguration {
@ -67,7 +65,6 @@ public class ThymeleafAutoConfiguration {
public static final String DEFAULT_SUFFIX = ".html"; public static final String DEFAULT_SUFFIX = ".html";
@Configuration @Configuration
@EnableConfigurationProperties(ThymeleafProperties.class)
@ConditionalOnMissingBean(name = "defaultTemplateResolver") @ConditionalOnMissingBean(name = "defaultTemplateResolver")
public static class DefaultTemplateResolverConfiguration { public static class DefaultTemplateResolverConfiguration {
@ -120,8 +117,14 @@ public class ThymeleafAutoConfiguration {
private String encoding = "UTF-8"; private String encoding = "UTF-8";
private String contentType = "text/html";
private boolean cache = true; private boolean cache = true;
private String[] viewNames;
private String[] excludedViewNames;
public boolean isCheckTemplateLocation() { public boolean isCheckTemplateLocation() {
return checkTemplateLocation; return checkTemplateLocation;
} }
@ -162,6 +165,14 @@ public class ThymeleafAutoConfiguration {
this.encoding = encoding; this.encoding = encoding;
} }
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public boolean isCache() { public boolean isCache() {
return cache; return cache;
} }
@ -169,6 +180,22 @@ public class ThymeleafAutoConfiguration {
public void setCache(boolean cache) { public void setCache(boolean cache) {
this.cache = cache; this.cache = cache;
} }
public String[] getExcludedViewNames() {
return excludedViewNames;
}
public void setExcludedViewNames(String[] excludedViewNames) {
this.excludedViewNames = excludedViewNames;
}
public String[] getViewNames() {
return viewNames;
}
public void setViewNames(String[] viewNames) {
this.viewNames = viewNames;
}
} }
@Configuration @Configuration
@ -209,15 +236,11 @@ public class ThymeleafAutoConfiguration {
@Configuration @Configuration
@ConditionalOnClass({ Servlet.class }) @ConditionalOnClass({ Servlet.class })
protected static class ThymeleafViewResolverConfiguration implements EnvironmentAware { protected static class ThymeleafViewResolverConfiguration {
private RelaxedPropertyResolver environment; @Autowired
private ThymeleafProperties properties;
@Override
public void setEnvironment(Environment environment) {
this.environment = new RelaxedPropertyResolver(environment,
"spring.thymeleaf.");
}
@Autowired @Autowired
private SpringTemplateEngine templateEngine; private SpringTemplateEngine templateEngine;
@ -227,15 +250,11 @@ public class ThymeleafAutoConfiguration {
public ThymeleafViewResolver thymeleafViewResolver() { public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver(); ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(this.templateEngine); resolver.setTemplateEngine(this.templateEngine);
resolver.setCharacterEncoding(this.environment.getProperty("encoding", resolver.setCharacterEncoding(this.properties.getEncoding());
"UTF-8")); resolver.setContentType(appendCharset(this.properties.getContentType(),
resolver.setContentType(appendCharset(
this.environment.getProperty("contentType", "text/html"),
resolver.getCharacterEncoding())); resolver.getCharacterEncoding()));
resolver.setExcludedViewNames(this.environment.getProperty( resolver.setExcludedViewNames(this.properties.getExcludedViewNames());
"excludedViewNames", String[].class)); resolver.setViewNames(this.properties.getViewNames());
resolver.setViewNames(this.environment.getProperty("viewNames",
String[].class));
// This resolver acts as a fallback resolver (e.g. like a // This resolver acts as a fallback resolver (e.g. like a
// InternalResourceViewResolver) so it needs to have low precedence // InternalResourceViewResolver) so it needs to have low precedence
resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5); resolver.setOrder(Ordered.LOWEST_PRECEDENCE - 5);

Loading…
Cancel
Save