package controller; import model.ClazzEntity; import model.StudentEntity; import model.TeacherEntity; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import repository.ClazzRepository; import repository.StudentsRepository; import repository.TeacherRepository; import java.util.List; @Controller public class MainController { @Autowired StudentsRepository studentsRepository; @Autowired TeacherRepository teacherRepository; @Autowired ClazzRepository clazzRepository; @RequestMapping(value = "/", method = RequestMethod.GET) public String index() { return "index"; } @RequestMapping(value = "/admin/students", method = RequestMethod.GET) public String getStudents(ModelMap modelMap) { List<StudentEntity> studentList= studentsRepository.findAll(); modelMap.addAttribute("studentList", studentList); return "admin/students"; } @RequestMapping(value = "/admin/students/add", method = RequestMethod.GET) public String addStudent() { return "admin/addStudent"; } @RequestMapping(value = "/admin/students/addS", method = RequestMethod.POST) public String addStudentPost(@ModelAttribute("student") StudentEntity studentEntity) { studentsRepository.saveAndFlush(studentEntity); return "redirect:/admin/students"; } @RequestMapping(value = "/admin/students/show/{sid}", method = RequestMethod.GET) public String showStudent(@PathVariable("sid") Integer sid, ModelMap modelMap) { StudentEntity studentEntity = studentsRepository.findOne(sid); modelMap.addAttribute("student",studentEntity); return "admin/studentDetail"; } @RequestMapping(value = "/admin/students/delete/{sid}", method = RequestMethod.GET) public String deleteStudent(@PathVariable("sid") Integer sid) { studentsRepository.delete(sid); studentsRepository.flush(); return "redirect:/admin/students"; } @RequestMapping(value = "/admin/students/update/{sid}", method = RequestMethod.GET) public String updateStudent(@PathVariable("sid") Integer sid, ModelMap modelMap) { StudentEntity studentEntity = studentsRepository.findOne(sid); modelMap.addAttribute("student", studentEntity); return "admin/updateStudent"; } @RequestMapping(value = "/admin/students/updateP", method = RequestMethod.POST) public String updateStudentPost(@ModelAttribute("studentP") StudentEntity studentEntity) { studentsRepository.updateStudent( studentEntity.getSname(), studentEntity.getSsex(), studentEntity.getSbirthday(), studentEntity.getSphone(), studentEntity.getSid() ); studentsRepository.flush(); return "redirect:/admin/students"; } @RequestMapping(value = "/admin/teachers", method = RequestMethod.GET) public String getTeachers(ModelMap modelMap) { List<TeacherEntity> teacherList = teacherRepository.findAll(); modelMap.addAttribute("teacherList", teacherList); return "admin/teachers"; } @RequestMapping(value="/admin/clazzs",method = RequestMethod.GET) public String getClazzs(ModelMap modelMap){ List<ClazzEntity> clazzList = clazzRepository.findAll(); modelMap.addAttribute("clazzList",clazzList); return "admin/clazzs"; } @RequestMapping(value = "/admin/teachers/add", method = RequestMethod.GET) public String addTeacher() { return "admin/addTeacher"; } @RequestMapping(value = "/admin/teachers/addT", method = RequestMethod.POST) public String addTeacherPost(@ModelAttribute("teacher") TeacherEntity teacherEntity) { teacherRepository.saveAndFlush(teacherEntity); return "redirect:/admin/teachers"; } @RequestMapping(value = "/admin/teachers/show/{tid}", method = RequestMethod.GET) public String showTeacher(@PathVariable("tid") Integer tid, ModelMap modelMap) { TeacherEntity teacherEntity = teacherRepository.findOne(tid); modelMap.addAttribute("teacher",teacherEntity); return "admin/teacherDetail"; } @RequestMapping(value = "/admin/teachers/delete/{tid}", method = RequestMethod.GET) public String deleteTeacher(@PathVariable("tid") Integer tid) { teacherRepository.delete(tid); teacherRepository.flush(); return "redirect:/admin/teachers"; } @RequestMapping(value = "/admin/teachers/update/{tid}", method = RequestMethod.GET) public String updateTeacher(@PathVariable("tid") Integer tid, ModelMap modelMap) { TeacherEntity teacherEntity = teacherRepository.findOne(tid); modelMap.addAttribute("teacher", teacherEntity); return "admin/updateTeacher"; } @RequestMapping(value = "/admin/teachers/update", method = RequestMethod.POST) public String updateTeacherPost(@ModelAttribute("teacher") TeacherEntity teacherEntity) { teacherRepository.updateTeacher( teacherEntity.getTname(), teacherEntity.getTsex(), teacherEntity.getTbirthday(), teacherEntity.getTphone(), teacherEntity.getTfield(), teacherEntity.getTqualificationLevel(), teacherEntity.getTemail(), teacherEntity.getTqq(), teacherEntity.getTmsn(), teacherEntity.getTid()); teacherRepository.flush(); return "redirect:/admin/teachers"; } @RequestMapping(value = "/admin/clazzs/show/{cid}", method = RequestMethod.GET) public String showClazz(@PathVariable("cid") Integer cid, ModelMap modelMap) { ClazzEntity clazzEntity = clazzRepository.findOne(cid); modelMap.addAttribute("clazz",clazzEntity); return "admin/clazzDetail"; } @RequestMapping(value = "/admin/clazzs/update/{cid}", method = RequestMethod.GET) public String updateClazz(@PathVariable("cid") Integer cid, ModelMap modelMap) { ClazzEntity clazzEntity = clazzRepository.findOne(cid); modelMap.addAttribute("clazz",clazzEntity); return "admin/updateClazz"; } @RequestMapping(value = "/admin/clazzs/update", method = RequestMethod.POST) public String updateClazzPost(@ModelAttribute("clazz") ClazzEntity clazzEntity) { clazzRepository.updateClazz( clazzEntity.getCname(), clazzEntity.getCyear(), clazzEntity.getCid() ); clazzRepository.flush(); return "redirect:/admin/clazzs"; } @RequestMapping(value = "/admin/clazzs/delete/{cid}", method = RequestMethod.GET) public String deleteClazz(@PathVariable("cid") Integer cid) { clazzRepository.delete(cid); clazzRepository.flush(); return "redirect:/admin/clazzs"; } @RequestMapping(value = "/admin/clazzs/add", method = RequestMethod.GET) public String addClazz() { return "admin/addClazz"; } @RequestMapping(value = "/admin/clazzs/add", method = RequestMethod.POST) public String addClazzPost(@ModelAttribute("clazz") ClazzEntity clazzEntity) { clazzRepository.saveAndFlush(clazzEntity); return "redirect:/admin/clazzs"; } }