Skip to content

Commit 5e8022f

Browse files
lor6yetanotherallisonf
authored andcommitted
spring template engines (eugenp#2326)
* spring template engines * formatting
1 parent aeec8f5 commit 5e8022f

13 files changed

Lines changed: 278 additions & 23 deletions

File tree

‎spring-mvc-simple/pom.xml‎

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@
1515
</parent>
1616

1717
<properties>
18-
<springframework.version>4.3.4.RELEASE</springframework.version>
19-
<maven-war-plugin.version>2.6</maven-war-plugin.version>
18+
<springframework.version>4.3.10.RELEASE</springframework.version>
19+
<maven-war-plugin.version>3.1.0</maven-war-plugin.version>
2020
<jstl.version>1.2</jstl.version>
2121
<javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version>
2222
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
23-
<hibernate-validator.version>5.3.3.Final</hibernate-validator.version>
23+
<hibernate-validator.version>5.4.1.Final</hibernate-validator.version>
2424
<deploy-path>enter-location-of-server</deploy-path>
2525
<fileupload.version>1.3.2</fileupload.version>
26+
<java.version>1.8</java.version>
27+
<org.thymeleaf-version>3.0.7.RELEASE</org.thymeleaf-version>
28+
<groovy.version>2.4.12</groovy.version>
29+
<freemarker.version>2.3.23</freemarker.version>
2630
</properties>
2731

2832
<dependencies>
@@ -79,6 +83,38 @@
7983
<artifactId>commons-fileupload</artifactId>
8084
<version>${fileupload.version}</version>
8185
</dependency>
86+
87+
<!-- thymeleaf dependencies -->
88+
<dependency>
89+
<groupId>org.thymeleaf</groupId>
90+
<artifactId>thymeleaf</artifactId>
91+
<version>${org.thymeleaf-version}</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.thymeleaf</groupId>
95+
<artifactId>thymeleaf-spring4</artifactId>
96+
<version>${org.thymeleaf-version}</version>
97+
</dependency>
98+
99+
<!-- freemarker dependencies -->
100+
<dependency>
101+
<groupId>org.freemarker</groupId>
102+
<artifactId>freemarker</artifactId>
103+
<version>${freemarker.version}</version>
104+
</dependency>
105+
<dependency>
106+
<groupId>org.springframework</groupId>
107+
<artifactId>spring-context-support</artifactId>
108+
<version>${springframework.version}</version>
109+
</dependency>
110+
111+
<!-- groovy template dependency -->
112+
<dependency>
113+
<groupId>org.codehaus.groovy</groupId>
114+
<artifactId>groovy-templates</artifactId>
115+
<version>${groovy.version}</version>
116+
</dependency>
117+
82118
</dependencies>
83119
<build>
84120
<pluginManagement>
@@ -94,6 +130,14 @@
94130
<outputDirectory>${deploy-path}</outputDirectory>
95131
</configuration>
96132
</plugin>
133+
<plugin>
134+
<groupId>org.apache.maven.plugins</groupId>
135+
<artifactId>maven-compiler-plugin</artifactId>
136+
<configuration>
137+
<source>1.8</source>
138+
<target>1.8</target>
139+
</configuration>
140+
</plugin>
97141
</plugins>
98142
</pluginManagement>
99143
<finalName>springMvcSimple</finalName>

‎spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/ApplicationConfiguration.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
@Configuration
1414
@EnableWebMvc
15-
@ComponentScan(basePackages = "com.baeldung.springmvcforms")
15+
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
1616
class ApplicationConfiguration extends WebMvcConfigurerAdapter {
1717

1818
@Override
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
8+
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;
9+
10+
@Configuration
11+
@EnableWebMvc
12+
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
13+
public class FreemarkerConfiguration {
14+
@Bean
15+
public FreeMarkerConfigurer freemarkerConfig() {
16+
FreeMarkerConfigurer freeMarkerConfigurer = new FreeMarkerConfigurer();
17+
freeMarkerConfigurer.setTemplateLoaderPath("/WEB-INF/views/");
18+
return freeMarkerConfigurer;
19+
}
20+
21+
@Bean
22+
public FreeMarkerViewResolver freemarkerViewResolver() {
23+
FreeMarkerViewResolver resolver = new FreeMarkerViewResolver();
24+
resolver.setCache(true);
25+
resolver.setPrefix("");
26+
resolver.setSuffix(".ftl");
27+
return resolver;
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
8+
import org.springframework.web.servlet.view.groovy.GroovyMarkupViewResolver;
9+
10+
@Configuration
11+
@EnableWebMvc
12+
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
13+
public class GroovyConfiguration {
14+
15+
@Bean
16+
public GroovyMarkupConfigurer groovyMarkupConfigurer() {
17+
GroovyMarkupConfigurer configurer = new GroovyMarkupConfigurer();
18+
configurer.setResourceLoaderPath("/WEB-INF/views/");
19+
return configurer;
20+
}
21+
22+
@Bean
23+
public GroovyMarkupViewResolver thymeleafViewResolver() {
24+
GroovyMarkupViewResolver viewResolver = new GroovyMarkupViewResolver();
25+
viewResolver.setSuffix(".tpl");
26+
return viewResolver;
27+
}
28+
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.spring.configuration;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
7+
import org.thymeleaf.spring4.SpringTemplateEngine;
8+
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
9+
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
10+
11+
@Configuration
12+
@EnableWebMvc
13+
@ComponentScan(basePackages = { "com.baeldung.springmvcforms", "com.baeldung.spring.controller", "com.baeldung.spring.validator" })
14+
public class ThymeleafConfiguration {
15+
@Bean
16+
public SpringTemplateEngine templateEngine() {
17+
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
18+
templateEngine.setTemplateResolver(thymeleafTemplateResolver());
19+
return templateEngine;
20+
}
21+
22+
@Bean
23+
public SpringResourceTemplateResolver thymeleafTemplateResolver() {
24+
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
25+
templateResolver.setPrefix("/WEB-INF/views/");
26+
templateResolver.setSuffix(".html");
27+
templateResolver.setTemplateMode("HTML5");
28+
return templateResolver;
29+
}
30+
31+
@Bean
32+
public ThymeleafViewResolver thymeleafViewResolver() {
33+
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
34+
viewResolver.setTemplateEngine(templateEngine());
35+
return viewResolver;
36+
}
37+
38+
}

‎spring-mvc-simple/src/main/java/com/baeldung/spring/configuration/WebInitializer.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ public void onStartup(ServletContext container) throws ServletException {
1515

1616
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
1717
ctx.register(ApplicationConfiguration.class);
18+
//ctx.register(ThymeleafConfiguration.class);
19+
//ctx.register(FreemarkerConfiguration.class);
20+
//ctx.register(GroovyConfiguration.class);
1821
ctx.setServletContext(container);
1922

2023
// Manage the lifecycle of the root application context
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.spring.controller;
2+
3+
import org.springframework.stereotype.Controller;
4+
import org.springframework.ui.Model;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.ResponseBody;
8+
9+
import com.baeldung.spring.domain.User;
10+
11+
@Controller
12+
public class UserController {
13+
14+
@GetMapping("/registration")
15+
public String getRegistration(Model model) {
16+
model.addAttribute("user", new User());
17+
return "registration";
18+
}
19+
20+
@GetMapping("/registration-thymeleaf")
21+
public String getRegistrationThymeleaf(Model model) {
22+
model.addAttribute("user", new User());
23+
return "registration-thymeleaf";
24+
}
25+
26+
@GetMapping("/registration-freemarker")
27+
public String getRegistrationFreemarker(Model model) {
28+
model.addAttribute("user", new User());
29+
return "registration-freemarker";
30+
}
31+
32+
@GetMapping("/registration-groovy")
33+
public String getRegistrationGroovy(Model model) {
34+
model.addAttribute("user", new User());
35+
return "registration-groovy";
36+
}
37+
38+
@PostMapping("/register")
39+
@ResponseBody
40+
public void register(User user){
41+
System.out.println(user.getEmail());
42+
}
43+
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.spring.domain;
2+
3+
public class User {
4+
private String email;
5+
private String password;
6+
7+
public String getEmail() {
8+
return email;
9+
}
10+
11+
public void setEmail(String email) {
12+
this.email = email;
13+
}
14+
15+
public String getPassword() {
16+
return password;
17+
}
18+
19+
public void setPassword(String password) {
20+
this.password = password;
21+
}
22+
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<#import "/spring.ftl" as spring/>
2+
<html>
3+
<head>
4+
<meta charset="ISO-8859-1" />
5+
<title>User Registration</title>
6+
</head>
7+
<body>
8+
<form action="register" method="post">
9+
<@spring.bind path="user" />
10+
Email:<@spring.formInput "user.email"/> <br />
11+
Password:<@spring.formPasswordInput "user.password"/> <br />
12+
<input type="submit" value="Submit" />
13+
</form>
14+
</body>
15+
</html>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
yieldUnescaped '<!DOCTYPE html>'
2+
html(lang:'en') {
3+
head {
4+
meta('http-equiv':'"Content-Type" content="text/html; charset=utf-8"')
5+
title('User Registration')
6+
}
7+
body {
8+
form (id:'userForm', action:'register', method:'post') {
9+
label (for:'email', 'Email')
10+
input (name:'email', type:'text', value:user.email?:'')
11+
label (for:'password', 'Password')
12+
input (name:'password', type:'password', value:user.password?:'')
13+
div (class:'form-actions') {
14+
input (type:'submit', value:'Submit')
15+
}
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)