From a4639b70e8ea30ff1b85e0c91175d668737050db Mon Sep 17 00:00:00 2001 From: achraftt Date: Sun, 13 Feb 2022 12:17:53 +0100 Subject: [PATCH 1/2] BAEL-5039: Add a new section on using a datepicker to submit date values --- .../thymeleaf/controller/DatesController.java | 15 +++++++ .../com/baeldung/thymeleaf/model/Student.java | 14 +++++++ .../WEB-INF/views/datePicker/displayDate.html | 12 ++++++ .../WEB-INF/views/datePicker/saveStudent.html | 42 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/displayDate.html create mode 100644 spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html 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 @@ + + + + +Baeldung - using a datepicker to submit date + + +

Student birth date

+

+ + \ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html new file mode 100644 index 0000000000..2faaed173c --- /dev/null +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html @@ -0,0 +1,42 @@ + + + + +Baeldung - using a datepicker to submit date + + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ +
+
+
+ + \ No newline at end of file From adca84eca2fb787d80589188088ae31ff537ea77 Mon Sep 17 00:00:00 2001 From: achraftt Date: Wed, 16 Feb 2022 14:22:45 +0100 Subject: [PATCH 2/2] BAEL-5039: edit identation --- .../WEB-INF/views/datePicker/displayDate.html | 10 +-- .../WEB-INF/views/datePicker/saveStudent.html | 66 +++++++++---------- 2 files changed, 38 insertions(+), 38 deletions(-) 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 index d3409f98f3..7ae84e026f 100644 --- 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 @@ -1,12 +1,12 @@ + xmlns:th="http://www.thymeleaf.org"> - -Baeldung - using a datepicker to submit date + + Baeldung - using a datepicker to submit date -

Student birth date

-

+

Student birth date

+

\ No newline at end of file diff --git a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html index 2faaed173c..d897369326 100644 --- a/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html +++ b/spring-web-modules/spring-thymeleaf/src/main/webapp/WEB-INF/views/datePicker/saveStudent.html @@ -1,42 +1,42 @@ + xmlns:th="http://www.thymeleaf.org"> - -Baeldung - using a datepicker to submit date + + Baeldung - using a datepicker to submit date
-
-
- - -
-
-
- - -
-
-
- - -
-
-
- - -
-
-
- - -
-
-
- -
-
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ + +
+
+
+ +
+
\ No newline at end of file