Merge pull request #11813 from achraftt/BAEL-5039

BAEL-5039: Add a new section on using a datepicker to submit date values
This commit is contained in:
Greg Martin 2022-02-27 10:20:24 -05:00 committed by GitHub
commit b91ca46c23
4 changed files with 83 additions and 0 deletions

View File

@ -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";
}
}

View File

@ -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;
}

View File

@ -0,0 +1,12 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Baeldung - using a datepicker to submit date</title>
</head>
<body>
<h1>Student birth date</h1>
<p th:text="${#dates.format(student.birthDate, 'dd/MM/yyyy')}"></p>
</body>
</html>

View File

@ -0,0 +1,42 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Baeldung - using a datepicker to submit date</title>
</head>
<body>
<form th:action="@{/saveStudent}" method="post" th:object="${student}">
<div>
<div>
<label for="student-id">ID:</label>
<input type="number" th:field="${student.id}" id="student-id"/>
</div>
</br>
<div>
<label for="student-name">Name:</label>
<input type="text" th:field="${student.name}" id="student-name"/>
</div>
</br>
<div>
<label for="student-gender">Gender:</label>
<input type="text" th:field="${student.gender}" id="student-gender"/>
</div>
</br>
<div>
<label for="student-birth-date">Date of birth:</label>
<input type="date" th:field="${student.birthDate}" id="student-birth-date"/>
</div>
</br>
<div>
<label for="student-percentage">Percentage:</label>
<input type="number" th:field="${student.percentage}" id="student-percentage"/>
</div>
</br>
<div>
<button type="submit" class="button">Submit</button>
</div>
</div>
</form>
</body>
</html>