From bf3e198ed2245f89136f0a28bb25bfc79dcdd88e Mon Sep 17 00:00:00 2001 From: Chenx221 Date: Mon, 19 Jun 2023 20:22:42 +0800 Subject: [PATCH] fix part3 Signed-off-by: Chenx221 --- project2/README.md | 47 ++-- .../controller/StudentController.java | 141 ++++++------ .../java/cyou/chenx221/mapper/StudentDao.java | 4 + .../chenx221/mapper/impl/StudentDaoImpl.java | 10 + .../cyou/chenx221/service/StudentService.java | 8 + .../main/resources/mapper/StudentMapper.xml | 23 +- .../views/{ => student}/studentList.jsp | 0 .../views/{ => student}/studentListQuery.jsp | 0 .../{ => student}/studentListQuery_t.jsp | 0 .../WEB-INF/views/student/studentList_s.jsp | 201 ++++++++++++++++++ .../views/{ => student}/studentList_t.jsp | 0 .../views/{ => student}/studentMod.jsp | 0 .../views/{ => student}/studentMod_t.jsp | 2 +- .../{ => student}/studentQueryResult.jsp | 0 .../{ => student}/studentQueryResult_t.jsp | 0 .../views/{ => student}/studentinfomodv2.jsp | 0 .../{ => student}/studentinfomodv2_t.jsp | 0 17 files changed, 330 insertions(+), 106 deletions(-) rename project2/web/WEB-INF/views/{ => student}/studentList.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentListQuery.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentListQuery_t.jsp (100%) create mode 100644 project2/web/WEB-INF/views/student/studentList_s.jsp rename project2/web/WEB-INF/views/{ => student}/studentList_t.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentMod.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentMod_t.jsp (99%) rename project2/web/WEB-INF/views/{ => student}/studentQueryResult.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentQueryResult_t.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentinfomodv2.jsp (100%) rename project2/web/WEB-INF/views/{ => student}/studentinfomodv2_t.jsp (100%) diff --git a/project2/README.md b/project2/README.md index b71b806..0f38656 100644 --- a/project2/README.md +++ b/project2/README.md @@ -70,40 +70,35 @@ - *Redirect*.infomodv2|*Redirect*.infomodv2_t - /student - ~/all - - 学生信息查询页 - - admin - - ~/all_t - - 学生信息查询页(教师版) - - teacher + - 学生信息查询页 [Get] + - admin,teacher,student + - studentList|studentList_t|studentList_s - ~/queryinfo - - 学生信息查询(条件)页 - - admin - - ~/queryinfo_t - - 学生信息查询(条件)页(教师版) - - teacher + - 学生信息查询(条件)页 [Get] + - admin,teacher + - studentListQuery|studentListQuery_t - ~/query - - 学生信息查询 - - admin + - 学生信息查询 [Post] + - admin,teacher + - studentQueryResult|studentQueryResult_t - ~/modstep1 - - 学生信息修改页 - - admin - - ~/modinfo + - 学生信息修改页(过时) [Get] + - admin,teacher + - studentMod|studentMod_t + - ~/modinfo [Post] - 学生信息修改 - admin,teacher + - *Redirect*.infomodv2|*Redirect*.infomodv2_t - ~/infomodv2 - - 学生信息修改页(v2) - - admin - - ~/infomodv2_t - - 学生信息修改页(v2)(教师版) - - teacher + - 学生信息修改页(v2) [Get] + - admin,teacher + - studentinfomodv2|studentinfomodv2_t - ~/delinfo - - 学生信息删除 - - admin - - ~/delinfoT - - 学生信息删除(教师版) - - teacher + - 学生信息删除 [Post] + - admin,teacher + - *Redirect*.infomodv2|*Redirect*.infomodv2_t - ~/addinfo - - 学生信息添加 + - 学生信息添加 [Post] - admin,teacher - /system - ~/settings diff --git a/project2/src/main/java/cyou/chenx221/controller/StudentController.java b/project2/src/main/java/cyou/chenx221/controller/StudentController.java index 6045a33..3b16543 100644 --- a/project2/src/main/java/cyou/chenx221/controller/StudentController.java +++ b/project2/src/main/java/cyou/chenx221/controller/StudentController.java @@ -37,22 +37,23 @@ public class StudentController { if (username != null) { model.addAttribute("username", username); } - List students = studentService.getAllStudents(); - model.addAttribute("students", students); - return "studentList"; // 重定向到 dashboard 页面 - } - - @GetMapping("/all_t")//学生信息查询(不带条件)(排除removed的学生) - public String getAllStudentsT(Model model) { - String username = new UsernameHelper().getCurrentUsername(); - if (username != null) { - model.addAttribute("username", username); + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) { + List students = studentService.getAllStudents(); + model.addAttribute("students", students); + return "student/studentList"; + } else if (role.equals("[ROLE_teacher]")) { + String classes = teacherService.getClassesById(userMapper.getDetailByUsername(username)); + List students = studentService.getAllStudentsT(classes); + model.addAttribute("classes", classes); + model.addAttribute("students", students); + return "student/studentList_t"; + } else { + int id = studentService.getIdByUsername(username); + Student student = studentService.getStudentById(id); + model.addAttribute("student", student); + return "student/studentList_s"; } - String classes = teacherService.getClassesById(userMapper.getDetailByUsername(username)); - List students = studentService.getAllStudentsT(classes); - model.addAttribute("classes", classes); - model.addAttribute("students", students); - return "studentList_t"; } @GetMapping("/queryinfo")//处理访问学生信息查询 @@ -61,17 +62,13 @@ public class StudentController { if (username != null) { model.addAttribute("username", username); } - return "studentListQuery"; - } - - @GetMapping("/queryinfo_t")//处理访问学生信息查询 - public String getQueryInfoT(Model model) { - String username = new UsernameHelper().getCurrentUsername(); - if (username != null) { - model.addAttribute("username", username); + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) + return "student/studentListQuery"; + else { + model.addAttribute("classes", teacherService.getClassesById(userMapper.getDetailByUsername(username))); + return "student/studentListQuery_t"; } - model.addAttribute("classes", teacherService.getClassesById(userMapper.getDetailByUsername(username))); - return "studentListQuery_t"; } @PostMapping(value = "/query", produces = "application/x-www-form-urlencoded;charset=UTF-8")//学生信息查询(带条件) @@ -118,7 +115,12 @@ public class StudentController { students = studentService.getQueryStudents2(student); } model.addAttribute("students", students); - return "studentQueryResult"; + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) + return "student/studentQueryResult"; + else { + return "student/studentQueryResult_t"; + } } @GetMapping("/modstep1")//处理学生信息修改页 @@ -134,7 +136,13 @@ public class StudentController { // 将消息添加到Model中 model.addAttribute("successMessage", successMessage); model.addAttribute("errorMessage", errorMessage); - return "studentMod"; + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) + return "student/studentMod"; + else { + model.addAttribute("classes", teacherService.getClassesById(userMapper.getDetailByUsername(username))); + return "student/studentMod_t"; + } } @PostMapping(value = "/modinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8")//处理学生信息修改请求 @@ -144,7 +152,6 @@ public class StudentController { @RequestParam(value = "id", defaultValue = "-1", required = false) int id, @RequestParam(value = "phone", defaultValue = "null", required = false) String phone, @RequestParam(value = "classes", defaultValue = "", required = false) String classes, - @RequestParam(value = "cla", defaultValue = "null", required = false) String cla, Model model) { Date birthday = null; if (!"null".equals(birthdayStr)) { @@ -164,9 +171,10 @@ public class StudentController { studentService.updateStudent(student); model.addAttribute("successMessage", "学生信息修改成功"); } - if (cla.equals("null")) { + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) return "redirect:/student/infomodv2"; - } else { + else { return "redirect:/student/infomodv2_t"; } } @@ -179,64 +187,49 @@ public class StudentController { if (username != null) { model.addAttribute("username", username); } - List students = studentService.getAllStudents();//和前面显示全部学生一样,页面需要学生信息的显示 - model.addAttribute("students", students); -// 将消息添加到Model中 - model.addAttribute("successMessage", successMessage); - model.addAttribute("errorMessage", errorMessage); - return "studentinfomodv2"; - } - @GetMapping("/infomodv2_t")//处理学生信息修改页 - public String getInfoModv2T(@RequestParam(value = "successMessage", required = false, defaultValue = "null") String successMessage, - @RequestParam(value = "errorMessage", required = false, defaultValue = "null") String errorMessage, - Model model) { - String username = new UsernameHelper().getCurrentUsername(); - if (username != null) { - model.addAttribute("username", username); - } - String classes = teacherService.getClassesById(userMapper.getDetailByUsername(username)); - List students = studentService.getAllStudentsT(classes); - model.addAttribute("classes", classes); - model.addAttribute("students", students); -// 将消息添加到Model中 model.addAttribute("successMessage", successMessage); model.addAttribute("errorMessage", errorMessage); - return "studentinfomodv2_t"; + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) { + List students = studentService.getAllStudents(); + model.addAttribute("students", students); + return "student/studentinfomodv2"; + } else { + String classes = teacherService.getClassesById(userMapper.getDetailByUsername(username)); + List students = studentService.getAllStudentsT(classes); + model.addAttribute("classes", classes); + model.addAttribute("students", students); + return "student/studentinfomodv2_t"; + } } @PostMapping(value = "/delinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8") public String UpdateModInfo(@RequestParam(value = "id") int id, + @RequestParam(value = "cla", required = false, defaultValue = "null") String classes, Model model) { Student temp = studentService.getStudentById(id); if (id <= 0 || temp == null) { model.addAttribute("errorMessage", "学生信息删除失败,可能是ID非法或者学生已删除"); } else { -// Student student = new Student(id); - studentService.deleteStudent(id); - model.addAttribute("successMessage", "学生信息删除成功"); - } - return "redirect:/student/infomodv2"; - } - - - @PostMapping(value = "/delinfoT", produces = "application/x-www-form-urlencoded;charset=UTF-8") - public String UpdateModInfoT(@RequestParam(value = "id") int id, - @RequestParam(value = "cla") String classes, - Model model) { - Student temp = studentService.getStudentById(id); - if (id <= 0 || temp == null) { - model.addAttribute("errorMessage", "学生信息删除失败,可能是ID非法或者学生已删除"); - } else { - if (temp.getClasses().equals(classes)) { + if (classes.equals("null")) { studentService.deleteStudent(id); model.addAttribute("successMessage", "学生信息删除成功"); } else { - model.addAttribute("errorMessage", "学生信息删除失败,这名学生不属于您的班级"); + if (temp.getClasses().equals(classes)) { + studentService.deleteStudent(id); + model.addAttribute("successMessage", "学生信息删除成功"); + } else { + model.addAttribute("errorMessage", "学生信息删除失败,这名学生不属于您的班级"); + } } - } - return "redirect:/student/infomodv2_t"; + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) + return "redirect:/student/infomodv2"; + else { + return "redirect:/student/infomodv2_t"; + } } @PostMapping(value = "/addinfo", produces = "application/x-www-form-urlencoded;charset=UTF-8") @@ -245,7 +238,6 @@ public class StudentController { @RequestParam(value = "sex") String sex, @RequestParam(value = "phone") String phone, @RequestParam(value = "classes") String classes, - @RequestParam(value = "spec" ,defaultValue = "null", required = false) String spec, Model model) { Student student; String classesPattern = "[1-5]班";// 使用正则表达式验证班级格式 @@ -260,9 +252,10 @@ public class StudentController { studentService.insertStudent(student); model.addAttribute("successMessage", "学生信息添加成功"); } - if(spec.equals("null")){ + String role = new UsernameHelper().getCurrentRole(); + if (role.equals("[ROLE_admin]")) return "redirect:/student/infomodv2"; - }else { + else { return "redirect:/student/infomodv2_t"; } } diff --git a/project2/src/main/java/cyou/chenx221/mapper/StudentDao.java b/project2/src/main/java/cyou/chenx221/mapper/StudentDao.java index bab4a61..729a929 100644 --- a/project2/src/main/java/cyou/chenx221/mapper/StudentDao.java +++ b/project2/src/main/java/cyou/chenx221/mapper/StudentDao.java @@ -17,4 +17,8 @@ public interface StudentDao { void updateStudent(Student student); List getAllStudentsT(String classes); + + int getIdByUsername(String username); + + int getDetailByUsername(String username); } diff --git a/project2/src/main/java/cyou/chenx221/mapper/impl/StudentDaoImpl.java b/project2/src/main/java/cyou/chenx221/mapper/impl/StudentDaoImpl.java index bfde486..b485996 100644 --- a/project2/src/main/java/cyou/chenx221/mapper/impl/StudentDaoImpl.java +++ b/project2/src/main/java/cyou/chenx221/mapper/impl/StudentDaoImpl.java @@ -32,6 +32,16 @@ public class StudentDaoImpl implements StudentDao { return sqlSession.selectList("getAllStudentsT", classes); } + @Override + public int getIdByUsername(String username) { + return sqlSession.selectOne("getIdByUsername", username); + } + + @Override + public int getDetailByUsername(String username) { + return sqlSession.selectOne("getDetailByUsername", username); + } + @Override public void deleteStudent(int id) { sqlSession.update("deleteStudent", id); diff --git a/project2/src/main/java/cyou/chenx221/service/StudentService.java b/project2/src/main/java/cyou/chenx221/service/StudentService.java index 6dd34ca..6e0e94e 100644 --- a/project2/src/main/java/cyou/chenx221/service/StudentService.java +++ b/project2/src/main/java/cyou/chenx221/service/StudentService.java @@ -49,4 +49,12 @@ public class StudentService { public List getAllStudentsT(String classes) { return studentDao.getAllStudentsT(classes); } + + public int getIdByUsername(String username) { + return studentDao.getIdByUsername(username); + } + + public int getDetailByUsername(String username) { + return studentDao.getDetailByUsername(username); + } } diff --git a/project2/src/main/resources/mapper/StudentMapper.xml b/project2/src/main/resources/mapper/StudentMapper.xml index 9c67cf2..f6a1813 100644 --- a/project2/src/main/resources/mapper/StudentMapper.xml +++ b/project2/src/main/resources/mapper/StudentMapper.xml @@ -70,9 +70,22 @@ VALUES (#{name}, #{birthday}, #{sex}, #{phone}, #{classes}) - + + + + + diff --git a/project2/web/WEB-INF/views/studentList.jsp b/project2/web/WEB-INF/views/student/studentList.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentList.jsp rename to project2/web/WEB-INF/views/student/studentList.jsp diff --git a/project2/web/WEB-INF/views/studentListQuery.jsp b/project2/web/WEB-INF/views/student/studentListQuery.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentListQuery.jsp rename to project2/web/WEB-INF/views/student/studentListQuery.jsp diff --git a/project2/web/WEB-INF/views/studentListQuery_t.jsp b/project2/web/WEB-INF/views/student/studentListQuery_t.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentListQuery_t.jsp rename to project2/web/WEB-INF/views/student/studentListQuery_t.jsp diff --git a/project2/web/WEB-INF/views/student/studentList_s.jsp b/project2/web/WEB-INF/views/student/studentList_s.jsp new file mode 100644 index 0000000..382e32e --- /dev/null +++ b/project2/web/WEB-INF/views/student/studentList_s.jsp @@ -0,0 +1,201 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %> + + + + + + + + 学生信息查看 + + + + + + + + + + +
+ +
+ + + + +
+
+
+
+
+
+
+ +
+
+
+
+ +
+
+
+
+ + + + + + + + + + + + + + + + + + + + + +
ID姓名性别出生日期联系方式班级
${student.id}${student.name}${student.sex}${student.birthday}${student.phone}${student.classes}
+
+
+
+
+
+ + + + +
+
+ + + + + + + + + \ No newline at end of file diff --git a/project2/web/WEB-INF/views/studentList_t.jsp b/project2/web/WEB-INF/views/student/studentList_t.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentList_t.jsp rename to project2/web/WEB-INF/views/student/studentList_t.jsp diff --git a/project2/web/WEB-INF/views/studentMod.jsp b/project2/web/WEB-INF/views/student/studentMod.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentMod.jsp rename to project2/web/WEB-INF/views/student/studentMod.jsp diff --git a/project2/web/WEB-INF/views/studentMod_t.jsp b/project2/web/WEB-INF/views/student/studentMod_t.jsp similarity index 99% rename from project2/web/WEB-INF/views/studentMod_t.jsp rename to project2/web/WEB-INF/views/student/studentMod_t.jsp index 341cca5..0cb176f 100644 --- a/project2/web/WEB-INF/views/studentMod_t.jsp +++ b/project2/web/WEB-INF/views/student/studentMod_t.jsp @@ -206,7 +206,7 @@
+ name="classes" value="${classes}"/>
diff --git a/project2/web/WEB-INF/views/studentQueryResult.jsp b/project2/web/WEB-INF/views/student/studentQueryResult.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentQueryResult.jsp rename to project2/web/WEB-INF/views/student/studentQueryResult.jsp diff --git a/project2/web/WEB-INF/views/studentQueryResult_t.jsp b/project2/web/WEB-INF/views/student/studentQueryResult_t.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentQueryResult_t.jsp rename to project2/web/WEB-INF/views/student/studentQueryResult_t.jsp diff --git a/project2/web/WEB-INF/views/studentinfomodv2.jsp b/project2/web/WEB-INF/views/student/studentinfomodv2.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentinfomodv2.jsp rename to project2/web/WEB-INF/views/student/studentinfomodv2.jsp diff --git a/project2/web/WEB-INF/views/studentinfomodv2_t.jsp b/project2/web/WEB-INF/views/student/studentinfomodv2_t.jsp similarity index 100% rename from project2/web/WEB-INF/views/studentinfomodv2_t.jsp rename to project2/web/WEB-INF/views/student/studentinfomodv2_t.jsp