Skip to content

Commit 9d9664d

Browse files
author
Eugen
committed
Merge pull request eugenp#313 from vkadapa/master
Vishwanth: Merged my changes of Thymeleaf POC (Proof of Concept)
2 parents c654cb1 + 5aaeb73 commit 9d9664d

File tree

10 files changed

+242
-40
lines changed

10 files changed

+242
-40
lines changed

spring-mvc-java/pom.xml

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34
<groupId>org.baeldung</groupId>
45
<artifactId>spring-mvc-java</artifactId>
56
<version>0.1-SNAPSHOT</version>
6-
77
<name>spring-mvc-java</name>
88
<packaging>war</packaging>
9-
109
<dependencies>
11-
1210
<!-- Spring -->
13-
1411
<dependency>
1512
<groupId>org.springframework</groupId>
1613
<artifactId>spring-web</artifactId>
@@ -21,64 +18,53 @@
2118
<artifactId>spring-webmvc</artifactId>
2219
<version>${org.springframework.version}</version>
2320
</dependency>
24-
2521
<!-- web -->
26-
2722
<dependency>
2823
<groupId>javax.servlet</groupId>
2924
<artifactId>javax.servlet-api</artifactId>
3025
<version>3.0.1</version>
3126
<scope>provided</scope>
3227
</dependency>
33-
3428
<dependency>
3529
<groupId>javax.servlet</groupId>
3630
<artifactId>jstl</artifactId>
3731
<version>1.2</version>
3832
<scope>runtime</scope>
3933
</dependency>
40-
4134
<!-- AOP -->
4235
<dependency>
4336
<groupId>org.springframework</groupId>
4437
<artifactId>spring-aop</artifactId>
4538
<version>${org.springframework.version}</version>
4639
</dependency>
47-
4840
<dependency>
4941
<groupId>org.aspectj</groupId>
5042
<artifactId>aspectjrt</artifactId>
5143
<version>${aspectj.version}</version>
5244
</dependency>
53-
5445
<dependency>
5546
<groupId>org.aspectj</groupId>
5647
<artifactId>aspectjweaver</artifactId>
5748
<version>${aspectj.version}</version>
5849
</dependency>
59-
6050
<!-- logging -->
6151
<dependency>
6252
<groupId>org.slf4j</groupId>
6353
<artifactId>slf4j-api</artifactId>
6454
<version>${org.slf4j.version}</version>
6555
</dependency>
66-
6756
<dependency>
6857
<groupId>org.slf4j</groupId>
6958
<artifactId>slf4j-log4j12</artifactId>
7059
<version>${org.slf4j.version}</version>
7160
</dependency>
72-
7361
<!-- test scoped -->
74-
7562
<dependency>
7663
<groupId>junit</groupId>
7764
<artifactId>junit-dep</artifactId>
7865
<version>${junit.version}</version>
7966
<scope>test</scope>
8067
</dependency>
81-
8268
<dependency>
8369
<groupId>org.hamcrest</groupId>
8470
<artifactId>hamcrest-core</artifactId>
@@ -91,23 +77,30 @@
9177
<version>${org.hamcrest.version}</version>
9278
<scope>test</scope>
9379
</dependency>
94-
9580
<dependency>
9681
<groupId>org.mockito</groupId>
9782
<artifactId>mockito-core</artifactId>
9883
<version>${mockito.version}</version>
9984
<scope>test</scope>
10085
</dependency>
101-
10286
<dependency>
10387
<groupId>org.springframework</groupId>
10488
<artifactId>spring-test</artifactId>
10589
<version>${org.springframework.version}</version>
10690
<scope>test</scope>
10791
</dependency>
108-
92+
<!-- Thymeleaf -->
93+
<dependency>
94+
<groupId>org.thymeleaf</groupId>
95+
<artifactId>thymeleaf-spring4</artifactId>
96+
<version>${thymeleaf.version}</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.thymeleaf</groupId>
100+
<artifactId>thymeleaf</artifactId>
101+
<version>${thymeleaf.version}</version>
102+
</dependency>
109103
</dependencies>
110-
111104
<build>
112105
<finalName>spring-mvc-java</finalName>
113106
<resources>
@@ -116,9 +109,7 @@
116109
<filtering>true</filtering>
117110
</resource>
118111
</resources>
119-
120112
<plugins>
121-
122113
<plugin>
123114
<groupId>org.apache.maven.plugins</groupId>
124115
<artifactId>maven-compiler-plugin</artifactId>
@@ -128,7 +119,10 @@
128119
<target>1.8</target>
129120
</configuration>
130121
</plugin>
131-
122+
<plugin>
123+
<artifactId>maven-resources-plugin</artifactId>
124+
<version>2.7</version>
125+
</plugin>
132126
<plugin>
133127
<groupId>org.apache.maven.plugins</groupId>
134128
<artifactId>maven-war-plugin</artifactId>
@@ -137,7 +131,6 @@
137131
<failOnMissingWebXml>false</failOnMissingWebXml>
138132
</configuration>
139133
</plugin>
140-
141134
<plugin>
142135
<groupId>org.apache.maven.plugins</groupId>
143136
<artifactId>maven-surefire-plugin</artifactId>
@@ -151,7 +144,6 @@
151144
</systemPropertyVariables>
152145
</configuration>
153146
</plugin>
154-
155147
<plugin>
156148
<groupId>org.codehaus.cargo</groupId>
157149
<artifactId>cargo-maven2-plugin</artifactId>
@@ -172,50 +164,38 @@
172164
</configuration>
173165
</configuration>
174166
</plugin>
175-
176167
</plugins>
177-
178168
</build>
179-
180169
<properties>
181170
<!-- Spring -->
182171
<org.springframework.version>4.2.2.RELEASE</org.springframework.version>
183172
<org.springframework.security.version>4.0.2.RELEASE</org.springframework.security.version>
184-
173+
<thymeleaf.version>2.1.4.RELEASE</thymeleaf.version>
185174
<!-- persistence -->
186175
<hibernate.version>4.3.11.Final</hibernate.version>
187176
<mysql-connector-java.version>5.1.36</mysql-connector-java.version>
188-
189177
<!-- logging -->
190178
<org.slf4j.version>1.7.12</org.slf4j.version>
191179
<logback.version>1.1.3</logback.version>
192-
193180
<!-- various -->
194181
<hibernate-validator.version>5.2.1.Final</hibernate-validator.version>
195-
196182
<!-- util -->
197183
<guava.version>18.0</guava.version>
198184
<commons-lang3.version>3.4</commons-lang3.version>
199-
200185
<!-- testing -->
201186
<org.hamcrest.version>1.3</org.hamcrest.version>
202187
<junit.version>4.11</junit.version>
203188
<mockito.version>1.10.19</mockito.version>
204-
205189
<httpcore.version>4.4.1</httpcore.version>
206190
<httpclient.version>4.5</httpclient.version>
207-
208191
<rest-assured.version>2.4.1</rest-assured.version>
209-
210192
<!-- maven plugins -->
211193
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
212194
<maven-war-plugin.version>2.6</maven-war-plugin.version>
213195
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
214196
<maven-resources-plugin.version>2.7</maven-resources-plugin.version>
215197
<cargo-maven2-plugin.version>1.4.15</cargo-maven2-plugin.version>
216-
217198
<!-- AspectJ -->
218199
<aspectj.version>1.8.7</aspectj.version>
219200
</properties>
220-
221201
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.dialect;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.baeldung.processor.NameProcessor;
7+
import org.thymeleaf.dialect.AbstractDialect;
8+
import org.thymeleaf.processor.IProcessor;
9+
10+
public class CustomDialect extends AbstractDialect {
11+
12+
@Override
13+
public String getPrefix() {
14+
return "custom";
15+
}
16+
17+
@Override
18+
public Set<IProcessor> getProcessors() {
19+
final Set<IProcessor> processors = new HashSet<IProcessor>();
20+
processors.add(new NameProcessor());
21+
return processors;
22+
}
23+
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.model;
2+
3+
public class User {
4+
private String firstname;
5+
private String lastname;
6+
private String emailId;
7+
8+
public String getFirstname() {
9+
return firstname;
10+
}
11+
12+
public void setFirstname(final String firstname) {
13+
this.firstname = firstname;
14+
}
15+
16+
public String getLastname() {
17+
return lastname;
18+
}
19+
20+
public void setLastname(final String lastname) {
21+
this.lastname = lastname;
22+
}
23+
24+
public String getEmailId() {
25+
return emailId;
26+
}
27+
28+
public void setEmailId(final String emailId) {
29+
this.emailId = emailId;
30+
}
31+
32+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.processor;
2+
3+
import org.thymeleaf.Arguments;
4+
import org.thymeleaf.dom.Element;
5+
import org.thymeleaf.processor.attr.AbstractTextChildModifierAttrProcessor;
6+
7+
public class NameProcessor extends AbstractTextChildModifierAttrProcessor {
8+
9+
public NameProcessor() {
10+
super("name");
11+
}
12+
13+
@Override
14+
protected String getText(final Arguments arguements, final Element elements, final String attributeName) {
15+
return "Hello, " + elements.getAttributeValue(attributeName) + "!";
16+
}
17+
18+
@Override
19+
public int getPrecedence() {
20+
return 1000;
21+
}
22+
23+
}

spring-mvc-java/src/main/java/org/baeldung/spring/web/config/ClientWebConfig.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,25 @@
11
package org.baeldung.spring.web.config;
22

3+
import java.util.HashSet;
4+
import java.util.Set;
5+
6+
import org.baeldung.dialect.CustomDialect;
7+
import org.springframework.context.MessageSource;
38
import org.springframework.context.annotation.Bean;
49
import org.springframework.context.annotation.Configuration;
10+
import org.springframework.context.annotation.Description;
11+
import org.springframework.context.support.ResourceBundleMessageSource;
512
import org.springframework.web.servlet.ViewResolver;
613
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
14+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
715
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
816
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
917
import org.springframework.web.servlet.view.InternalResourceViewResolver;
1018
import org.springframework.web.servlet.view.JstlView;
19+
import org.thymeleaf.dialect.IDialect;
20+
import org.thymeleaf.spring4.SpringTemplateEngine;
21+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
22+
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
1123

1224
@EnableWebMvc
1325
@Configuration
@@ -26,14 +38,56 @@ public void addViewControllers(final ViewControllerRegistry registry) {
2638
registry.addViewController("/sample.html");
2739
}
2840

41+
@Bean
42+
public ViewResolver thymeleafViewResolver() {
43+
final ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
44+
viewResolver.setTemplateEngine(templateEngine());
45+
viewResolver.setOrder(1);
46+
return viewResolver;
47+
}
48+
2949
@Bean
3050
public ViewResolver viewResolver() {
3151
final InternalResourceViewResolver bean = new InternalResourceViewResolver();
32-
3352
bean.setViewClass(JstlView.class);
3453
bean.setPrefix("/WEB-INF/view/");
3554
bean.setSuffix(".jsp");
36-
55+
bean.setOrder(0);
3756
return bean;
3857
}
58+
59+
@Bean
60+
@Description("Thymeleaf template resolver serving HTML 5")
61+
public ServletContextTemplateResolver templateResolver() {
62+
final ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
63+
templateResolver.setPrefix("/WEB-INF/templates/");
64+
templateResolver.setSuffix(".html");
65+
templateResolver.setTemplateMode("HTML5");
66+
return templateResolver;
67+
}
68+
69+
@Bean
70+
@Description("Thymeleaf template engine with Spring integration")
71+
public SpringTemplateEngine templateEngine() {
72+
final SpringTemplateEngine templateEngine = new SpringTemplateEngine();
73+
templateEngine.setTemplateResolver(templateResolver());
74+
final Set<IDialect> dialects = new HashSet<>();
75+
dialects.add(new CustomDialect());
76+
templateEngine.setAdditionalDialects(dialects);
77+
return templateEngine;
78+
}
79+
80+
@Bean
81+
@Description("Spring message resolver")
82+
public MessageSource messageSource() {
83+
final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
84+
messageSource.setBasename("messages");
85+
return messageSource;
86+
}
87+
88+
@Override
89+
public void addResourceHandlers(final ResourceHandlerRegistry registry) {
90+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
91+
}
92+
3993
}

0 commit comments

Comments
 (0)