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/README.md b/spring-thymeleaf/README.md new file mode 100644 index 0000000000..8cb1c2e982 --- /dev/null +++ b/spring-thymeleaf/README.md @@ -0,0 +1,23 @@ +========= + +## Spring Thymeleaf Example Project + + +### Relevant Articles: + + +### Build the Project + +mvn clean install + +### Run the Project +mvn cargo:run +- **note**: starts on port '8082' + +Access the pages using the URLs: + +http://localhost:8082/spring-thymeleaf/ +http://localhost:8082/spring-thymeleaf/addStudent/ +http://localhost:8082/spring-thymeleaf/listStudents/ + +The first URL is the home page of the application. The home page has links to the other two pages. diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml new file mode 100644 index 0000000000..1a8dff671e --- /dev/null +++ b/spring-thymeleaf/pom.xml @@ -0,0 +1,148 @@ + + 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 + 1.4.15 + + + + + 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} + + + + + + + + + org.codehaus.cargo + cargo-maven2-plugin + ${cargo-maven2-plugin.version} + + true + + jetty8x + embedded + + + + + + 8082 + + + + + + + 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..3bef22cdae --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/controller/StudentController.java @@ -0,0 +1,70 @@ +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"); + student1.setGender('M'); + student1.setPercentage(Float.valueOf("80.45")); + + students.add(student1); + + Student student2 = new Student(); + student2.setId(1002); + student2.setName("Jane Williams"); + student2.setGender('F'); + student2.setPercentage(Float.valueOf("60.25")); + + 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..f21169dbcf --- /dev/null +++ b/spring-thymeleaf/src/main/java/org/baeldung/thymeleaf/model/Student.java @@ -0,0 +1,60 @@ +package org.baeldung.thymeleaf.model; + +import java.io.Serializable; + +import javax.validation.constraints.Min; +import javax.validation.constraints.NotNull; + +/** + * + * Simple student POJO with few fields + * + */ +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 at least 4 digits.") + private Integer id; + + @NotNull(message = "Student name is required.") + private String name; + + @NotNull(message = "Student gender is required.") + private Character gender; + + private Float percentage; + + 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; + } + + public Character getGender() { + return gender; + } + + public void setGender(Character gender) { + this.gender = gender; + } + + public Float getPercentage() { + return percentage; + } + + public void setPercentage(Float percentage) { + this.percentage = percentage; + } +} 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..33295968e0 --- /dev/null +++ b/spring-thymeleaf/src/main/resources/messages_en_US.properties @@ -0,0 +1,9 @@ +msg.id=ID +msg.name=Name +msg.gender=Gender +msg.percent=Percentage +welcome.message=Welcome Student !!! +msg.AddStudent=Add Student +msg.ListStudents=List Students +msg.Home=Home + 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..ef42557429 --- /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..c5466def25 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/addStudent.html @@ -0,0 +1,43 @@ + + + +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..8adecc49f1 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/home.html @@ -0,0 +1,23 @@ + + + +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..c25de9eb17 --- /dev/null +++ b/spring-thymeleaf/src/main/webapp/WEB-INF/views/listStudents.html @@ -0,0 +1,36 @@ + + + +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