Skip to content

Commit ee40fb8

Browse files
Max Bruchmannwilkinsona
Max Bruchmann
authored andcommitted
Add auto-configuration for Thymeleaf data dialect
Closes spring-projects#1120
1 parent 53d2430 commit ee40fb8

File tree

5 files changed

+59
-24
lines changed

5 files changed

+59
-24
lines changed

spring-boot-autoconfigure/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@
290290
<artifactId>thymeleaf-layout-dialect</artifactId>
291291
<optional>true</optional>
292292
</dependency>
293+
<dependency>
294+
<groupId>com.github.mxab.thymeleaf.extras</groupId>
295+
<artifactId>thymeleaf-extras-data-attribute</artifactId>
296+
<optional>true</optional>
297+
</dependency>
293298
<dependency>
294299
<groupId>org.thymeleaf.extras</groupId>
295300
<artifactId>thymeleaf-extras-springsecurity3</artifactId>

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

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import org.thymeleaf.templateresolver.ITemplateResolver;
4848
import org.thymeleaf.templateresolver.TemplateResolver;
4949

50+
import com.github.mxab.thymeleaf.extras.dataattribute.dialect.DataAttributeDialect;
51+
5052
/**
5153
* {@link EnableAutoConfiguration Auto-configuration} for Thymeleaf.
5254
*
@@ -66,20 +68,20 @@ public class ThymeleafAutoConfiguration {
6668

6769
@Configuration
6870
@ConditionalOnMissingBean(name = "defaultTemplateResolver")
69-
public static class DefaultTemplateResolverConfiguration {
71+
public static class DefaultTemplateResolverConfiguration {
7072

7173
@Autowired
7274
private ThymeleafProperties properties;
7375

7476
@Autowired
7577
private final ResourceLoader resourceLoader = new DefaultResourceLoader();
7678

77-
7879
@PostConstruct
7980
public void checkTemplateLocationExists() {
8081
Boolean checkTemplateLocation = this.properties.isCheckTemplateLocation();
8182
if (checkTemplateLocation) {
82-
Resource resource = this.resourceLoader.getResource(this.properties.getPrefix());
83+
Resource resource = this.resourceLoader.getResource(this.properties
84+
.getPrefix());
8385
Assert.state(resource.exists(), "Cannot find template location: "
8486
+ resource + " (please add some templates "
8587
+ "or check your Thymeleaf configuration)");
@@ -126,71 +128,71 @@ public static class ThymeleafProperties {
126128
private String[] excludedViewNames;
127129

128130
public boolean isCheckTemplateLocation() {
129-
return checkTemplateLocation;
131+
return this.checkTemplateLocation;
130132
}
131133

132134
public void setCheckTemplateLocation(boolean checkTemplateLocation) {
133135
this.checkTemplateLocation = checkTemplateLocation;
134136
}
135137

136138
public String getPrefix() {
137-
return prefix;
139+
return this.prefix;
138140
}
139141

140142
public void setPrefix(String prefix) {
141143
this.prefix = prefix;
142144
}
143145

144146
public String getSuffix() {
145-
return suffix;
147+
return this.suffix;
146148
}
147149

148150
public void setSuffix(String suffix) {
149151
this.suffix = suffix;
150152
}
151153

152154
public String getMode() {
153-
return mode;
155+
return this.mode;
154156
}
155157

156158
public void setMode(String mode) {
157159
this.mode = mode;
158160
}
159161

160162
public String getEncoding() {
161-
return encoding;
163+
return this.encoding;
162164
}
163165

164166
public void setEncoding(String encoding) {
165167
this.encoding = encoding;
166168
}
167169

168170
public String getContentType() {
169-
return contentType;
171+
return this.contentType;
170172
}
171173

172174
public void setContentType(String contentType) {
173175
this.contentType = contentType;
174176
}
175177

176178
public boolean isCache() {
177-
return cache;
179+
return this.cache;
178180
}
179181

180182
public void setCache(boolean cache) {
181183
this.cache = cache;
182184
}
183185

184186
public String[] getExcludedViewNames() {
185-
return excludedViewNames;
187+
return this.excludedViewNames;
186188
}
187189

188190
public void setExcludedViewNames(String[] excludedViewNames) {
189191
this.excludedViewNames = excludedViewNames;
190192
}
191193

192194
public String[] getViewNames() {
193-
return viewNames;
195+
return this.viewNames;
194196
}
195197

196198
public void setViewNames(String[] viewNames) {
@@ -234,14 +236,35 @@ public LayoutDialect layoutDialect() {
234236

235237
}
236238

239+
@Configuration
240+
@ConditionalOnClass(DataAttributeDialect.class)
241+
protected static class DataAttributeDialectConfiguration {
242+
243+
@Bean
244+
public DataAttributeDialect dialect() {
245+
return new DataAttributeDialect();
246+
}
247+
248+
}
249+
250+
@Configuration
251+
@ConditionalOnClass({ SpringSecurityDialect.class })
252+
protected static class ThymeleafSecurityDialectConfiguration {
253+
254+
@Bean
255+
public SpringSecurityDialect securityDialect() {
256+
return new SpringSecurityDialect();
257+
}
258+
259+
}
260+
237261
@Configuration
238262
@ConditionalOnClass({ Servlet.class })
239263
protected static class ThymeleafViewResolverConfiguration {
240264

241265
@Autowired
242266
private ThymeleafProperties properties;
243267

244-
245268
@Autowired
246269
private SpringTemplateEngine templateEngine;
247270

@@ -270,15 +293,4 @@ private String appendCharset(String type, String charset) {
270293

271294
}
272295

273-
@Configuration
274-
@ConditionalOnClass({ SpringSecurityDialect.class })
275-
protected static class ThymeleafSecurityDialectConfiguration {
276-
277-
@Bean
278-
public SpringSecurityDialect securityDialect() {
279-
return new SpringSecurityDialect();
280-
}
281-
282-
}
283-
284296
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/thymeleaf/ThymeleafAutoConfigurationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,15 @@ public void createLayoutFromConfigClass() throws Exception {
137137
context.close();
138138
}
139139

140+
@Test
141+
public void useDataDialect() throws Exception {
142+
this.context.register(ThymeleafAutoConfiguration.class,
143+
PropertyPlaceholderAutoConfiguration.class);
144+
this.context.refresh();
145+
TemplateEngine engine = this.context.getBean(TemplateEngine.class);
146+
Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar"));
147+
String result = engine.process("data-dialect", attrs);
148+
assertEquals("<html><body data-foo=\"bar\"></body></html>", result);
149+
}
150+
140151
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<html><body data:foo="${foo}"></body></html>

spring-boot-dependencies/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
<thymeleaf.version>2.1.3.RELEASE</thymeleaf.version>
116116
<thymeleaf-extras-springsecurity3.version>2.1.1.RELEASE</thymeleaf-extras-springsecurity3.version>
117117
<thymeleaf-layout-dialect.version>1.2.5</thymeleaf-layout-dialect.version>
118+
<thymeleaf-extras-data-attribute.version>1.3</thymeleaf-extras-data-attribute.version>
118119
<tomcat.version>7.0.54</tomcat.version>
119120
<velocity.version>1.7</velocity.version>
120121
<velocity-tools.version>2.0</velocity-tools.version>
@@ -401,6 +402,11 @@
401402
<artifactId>gemfire</artifactId>
402403
<version>${gemfire.version}</version>
403404
</dependency>
405+
<dependency>
406+
<groupId>com.github.mxab.thymeleaf.extras</groupId>
407+
<artifactId>thymeleaf-extras-data-attribute</artifactId>
408+
<version>${thymeleaf-extras-data-attribute.version}</version>
409+
</dependency>
404410
<dependency>
405411
<groupId>com.h2database</groupId>
406412
<artifactId>h2</artifactId>

0 commit comments

Comments
 (0)