-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentController.java
61 lines (51 loc) · 1.96 KB
/
StudentController.java
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
package com.Student.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import com.Student.Model.Student;
import com.Student.Service.StudentService;
import java.util.List;
@Controller
public class StudentController {
@Autowired
private StudentService studentService;
@GetMapping("/add")
public String addStudent(Model model) {
Student student = new Student();
model.addAttribute("student", student);
return "addStudent";
}
@PostMapping("/save")
public String saveStudent(@ModelAttribute Student student) {
studentService.saveStudent(student);
return "redirect:/";
}
@GetMapping("/update/{id}")
public String showUpdateForm(@PathVariable("id") Long id, Model model) {
Student student = studentService.getStudentById(id);
model.addAttribute("student", student);
return "updateStudent";
}
@PostMapping("/update/{id}")
public String updateStudentById(@PathVariable("id") Long id, @ModelAttribute("student") Student student) {
Student existingStudent = studentService.getStudentById(id);
existingStudent.setName(student.getName());
existingStudent.setGender(student.getGender());
existingStudent.setDepartment(student.getDepartment());
studentService.saveStudent(existingStudent);
return "redirect:/";
}
//
@GetMapping("/delete/{id}")
public String deleteStudent(@PathVariable("id") Long id) {
studentService.deleteStudentById(id);
return "redirect:/";
}
@GetMapping("/")
public String viewHomePage(Model model) {
List<Student> studentList = studentService.getAllStudents();
model.addAttribute("studentList", studentList);
return "homePage";
}
}