diff --git a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java index 20f5d02fed..61443a3631 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java +++ b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/controller/DatesController.java @@ -5,8 +5,10 @@ import java.time.LocalDate; import java.time.LocalDateTime; import java.util.Date; +import com.baeldung.thymeleaf.model.Student; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @@ -22,4 +24,17 @@ public class DatesController { return "dates.html"; } + @RequestMapping(value = "/saveStudent", method = RequestMethod.GET) + public String displaySaveStudent(Model model) { + model.addAttribute("student", new Student()); + return "datePicker/saveStudent.html"; + } + + @RequestMapping(value = "/saveStudent", method = RequestMethod.POST) + public String saveStudent(Model model, @ModelAttribute("student") Student student) { + model.addAttribute("student", student); + + return "datePicker/displayDate.html"; + } + } diff --git a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java index 202c04358a..c08985fd61 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java +++ b/spring-web-modules/spring-thymeleaf/src/main/java/com/baeldung/thymeleaf/model/Student.java @@ -1,6 +1,9 @@ package com.baeldung.thymeleaf.model; +import org.springframework.format.annotation.DateTimeFormat; + import java.io.Serializable; +import java.util.Date; import javax.validation.constraints.Min; import javax.validation.constraints.NotNull; @@ -24,6 +27,9 @@ public class Student implements Serializable { @NotNull(message = "Student gender is required.") private Character gender; + @DateTimeFormat(pattern = "yyyy-MM-dd") + private Date birthDate; + private Float percentage; public Integer getId() { @@ -50,6 +56,14 @@ public class Student implements Serializable { this.gender = gender; } + public Date getBirthDate() { + return birthDate; + } + + public void setBirthDate(Date birthDate) { + this.birthDate = birthDate; + } + public Float getPercentage() { return percentage; } diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/displayDate.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/displayDate.html new file mode 100644 index 0000000000..d3409f98f3 --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/displayDate.html @@ -0,0 +1,12 @@ + + +
+ +