From 0c8f855ff76c2445e80af0fa1eb19c1214ed5637 Mon Sep 17 00:00:00 2001 From: gmaipady Date: Wed, 9 Dec 2015 17:42:35 +0530 Subject: [PATCH] Added spring-thymeleaf initial version --- spring-thymeleaf/.classpath | 32 +++++ spring-thymeleaf/.project | 42 ++++++ spring-thymeleaf/pom.xml | 128 ++++++++++++++++++ .../thymeleaf/controller/HomeController.java | 27 ++++ .../controller/StudentController.java | 65 +++++++++ .../thymeleaf/formatter/NameFormatter.java | 30 ++++ .../org/baeldung/thymeleaf/model/Student.java | 40 ++++++ .../src/main/resources/logback.xml | 20 +++ .../main/resources/messages_en_US.properties | 4 + .../WEB-INF/appServlet/servlet-context.xml | 53 ++++++++ .../src/main/webapp/WEB-INF/root-context.xml | 8 ++ .../main/webapp/WEB-INF/views/addStudent.html | 30 ++++ .../src/main/webapp/WEB-INF/views/home.html | 15 ++ .../webapp/WEB-INF/views/listStudents.html | 24 ++++ .../src/main/webapp/WEB-INF/web.xml | 34 +++++ 15 files changed, 552 insertions(+) create mode 100644 spring-thymeleaf/.classpath create mode 100644 spring-thymeleaf/.project create mode 100644 spring-thymeleaf/pom.xml create mode 100644 spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/HomeController.java create mode 100644 spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/StudentController.java create mode 100644 spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/formatter/NameFormatter.java create mode 100644 spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/model/Student.java create mode 100644 spring-thymeleaf/src/main/resources/logback.xml create mode 100644 spring-thymeleaf/src/main/resources/messages_en_US.properties create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/appServlet/servlet-context.xml create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/root-context.xml create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html create mode 100644 spring-thymeleaf/src/main/webapp/WEB-INF/web.xml diff --git a/spring-thymeleaf/.classpath b/spring-thymeleaf/.classpath new file mode 100644 index 0000000000..28e4a52cd4 --- /dev/null +++ b/spring-thymeleaf/.classpath @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-thymeleaf/.project b/spring-thymeleaf/.project new file mode 100644 index 0000000000..3c898c7e84 --- /dev/null +++ b/spring-thymeleaf/.project @@ -0,0 +1,42 @@ + + + spring-thymeleaf + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.wst.jsdt.core.javascriptValidator + + + + + org.eclipse.wst.common.project.facet.core.builder + + + + + org.eclipse.wst.validation.validationbuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.jdt.core.javanature + org.eclipse.jem.workbench.JavaEMFNature + org.eclipse.wst.common.modulecore.ModuleCoreNature + org.eclipse.m2e.core.maven2Nature + org.eclipse.wst.common.project.facet.core.nature + org.eclipse.wst.jsdt.core.jsNature + + diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml new file mode 100644 index 0000000000..770f67d59c --- /dev/null +++ b/spring-thymeleaf/pom.xml @@ -0,0 +1,128 @@ + + 4.0.0 + org.baeldung + spring-thymeleaf + 0.1-SNAPSHOT + war + + 1.7 + + 4.1.8.RELEASE + 3.0.1 + + 1.7.12 + 1.1.3 + + 2.1.4.RELEASE + + 1.1.0.Final + 5.1.2.Final + + 3.3 + 2.6 + 2.18.1 + + + + + org.springframework + spring-context + ${org.springframework-version} + + + + commons-logging + commons-logging + + + + + org.springframework + spring-webmvc + ${org.springframework-version} + + + + org.thymeleaf + thymeleaf + ${org.thymeleaf-version} + + + org.thymeleaf + thymeleaf-spring4 + ${org.thymeleaf-version} + + + + + org.slf4j + slf4j-api + ${org.slf4j.version} + + + ch.qos.logback + logback-classic + ${logback.version} + + + + org.slf4j + jcl-over-slf4j + ${org.slf4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + javax.servlet + javax.servlet-api + ${javax.servlet-version} + provided + + + + javax.validation + validation-api + ${javax.validation-version} + + + org.hibernate + hibernate-validator + ${org.hibernate-version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java-version} + ${java-version} + + + + org.apache.maven.plugins + maven-war-plugin + ${maven-war-plugin.version} + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + + + + + + + + diff --git a/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/HomeController.java b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/HomeController.java new file mode 100644 index 0000000000..505dc8303b --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/HomeController.java @@ -0,0 +1,27 @@ +package org.baeldung.thymeleaf.controller; + +import java.text.DateFormat; +import java.util.Date; +import java.util.Locale; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +/** + * Handles requests for the application home page. + * + */ +@Controller +public class HomeController { + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String getHome(Model model) { + + DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, Locale.getDefault()); + model.addAttribute("serverTime", dateFormat.format(new Date())); + return "home"; + } + +} diff --git a/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/StudentController.java b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/StudentController.java new file mode 100644 index 0000000000..fb7f52f853 --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/StudentController.java @@ -0,0 +1,65 @@ +package org.baeldung.thymeleaf.controller; + +import java.util.ArrayList; +import java.util.List; + +import javax.validation.Valid; + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.ModelAttribute; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; + +import org.baeldung.thymeleaf.model.Student; + +/** + * Handles requests for the student model. + * + */ +@Controller +public class StudentController { + + @RequestMapping(value = "/saveStudent", method = RequestMethod.POST) + public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) { + if (!errors.hasErrors()) { + // get mock objects + List students = buildStudents(); + // add current student + students.add(student); + model.addAttribute("students", students); + } + return ((errors.hasErrors()) ? "addStudent" : "listStudents"); + } + + @RequestMapping(value = "/addStudent", method = RequestMethod.GET) + public String addStudent(Model model) { + model.addAttribute("student", new Student()); + return "addStudent"; + } + + @RequestMapping(value = "/listStudents", method = RequestMethod.GET) + public String listStudent(Model model) { + + model.addAttribute("students", buildStudents()); + + return "listStudents"; + } + + private List buildStudents() { + List students = new ArrayList(); + + Student student1 = new Student(); + student1.setId(1001); + student1.setName("John Smith"); + students.add(student1); + + Student student2 = new Student(); + student2.setId(1002); + student2.setName("Jane Williams"); + students.add(student2); + + return students; + } +} diff --git a/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/formatter/NameFormatter.java b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/formatter/NameFormatter.java new file mode 100644 index 0000000000..9c07ef8d14 --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/formatter/NameFormatter.java @@ -0,0 +1,30 @@ +package org.baeldung.thymeleaf.formatter; + +import java.text.ParseException; +import java.util.Locale; + +import org.springframework.format.Formatter; +import org.thymeleaf.util.StringUtils; + +/** + * + * Name formatter class that implements the Spring Formatter interface. + * Formats a name(String) and return the value with spaces replaced by commas. + * + */ +public class NameFormatter implements Formatter { + + @Override + public String print(String input, Locale locale) { + return formatName(input, locale); + } + + @Override + public String parse(String input, Locale locale) throws ParseException { + return formatName(input, locale); + } + + private String formatName(String input, Locale locale) { + return StringUtils.replace(input, " ", ","); + } +} diff --git a/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/model/Student.java b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/model/Student.java new file mode 100644 index 0000000000..d34af3961e --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/model/Student.java @@ -0,0 +1,40 @@ +package org.baeldung.thymeleaf.model; + +import java.io.Serializable; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +/** + * + * Simple student POJO with two fields - id and name + * + */ +public class Student implements Serializable { + + private static final long serialVersionUID = -8582553475226281591L; + + @NotNull(message = "Student ID is required.") + @Min(value = 1000, message = "Student ID must be atleast 4 digits.") + private Integer id; + + @NotNull(message = "Student Name is required.") + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + +} diff --git a/spring-thymeleaf/src/main/resources/logback.xml b/spring-thymeleaf/src/main/resources/logback.xml new file mode 100644 index 0000000000..1146dade63 --- /dev/null +++ b/spring-thymeleaf/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + web - %date [%thread] %-5level %logger{36} - %message%n + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-thymeleaf/src/main/resources/messages_en_US.properties b/spring-thymeleaf/src/main/resources/messages_en_US.properties new file mode 100644 index 0000000000..0cfe16cd84 --- /dev/null +++ b/spring-thymeleaf/src/main/resources/messages_en_US.properties @@ -0,0 +1,4 @@ +msg.id=ID +msg.name=Name +welcome.message=Welcome Student !!! + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/appServlet/servlet-context.xml b/spring-thymeleaf/src/main/webapp/WEB-INF/appServlet/servlet-context.xml new file mode 100644 index 0000000000..38a4233c64 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/appServlet/servlet-context.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/root-context.xml b/spring-thymeleaf/src/main/webapp/WEB-INF/root-context.xml new file mode 100644 index 0000000000..5cb2a94d73 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/root-context.xml @@ -0,0 +1,8 @@ + + + + + + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html new file mode 100644 index 0000000000..9358c991e9 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html @@ -0,0 +1,30 @@ + + + +Add Student + + +

Add Student

+
+
    +
  • +
  • +
+ + + + + + + + + + + + +
+
+ + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html new file mode 100644 index 0000000000..7d9d5852e4 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html @@ -0,0 +1,15 @@ + + + +Home + + +

+ +

+

+ Current time is +

+ + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html new file mode 100644 index 0000000000..e35082be50 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html @@ -0,0 +1,24 @@ + + + +Student List + + +

Student List

+ + + + + + + + + + + + + +
+ + diff --git a/spring-thymeleaf/src/main/webapp/WEB-INF/web.xml b/spring-thymeleaf/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000000..275a482c6f --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,34 @@ + + + + + + contextConfigLocation + /WEB-INF/root-context.xml + + + + + org.springframework.web.context.ContextLoaderListener + + + + + appServlet + org.springframework.web.servlet.DispatcherServlet + + contextConfigLocation + /WEB-INF/appServlet/servlet-context.xml + + 1 + + + + appServlet + / + + \ No newline at end of file