首页>代码>基于jsp+servlet的idea版学生管理系统,前端框架layui>/studentManage/源码/studentManage/src/main/java/controller/StudentInfoServlet.java
01 | package controller; |
02 |
03 | import domain.Student; |
04 | import service.StudentService; |
05 | import service.impl.StudentServiceImpl; |
06 | import utils.MyBeanUtils; |
07 |
08 | import javax.servlet.annotation.WebServlet; |
09 | import javax.servlet.http.HttpServletRequest; |
10 | import javax.servlet.http.HttpServletResponse; |
11 | import java.util.List; |
12 | import java.util.Map; |
13 |
14 | @WebServlet (name = "studentInfoServlet" , urlPatterns = { "/student" }) |
15 | public class StudentInfoServlet extends BaseServlet { |
16 |
17 | private StudentService studentService = new StudentServiceImpl(); |
18 |
19 | public void home(HttpServletRequest request, HttpServletResponse response) throws Exception { |
20 | List<Student> studentList = studentService.queryAllStudent(); |
21 | System.out.println(studentList); |
22 | if (studentList != null ) { |
23 | request.setAttribute( "studentList" , studentList); |
24 | } else { |
25 | request.setAttribute( "msg" , "无数据" ); |
26 | } |
27 | request.getRequestDispatcher( "/pages/studentInfo/allInfo.jsp" ).forward(request, response); |
28 |
29 | } |
30 |
31 | public void insertInfo(HttpServletRequest request, HttpServletResponse response) throws Exception { |
32 | Student student = new Student(); |
33 | Map<String, String[]> map = request.getParameterMap(); |
34 | try { |
35 | MyBeanUtils.populate(student, map); |
36 | } catch (Exception e) { |
37 | e.printStackTrace(); |
38 | } |
39 | int row = studentService.insertStudentInfo(student); |
40 | if (row == 1 ) { |
41 | request.setAttribute( "stuMsg" , "添加成功" ); |
42 | } else { |
43 | request.setAttribute( "stuMsg" , "添加失败,id可能重复" ); |
44 | } |
45 | request.getRequestDispatcher( "/pages/studentInfo/insertInfo.jsp" ).forward(request, response); |
46 | } |
47 |
48 | public void delInfo(HttpServletRequest request, HttpServletResponse response) throws Exception { |
49 | String id=request.getParameter( "id" ); |
50 | int row = studentService.delStudentInfo(id); |
51 | if (row == 1 ) { |
52 | request.setAttribute( "stuMsg" , "删除成功" ); |
53 | } else { |
54 | request.setAttribute( "stuMsg" , "删除失败,可能无id" ); |
55 | } |
56 | request.getRequestDispatcher( "/pages/studentInfo/delInfo.jsp" ).forward(request, response); |
57 | } |
58 |
59 | public void findStudentInfoById(HttpServletRequest request, HttpServletResponse response) throws Exception { |
60 | String id=request.getParameter( "id" ); |
61 | Student student = studentService.findStudentInfoById(id); |
62 | if (student!= null ) { |
63 | request.setAttribute( "student" ,student); |
64 | } else { |
65 | request.setAttribute( "stuMsgError" , "查询失败,可能无id" ); |
66 | } |
67 | request.getRequestDispatcher( "/pages/studentInfo/updateInfo.jsp" ).forward(request, response); |
68 | } |
69 |
70 |
71 |
72 | public void updateStudentInfo(HttpServletRequest request, HttpServletResponse response) throws Exception { |
73 | Student student = new Student(); |
74 | Map<String, String[]> map = request.getParameterMap(); |
75 | try { |
76 | MyBeanUtils.populate(student, map); |
77 | } catch (Exception e) { |
78 | e.printStackTrace(); |
79 | } |
80 | System.out.println(student); |
81 | int row = studentService.updateStudentInfo(student); |
82 | if (row == 1 ) { |
83 | request.setAttribute( "stuMsg" , "修改成功" ); |
84 | } else { |
85 | request.setAttribute( "stuMsg" , "修改失败,可能修改后的id重复或者不存在" ); |
86 | } |
87 | request.getRequestDispatcher( "/pages/studentInfo/updateInfo.jsp" ).forward(request, response); |
88 | } |
89 | } |

ZzzxzzZ LV6
4月12日
zhaipengfei LV2
3月3日
ma406805131 LV19
2024年12月27日
120_wu LV4
2024年12月2日
李清清 LV2
2024年11月6日
xiao小果 LV13
2024年8月19日
破剑茶寮 LV4
2024年6月26日
15719908287 LV10
2024年6月19日
TY0165 LV20
2024年6月18日
微信网友_7024879466991616 LV2
2024年6月14日

李玟龙 LV2
6月16日
ZzzxzzZ LV6
4月12日
zhaipengfei LV2
3月3日
暂无贡献等级
kkkxyh LV13
2月11日
dongandmin LV9
2024年12月27日
ma406805131 LV19
2024年12月26日
120_wu LV4
2024年12月2日
haomc052829 LV4
2024年11月18日
李清清 LV2
2024年11月6日