From ee40fb8cf153b840836b9abcff2bb112c64cc79a Mon Sep 17 00:00:00 2001 From: Max Bruchmann Date: Wed, 18 Jun 2014 01:39:29 +0200 Subject: [PATCH] Add auto-configuration for Thymeleaf data dialect Closes #1120 --- spring-boot-autoconfigure/pom.xml | 5 ++ .../thymeleaf/ThymeleafAutoConfiguration.java | 60 +++++++++++-------- .../ThymeleafAutoConfigurationTests.java | 11 ++++ .../resources/templates/data-dialect.html | 1 + spring-boot-dependencies/pom.xml | 6 ++ 5 files changed, 59 insertions(+), 24 deletions(-) create mode 100644 spring-boot-autoconfigure/src/test/resources/templates/data-dialect.html diff --git a/spring-boot-autoconfigure/pom.xml b/spring-boot-autoconfigure/pom.xml index 3a7a816b1b..89f18db8c5 100644 --- a/spring-boot-autoconfigure/pom.xml +++ b/spring-boot-autoconfigure/pom.xml @@ -290,6 +290,11 @@ thymeleaf-layout-dialect true + + com.github.mxab.thymeleaf.extras + thymeleaf-extras-data-attribute + true + org.thymeleaf.extras thymeleaf-extras-springsecurity3 diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java index 613399869f..1783291872 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfiguration.java @@ -47,6 +47,8 @@ import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ITemplateResolver; import org.thymeleaf.templateresolver.TemplateResolver; +import com.github.mxab.thymeleaf.extras.dataattribute.dialect.DataAttributeDialect; + /** * {@link EnableAutoConfiguration Auto-configuration} for Thymeleaf. * @@ -66,7 +68,7 @@ public class ThymeleafAutoConfiguration { @Configuration @ConditionalOnMissingBean(name = "defaultTemplateResolver") - public static class DefaultTemplateResolverConfiguration { + public static class DefaultTemplateResolverConfiguration { @Autowired private ThymeleafProperties properties; @@ -74,12 +76,12 @@ public class ThymeleafAutoConfiguration { @Autowired private final ResourceLoader resourceLoader = new DefaultResourceLoader(); - @PostConstruct public void checkTemplateLocationExists() { Boolean checkTemplateLocation = this.properties.isCheckTemplateLocation(); if (checkTemplateLocation) { - Resource resource = this.resourceLoader.getResource(this.properties.getPrefix()); + Resource resource = this.resourceLoader.getResource(this.properties + .getPrefix()); Assert.state(resource.exists(), "Cannot find template location: " + resource + " (please add some templates " + "or check your Thymeleaf configuration)"); @@ -126,7 +128,7 @@ public class ThymeleafAutoConfiguration { private String[] excludedViewNames; public boolean isCheckTemplateLocation() { - return checkTemplateLocation; + return this.checkTemplateLocation; } public void setCheckTemplateLocation(boolean checkTemplateLocation) { @@ -134,7 +136,7 @@ public class ThymeleafAutoConfiguration { } public String getPrefix() { - return prefix; + return this.prefix; } public void setPrefix(String prefix) { @@ -142,7 +144,7 @@ public class ThymeleafAutoConfiguration { } public String getSuffix() { - return suffix; + return this.suffix; } public void setSuffix(String suffix) { @@ -150,7 +152,7 @@ public class ThymeleafAutoConfiguration { } public String getMode() { - return mode; + return this.mode; } public void setMode(String mode) { @@ -158,7 +160,7 @@ public class ThymeleafAutoConfiguration { } public String getEncoding() { - return encoding; + return this.encoding; } public void setEncoding(String encoding) { @@ -166,7 +168,7 @@ public class ThymeleafAutoConfiguration { } public String getContentType() { - return contentType; + return this.contentType; } public void setContentType(String contentType) { @@ -174,7 +176,7 @@ public class ThymeleafAutoConfiguration { } public boolean isCache() { - return cache; + return this.cache; } public void setCache(boolean cache) { @@ -182,7 +184,7 @@ public class ThymeleafAutoConfiguration { } public String[] getExcludedViewNames() { - return excludedViewNames; + return this.excludedViewNames; } public void setExcludedViewNames(String[] excludedViewNames) { @@ -190,7 +192,7 @@ public class ThymeleafAutoConfiguration { } public String[] getViewNames() { - return viewNames; + return this.viewNames; } public void setViewNames(String[] viewNames) { @@ -234,6 +236,28 @@ public class ThymeleafAutoConfiguration { } + @Configuration + @ConditionalOnClass(DataAttributeDialect.class) + protected static class DataAttributeDialectConfiguration { + + @Bean + public DataAttributeDialect dialect() { + return new DataAttributeDialect(); + } + + } + + @Configuration + @ConditionalOnClass({ SpringSecurityDialect.class }) + protected static class ThymeleafSecurityDialectConfiguration { + + @Bean + public SpringSecurityDialect securityDialect() { + return new SpringSecurityDialect(); + } + + } + @Configuration @ConditionalOnClass({ Servlet.class }) protected static class ThymeleafViewResolverConfiguration { @@ -241,7 +265,6 @@ public class ThymeleafAutoConfiguration { @Autowired private ThymeleafProperties properties; - @Autowired private SpringTemplateEngine templateEngine; @@ -270,15 +293,4 @@ public class ThymeleafAutoConfiguration { } - @Configuration - @ConditionalOnClass({ SpringSecurityDialect.class }) - protected static class ThymeleafSecurityDialectConfiguration { - - @Bean - public SpringSecurityDialect securityDialect() { - return new SpringSecurityDialect(); - } - - } - } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java index 89f69be6b2..629b300771 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java @@ -137,4 +137,15 @@ public class ThymeleafAutoConfigurationTests { context.close(); } + @Test + public void useDataDialect() throws Exception { + this.context.register(ThymeleafAutoConfiguration.class, + PropertyPlaceholderAutoConfiguration.class); + this.context.refresh(); + TemplateEngine engine = this.context.getBean(TemplateEngine.class); + Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); + String result = engine.process("data-dialect", attrs); + assertEquals("", result); + } + } diff --git a/spring-boot-autoconfigure/src/test/resources/templates/data-dialect.html b/spring-boot-autoconfigure/src/test/resources/templates/data-dialect.html new file mode 100644 index 0000000000..8f8a82b408 --- /dev/null +++ b/spring-boot-autoconfigure/src/test/resources/templates/data-dialect.html @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/spring-boot-dependencies/pom.xml b/spring-boot-dependencies/pom.xml index 32b770c9e1..a5127ed5ba 100644 --- a/spring-boot-dependencies/pom.xml +++ b/spring-boot-dependencies/pom.xml @@ -115,6 +115,7 @@ 2.1.3.RELEASE 2.1.1.RELEASE 1.2.5 + 1.3 7.0.54 1.7 2.0 @@ -401,6 +402,11 @@ gemfire ${gemfire.version} + + com.github.mxab.thymeleaf.extras + thymeleaf-extras-data-attribute + ${thymeleaf-extras-data-attribute.version} + com.h2database h2