Skip to content

Commit 0c8f855

Browse files
committed
Added spring-thymeleaf initial version
1 parent f60b24c commit 0c8f855

15 files changed

Lines changed: 552 additions & 0 deletions

File tree

‎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/pom.xml‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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+
</properties>
26+
<dependencies>
27+
<!-- Spring -->
28+
<dependency>
29+
<groupId>org.springframework</groupId>
30+
<artifactId>spring-context</artifactId>
31+
<version>${org.springframework-version}</version>
32+
<exclusions>
33+
<!-- Exclude Commons Logging in favor of SLF4j -->
34+
<exclusion>
35+
<groupId>commons-logging</groupId>
36+
<artifactId>commons-logging</artifactId>
37+
</exclusion>
38+
</exclusions>
39+
</dependency>
40+
<dependency>
41+
<groupId>org.springframework</groupId>
42+
<artifactId>spring-webmvc</artifactId>
43+
<version>${org.springframework-version}</version>
44+
</dependency>
45+
<!-- Thymeleaf -->
46+
<dependency>
47+
<groupId>org.thymeleaf</groupId>
48+
<artifactId>thymeleaf</artifactId>
49+
<version>${org.thymeleaf-version}</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.thymeleaf</groupId>
53+
<artifactId>thymeleaf-spring4</artifactId>
54+
<version>${org.thymeleaf-version}</version>
55+
</dependency>
56+
<!-- Logging -->
57+
<!-- logging -->
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-api</artifactId>
61+
<version>${org.slf4j.version}</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>ch.qos.logback</groupId>
65+
<artifactId>logback-classic</artifactId>
66+
<version>${logback.version}</version>
67+
<!-- <scope>runtime</scope> -->
68+
</dependency>
69+
<dependency>
70+
<groupId>org.slf4j</groupId>
71+
<artifactId>jcl-over-slf4j</artifactId>
72+
<version>${org.slf4j.version}</version>
73+
<!-- <scope>runtime</scope> --> <!-- some spring dependencies need to compile against jcl -->
74+
</dependency>
75+
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
76+
<groupId>org.slf4j</groupId>
77+
<artifactId>log4j-over-slf4j</artifactId>
78+
<version>${org.slf4j.version}</version>
79+
</dependency>
80+
<!-- Servlet -->
81+
<dependency>
82+
<groupId>javax.servlet</groupId>
83+
<artifactId>javax.servlet-api</artifactId>
84+
<version>${javax.servlet-version}</version>
85+
<scope>provided</scope>
86+
</dependency>
87+
<!-- Validation -->
88+
<dependency>
89+
<groupId>javax.validation</groupId>
90+
<artifactId>validation-api</artifactId>
91+
<version>${javax.validation-version}</version>
92+
</dependency>
93+
<dependency>
94+
<groupId>org.hibernate</groupId>
95+
<artifactId>hibernate-validator</artifactId>
96+
<version>${org.hibernate-version}</version>
97+
</dependency>
98+
</dependencies>
99+
<build>
100+
<plugins>
101+
<plugin>
102+
<groupId>org.apache.maven.plugins</groupId>
103+
<artifactId>maven-compiler-plugin</artifactId>
104+
<version>${maven-compiler-plugin.version}</version>
105+
<configuration>
106+
<source>${java-version}</source>
107+
<target>${java-version}</target>
108+
</configuration>
109+
</plugin>
110+
<plugin>
111+
<groupId>org.apache.maven.plugins</groupId>
112+
<artifactId>maven-war-plugin</artifactId>
113+
<version>${maven-war-plugin.version}</version>
114+
</plugin>
115+
<plugin>
116+
<groupId>org.apache.maven.plugins</groupId>
117+
<artifactId>maven-surefire-plugin</artifactId>
118+
<version>${maven-surefire-plugin.version}</version>
119+
<configuration>
120+
<excludes>
121+
</excludes>
122+
<systemPropertyVariables>
123+
</systemPropertyVariables>
124+
</configuration>
125+
</plugin>
126+
</plugins>
127+
</build>
128+
</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: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
students.add(student1);
57+
58+
Student student2 = new Student();
59+
student2.setId(1002);
60+
student2.setName("Jane Williams");
61+
students.add(student2);
62+
63+
return students;
64+
}
65+
}
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+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.baeldung.thymeleaf.model;
2+
3+
import java.io.Serializable;
4+
5+
import javax.validation.constraints.Min;
6+
import javax.validation.constraints.NotNull;
7+
8+
/**
9+
*
10+
* Simple student POJO with two fields - id and name
11+
*
12+
*/
13+
public class Student implements Serializable {
14+
15+
private static final long serialVersionUID = -8582553475226281591L;
16+
17+
@NotNull(message = "Student ID is required.")
18+
@Min(value = 1000, message = "Student ID must be atleast 4 digits.")
19+
private Integer id;
20+
21+
@NotNull(message = "Student Name is required.")
22+
private String name;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<configuration>
2+
3+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
4+
<encoder>
5+
<pattern>web - %date [%thread] %-5level %logger{36} - %message%n
6+
</pattern>
7+
</encoder>
8+
</appender>
9+
10+
<logger name="org.springframework" level="WARN" />
11+
<logger name="org.springframework.transaction" level="WARN" />
12+
13+
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
14+
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
15+
16+
<root level="INFO">
17+
<appender-ref ref="STDOUT" />
18+
</root>
19+
20+
</configuration>
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
msg.id=ID
2+
msg.name=Name
3+
welcome.message=Welcome Student !!!
4+

0 commit comments

Comments
 (0)