-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentController.java
More file actions
85 lines (53 loc) · 2.09 KB
/
StudentController.java
File metadata and controls
85 lines (53 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.StudentManagementSystem.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import com.StudentManagementSystem.Entity.Student;
import com.StudentManagementSystem.Service.StudentService;
@org.springframework.stereotype.Controller
public class StudentController {
@Autowired
StudentService studentService;
@GetMapping("/home")
public String home() {
return "home";
}
@GetMapping("/students")
public String getAllStudents(Model model) {
model.addAttribute("Students", studentService.getAllStudents());
return "Students";
}
@GetMapping("/student/new")
public String createStudentFrom(Model model) {
Student student = new Student();
model.addAttribute("Students", student);
return "createstudent";
}
@PostMapping("/students")
public String saveStudent(@ModelAttribute("student")Student student) {
studentService.saveStudent(student);
return "redirect:/students";
}
@GetMapping("/students/update/{id}")
public String editStudentForm(@PathVariable int id, Model model) {
model.addAttribute("student", studentService.getById(id));
return "updatestudent";
}
@PostMapping("/students/update/{id}")
public String updateStudent(@PathVariable int id, @ModelAttribute("student") Student student) {
Student existingStudent = studentService.getById(id);
existingStudent.setFirstname(student.getFirstname());
existingStudent.setLastname(student.getLastname());
existingStudent.setEmail(student.getEmail());
studentService.saveStudent(existingStudent);
return "redirect:/students";
}
@GetMapping("/students/{id}")
public String deleteById(@PathVariable int id) {
studentService.deleteById(id);
return "redirect:/students";
}
}