Skip to content

Commit e4e8042

Browse files
committed
Merge pull request eugenp#297 from gmaipady/master
Add spring-thymeleaf example
2 parents 3052a53 + 2ea5de3 commit e4e8042

File tree

16 files changed

+658
-0
lines changed

16 files changed

+658
-0
lines changed

spring-thymeleaf/.classpath

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
<attribute name="org.eclipse.jst.component.dependency" value="/WEB-INF/lib"/>
29+
</attributes>
30+
</classpathentry>
31+
<classpathentry kind="output" path="target/classes"/>
32+
</classpath>

spring-thymeleaf/.project

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>spring-thymeleaf</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.wst.validation.validationbuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>org.eclipse.m2e.core.maven2Builder</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
</buildSpec>
34+
<natures>
35+
<nature>org.eclipse.jdt.core.javanature</nature>
36+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
37+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
38+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
39+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
40+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
41+
</natures>
42+
</projectDescription>

spring-thymeleaf/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
=========
2+
3+
## Spring Thymeleaf Example Project
4+
5+
6+
### Relevant Articles:
7+
8+
9+
### Build the Project
10+
11+
mvn clean install
12+
13+
### Run the Project
14+
mvn cargo:run
15+
- **note**: starts on port '8082'
16+
17+
Access the pages using the URLs:
18+
19+
http://localhost:8082/spring-thymeleaf/
20+
http://localhost:8082/spring-thymeleaf/addStudent/
21+
http://localhost:8082/spring-thymeleaf/listStudents/
22+
23+
The first URL is the home page of the application. The home page has links to the other two pages.

spring-thymeleaf/pom.xml

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>org.baeldung</groupId>
5+
<artifactId>spring-thymeleaf</artifactId>
6+
<version>0.1-SNAPSHOT</version>
7+
<packaging>war</packaging>
8+
<properties>
9+
<java-version>1.7</java-version>
10+
<!-- spring -->
11+
<org.springframework-version>4.1.8.RELEASE</org.springframework-version>
12+
<javax.servlet-version>3.0.1</javax.servlet-version>
13+
<!-- logging -->
14+
<org.slf4j.version>1.7.12</org.slf4j.version>
15+
<logback.version>1.1.3</logback.version>
16+
<!-- thymeleaf -->
17+
<org.thymeleaf-version>2.1.4.RELEASE</org.thymeleaf-version>
18+
<!-- validation -->
19+
<javax.validation-version>1.1.0.Final</javax.validation-version>
20+
<org.hibernate-version>5.1.2.Final</org.hibernate-version>
21+
<!-- Maven plugins -->
22+
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
23+
<maven-war-plugin.version>2.6</maven-war-plugin.version>
24+
<maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
25+
<cargo-maven2-plugin.version>1.4.15</cargo-maven2-plugin.version>
26+
</properties>
27+
<dependencies>
28+
<!-- Spring -->
29+
<dependency>
30+
<groupId>org.springframework</groupId>
31+
<artifactId>spring-context</artifactId>
32+
<version>${org.springframework-version}</version>
33+
<exclusions>
34+
<!-- Exclude Commons Logging in favor of SLF4j -->
35+
<exclusion>
36+
<groupId>commons-logging</groupId>
37+
<artifactId>commons-logging</artifactId>
38+
</exclusion>
39+
</exclusions>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.springframework</groupId>
43+
<artifactId>spring-webmvc</artifactId>
44+
<version>${org.springframework-version}</version>
45+
</dependency>
46+
<!-- Thymeleaf -->
47+
<dependency>
48+
<groupId>org.thymeleaf</groupId>
49+
<artifactId>thymeleaf</artifactId>
50+
<version>${org.thymeleaf-version}</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.thymeleaf</groupId>
54+
<artifactId>thymeleaf-spring4</artifactId>
55+
<version>${org.thymeleaf-version}</version>
56+
</dependency>
57+
<!-- Logging -->
58+
<!-- logging -->
59+
<dependency>
60+
<groupId>org.slf4j</groupId>
61+
<artifactId>slf4j-api</artifactId>
62+
<version>${org.slf4j.version}</version>
63+
</dependency>
64+
<dependency>
65+
<groupId>ch.qos.logback</groupId>
66+
<artifactId>logback-classic</artifactId>
67+
<version>${logback.version}</version>
68+
<!-- <scope>runtime</scope> -->
69+
</dependency>
70+
<dependency>
71+
<groupId>org.slf4j</groupId>
72+
<artifactId>jcl-over-slf4j</artifactId>
73+
<version>${org.slf4j.version}</version>
74+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
75+
</dependency>
76+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
77+
<groupId>org.slf4j</groupId>
78+
<artifactId>log4j-over-slf4j</artifactId>
79+
<version>${org.slf4j.version}</version>
80+
</dependency>
81+
<!-- Servlet -->
82+
<dependency>
83+
<groupId>javax.servlet</groupId>
84+
<artifactId>javax.servlet-api</artifactId>
85+
<version>${javax.servlet-version}</version>
86+
<scope>provided</scope>
87+
</dependency>
88+
<!-- Validation -->
89+
<dependency>
90+
<groupId>javax.validation</groupId>
91+
<artifactId>validation-api</artifactId>
92+
<version>${javax.validation-version}</version>
93+
</dependency>
94+
<dependency>
95+
<groupId>org.hibernate</groupId>
96+
<artifactId>hibernate-validator</artifactId>
97+
<version>${org.hibernate-version}</version>
98+
</dependency>
99+
</dependencies>
100+
<build>
101+
<plugins>
102+
<plugin>
103+
<groupId>org.apache.maven.plugins</groupId>
104+
<artifactId>maven-compiler-plugin</artifactId>
105+
<version>${maven-compiler-plugin.version}</version>
106+
<configuration>
107+
<source>${java-version}</source>
108+
<target>${java-version}</target>
109+
</configuration>
110+
</plugin>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-war-plugin</artifactId>
114+
<version>${maven-war-plugin.version}</version>
115+
</plugin>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-surefire-plugin</artifactId>
119+
<version>${maven-surefire-plugin.version}</version>
120+
<configuration>
121+
<excludes>
122+
</excludes>
123+
<systemPropertyVariables>
124+
</systemPropertyVariables>
125+
</configuration>
126+
</plugin>
127+
<plugin>
128+
<groupId>org.codehaus.cargo</groupId>
129+
<artifactId>cargo-maven2-plugin</artifactId>
130+
<version>${cargo-maven2-plugin.version}</version>
131+
<configuration>
132+
<wait>true</wait>
133+
<container>
134+
<containerId>jetty8x</containerId>
135+
<type>embedded</type>
136+
<systemProperties>
137+
</systemProperties>
138+
</container>
139+
<configuration>
140+
<properties>
141+
<cargo.servlet.port>8082</cargo.servlet.port>
142+
</properties>
143+
</configuration>
144+
</configuration>
145+
</plugin>
146+
</plugins>
147+
</build>
148+
</project>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.baeldung.thymeleaf.controller;
2+
3+
import java.text.DateFormat;
4+
import java.util.Date;
5+
import java.util.Locale;
6+
7+
import org.springframework.stereotype.Controller;
8+
import org.springframework.ui.Model;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestMethod;
11+
12+
/**
13+
* Handles requests for the application home page.
14+
*
15+
*/
16+
@Controller
17+
public class HomeController {
18+
19+
@RequestMapping(value = "/", method = RequestMethod.GET)
20+
public String getHome(Model model) {
21+
22+
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault());
23+
model.addAttribute("serverTime", dateFormat.format(new Date()));
24+
return "home";
25+
}
26+
27+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.baeldung.thymeleaf.controller;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import javax.validation.Valid;
7+
8+
import org.springframework.stereotype.Controller;
9+
import org.springframework.ui.Model;
10+
import org.springframework.validation.BindingResult;
11+
import org.springframework.web.bind.annotation.ModelAttribute;
12+
import org.springframework.web.bind.annotation.RequestMapping;
13+
import org.springframework.web.bind.annotation.RequestMethod;
14+
15+
import org.baeldung.thymeleaf.model.Student;
16+
17+
/**
18+
* Handles requests for the student model.
19+
*
20+
*/
21+
@Controller
22+
public class StudentController {
23+
24+
@RequestMapping(value = "/saveStudent", method = RequestMethod.POST)
25+
public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) {
26+
if (!errors.hasErrors()) {
27+
// get mock objects
28+
List<Student> students = buildStudents();
29+
// add current student
30+
students.add(student);
31+
model.addAttribute("students", students);
32+
}
33+
return ((errors.hasErrors()) ? "addStudent" : "listStudents");
34+
}
35+
36+
@RequestMapping(value = "/addStudent", method = RequestMethod.GET)
37+
public String addStudent(Model model) {
38+
model.addAttribute("student", new Student());
39+
return "addStudent";
40+
}
41+
42+
@RequestMapping(value = "/listStudents", method = RequestMethod.GET)
43+
public String listStudent(Model model) {
44+
45+
model.addAttribute("students", buildStudents());
46+
47+
return "listStudents";
48+
}
49+
50+
private List<Student> buildStudents() {
51+
List<Student> students = new ArrayList<Student>();
52+
53+
Student student1 = new Student();
54+
student1.setId(1001);
55+
student1.setName("John Smith");
56+
student1.setGender('M');
57+
student1.setPercentage(Float.valueOf("80.45"));
58+
59+
students.add(student1);
60+
61+
Student student2 = new Student();
62+
student2.setId(1002);
63+
student2.setName("Jane Williams");
64+
student2.setGender('F');
65+
student2.setPercentage(Float.valueOf("60.25"));
66+
67+
students.add(student2);
68+
return students;
69+
}
70+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.thymeleaf.formatter;
2+
3+
import java.text.ParseException;
4+
import java.util.Locale;
5+
6+
import org.springframework.format.Formatter;
7+
import org.thymeleaf.util.StringUtils;
8+
9+
/**
10+
*
11+
* Name formatter class that implements the Spring Formatter interface.
12+
* Formats a name(String) and return the value with spaces replaced by commas.
13+
*
14+
*/
15+
public class NameFormatter implements Formatter<String> {
16+
17+
@Override
18+
public String print(String input, Locale locale) {
19+
return formatName(input, locale);
20+
}
21+
22+
@Override
23+
public String parse(String input, Locale locale) throws ParseException {
24+
return formatName(input, locale);
25+
}
26+
27+
private String formatName(String input, Locale locale) {
28+
return StringUtils.replace(input, " ", ",");
29+
}
30+
}

0 commit comments

Comments
 (0)