|
1 | 1 | package com.baeldung.thymeleaf.controller; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | 3 | import java.util.List; |
5 | 4 |
|
6 | 5 | import javax.validation.Valid; |
7 | 6 |
|
8 | | -import com.baeldung.thymeleaf.model.Student; |
9 | 7 | import org.springframework.stereotype.Controller; |
10 | 8 | import org.springframework.ui.Model; |
11 | 9 | import org.springframework.validation.BindingResult; |
12 | 10 | import org.springframework.web.bind.annotation.ModelAttribute; |
13 | 11 | import org.springframework.web.bind.annotation.RequestMapping; |
14 | 12 | import org.springframework.web.bind.annotation.RequestMethod; |
15 | 13 |
|
| 14 | +import com.baeldung.thymeleaf.model.Student; |
| 15 | +import com.baeldung.thymeleaf.utils.StudentUtils; |
| 16 | + |
16 | 17 | /** |
17 | 18 | * Handles requests for the student model. |
18 | 19 | * |
19 | 20 | */ |
20 | 21 | @Controller |
21 | 22 | public class StudentController { |
22 | 23 |
|
23 | | - @RequestMapping(value = "/saveStudent", method = RequestMethod.POST) |
24 | | - public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) { |
25 | | - if (!errors.hasErrors()) { |
26 | | - // get mock objects |
27 | | - List<Student> students = buildStudents(); |
28 | | - // add current student |
29 | | - students.add(student); |
30 | | - model.addAttribute("students", students); |
31 | | - } |
32 | | - return ((errors.hasErrors()) ? "addStudent" : "listStudents"); |
33 | | - } |
34 | | - |
35 | | - @RequestMapping(value = "/addStudent", method = RequestMethod.GET) |
36 | | - public String addStudent(Model model) { |
37 | | - model.addAttribute("student", new Student()); |
38 | | - return "addStudent"; |
39 | | - } |
40 | | - |
41 | | - @RequestMapping(value = "/listStudents", method = RequestMethod.GET) |
42 | | - public String listStudent(Model model) { |
43 | | - |
44 | | - model.addAttribute("students", buildStudents()); |
45 | | - |
46 | | - return "listStudents"; |
47 | | - } |
| 24 | + @RequestMapping(value = "/saveStudent", method = RequestMethod.POST) |
| 25 | + public String saveStudent(@Valid @ModelAttribute Student student, BindingResult errors, Model model) { |
| 26 | + if (!errors.hasErrors()) { |
| 27 | + // get mock objects |
| 28 | + List<Student> students = StudentUtils.buildStudents(); |
| 29 | + // add current student |
| 30 | + students.add(student); |
| 31 | + model.addAttribute("students", students); |
| 32 | + } |
| 33 | + return ((errors.hasErrors()) ? "addStudent.html" : "listStudents.html"); |
| 34 | + } |
48 | 35 |
|
49 | | - private List<Student> buildStudents() { |
50 | | - List<Student> students = new ArrayList<Student>(); |
| 36 | + @RequestMapping(value = "/addStudent", method = RequestMethod.GET) |
| 37 | + public String addStudent(Model model) { |
| 38 | + model.addAttribute("student", new Student()); |
| 39 | + return "addStudent.html"; |
| 40 | + } |
51 | 41 |
|
52 | | - Student student1 = new Student(); |
53 | | - student1.setId(1001); |
54 | | - student1.setName("John Smith"); |
55 | | - student1.setGender('M'); |
56 | | - student1.setPercentage(Float.valueOf("80.45")); |
| 42 | + @RequestMapping(value = "/listStudents", method = RequestMethod.GET) |
| 43 | + public String listStudent(Model model) { |
57 | 44 |
|
58 | | - students.add(student1); |
| 45 | + model.addAttribute("students", StudentUtils.buildStudents()); |
59 | 46 |
|
60 | | - Student student2 = new Student(); |
61 | | - student2.setId(1002); |
62 | | - student2.setName("Jane Williams"); |
63 | | - student2.setGender('F'); |
64 | | - student2.setPercentage(Float.valueOf("60.25")); |
| 47 | + return "listStudents.html"; |
| 48 | + } |
65 | 49 |
|
66 | | - students.add(student2); |
67 | | - return students; |
68 | | - } |
69 | 50 | } |
0 commit comments