• SpringBoot + Servlet + Mybatis+ layui 学生选课管理系统


    课程设计目的

    本次课设做的系统为学生课程设计管理系统,鉴于学校规模的不断扩大,而且随着每年的扩招,人数不断的增加,每次课程设计都采用手工操作,费时费力。为了解决这个问题,决定做一个课程设计管理系统,对每个学期选修课程设计的学生基本情况作个统计。学生可以在系统中注册账号,登录后学生从该系统中选题,填写同组学生的姓名,组长等基本情况;查询自己的课程设计成绩,课程设计的选择情况,成绩的合格情况,小组成员信息,上课程设计报告的上传,下载,删除,预览等功能。老师通过该系统添加课设信息,查看学生的选题情况,给出学生的分数。这有利于老师教学,及时了解学生的情况,提高教学质量,减轻老师的工作量,改善原有的比较繁锁的工作。

    题目基本情况

    本系统涉及权限管理,以老师,学生分别登录以进行不同的操作,老师可以对查看学生信息,给出学生成绩查看学生选题情况,添加课设信息。学生可以添加同组成员信息选择课程设计,退掉课程设计,查看课程设计的成绩,上传实验报告.为了提高信息的处理效率,设计一款小型的课程设计管理系统。将数据库的内容通过web服务器配合前端框架在浏览器展现出来,达到直观,方便的管理目的。

    数据库表设计

    教师属性:教师号,姓名,职称,密码。

    学生属性:学号,姓名,班级,密码。

    题目属性:课题号,课题目名称,题目信息,教工号。

    选题属性:学号,题号,成绩,组号。

    小组属性:组号,组员,组长。

    文件属性:文件号,文件名,后缀,路径大小,类型,下载次数,上传时间,学号

    技术栈

    数据库连接池技术  maven工具    servlet技术

    Ajax请求  Spring-Boot框架   Mybatis框架    JSON数据格式                                                         

     commons-fileupload组件  layui框架  

    程序流程图

     

     

    5.3.1登录操作

    本程序设计的权限管理中,不同身份可以进行的操作不一样,因此不同的身份,决定执行什么样的操作,如图6-1所示为教师登录验证界面,前端使用leyui框架布局,

    验证码使用BufferedImage类绘制,用户输入信息后进行后台的逻辑判断,成功则进入到后台管理界面。若用户名和姓名和验证码输入正确,则跳转到对应的请求映射,若勾选记住密码,则会在信息全部正确匹配后将正确的信息存入cookie,若其中一项输入错误,则会重新定向到登录界面。

    图6-1教师登录界面

    图6-2学生登录界面

    登录部分代码:

    1. Dao层:
    2.  @Select("SELECT * from student where stdname =#{stdname} and stdpassword =#{stdpassword}")
    3. public Student findstudentByNameAndPassword(Student student);//通过用户的姓名和密码查询
    4. Controller层:
    5. @RequestMapping("/tologin")
    6.     public String index() {
    7.         return "login";}
    8.  //登录1
    9.     @RequestMapping(value = "/login")
    10.     public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response, Student student) throws Exception {
    11.         String name = request.getParameter("stdname");
    12.         String pwd = request.getParameter("stdpassword");
    13.         String yzm = request.getParameter("captcha");
    14.         String shenfen = request.getParameter("shenfen");
    15.         HttpSession session = request.getSession();
    16.         String code = (String) session.getAttribute("code");
    17.         String rember = request.getParameter("rememberMe");
    18.         //3.处理跳转
    19.         //创建ModelAndView对象
    20.         ModelAndView mav = new ModelAndView();
    21.         if (yzm.equals(code)) {
    22.             Student student_find = studentService.login(student);
    23.             System.out.println(student_find);
    24.             if (student_find != null) {
    25.                 //1.办卡
    26.                 String cusername = java.net.URLEncoder.encode(name, "utf-8");
    27.                 Cookie cookie1 = new Cookie("uname", cusername);
    28.                 Cookie cookie2 = new Cookie("passwd", pwd);
    29.                 //3.设置范围
    30. /* cookie1.setPath("/mydemo3/");
    31. cookie2.setPath("/mydemo3/");
    32. */
    33.                 if ("true".equals(rember)) {
    34.                     //2.设置时效(一次浏览器的关闭)
    35.                     cookie1.setMaxAge(60 * 60 * 24 * 10);
    36.                     cookie2.setMaxAge(60 * 60 * 24 * 10);
    37.                 } else {
    38.                     cookie1.setMaxAge(0);
    39.                     cookie2.setMaxAge(0);
    40.                 }
    41.                 //4.给客户
    42.                 response.addCookie(cookie1);
    43.                 response.addCookie(cookie2);
    44.                 //向模型对象中添加数据
    45.                 mav.addObject("student", student_find);
    46.                 session.setAttribute("stulogin", student_find);
    47.                 session.setAttribute("studentname", student.getStdname());
    48.                 session.setAttribute("stdpwd", student.getStdpassword());
    49.                 //设置逻辑视图名
    50.                 mav.setViewName("studentstage");
    51.                 //返回ModelAndView对象
    52.                 return mav;
    53.             } else {
    54.                 mav.addObject("name", "请重新登录!");
    55.                 //设置逻辑视图名
    56.                 mav.setViewName("login");
    57.                 //返回ModelAndView对象
    58.                 return mav;
    59.             }
    60.         } else {
    61.             mav.setViewName("login");
    62.         }
    63.         return mav;
    64.     }
    65. 验证码:
    66. public class CaptcahCode {
    67.     public static String drawImageVerificate(HttpServletResponse response) {
    68.         //定义验证码的宽度和高度
    69.         int width = 120;
    70.         int height = 25;
    71.         //在内存中创建图片
    72.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    73.         //创建图片上下文
    74.         Graphics2D g = image.createGraphics();
    75.         //产生随机对象,此随机对象主要用于算术表达式的数字
    76.         Random random = new Random();
    77.         //设置背景
    78.         g.setColor(getRandomColor(240, 250));
    79.         //设置字体
    80.         g.setFont(new Font("微软雅黑", Font.PLAIN, 22));
    81.         //开始绘制
    82.         g.fillRect(0, 0, width, height);
    83.         //干扰线的绘制,绘制干扰线到图片中
    84.         g.setColor(getRandomColor(180, 230));
    85.         for (int i = 0; i < 100; i++) {
    86.             int x = random.nextInt(width);
    87.             int y = random.nextInt(height);
    88.             int x1 = random.nextInt(60);
    89.             int y1 = random.nextInt(60);
    90.             g.drawLine(x, y, x1, y1);
    91.         }
    92.         //开始进行对算术验证码表达式的拼接
    93.         int num1 = (int) (Math.random() * 10 + 1);
    94.         int num2 = (int) (Math.random() * 10 + 1);
    95.         int fuhao = random.nextInt(3);//产生一个0-2之间的随机整数
    96.         //记录符号
    97.         String fuhaostr = null;
    98.         int result = 0;
    99.         switch (fuhao) {
    100.             case 0:
    101.                 fuhaostr = "+";
    102.                 result = num1 + num2;
    103.                 break;
    104.             case 1:
    105.                 fuhaostr = "-";
    106.                 result = num1 - num2;
    107.                 break;
    108.             case 2:
    109.                 fuhaostr = "*";
    110.                 result = num1 * num2;
    111.                 break;
    112.         }
    113.         //拼接算术表达式,用户图片显示
    114.         String calc = num1 + " " + fuhaostr + " " + num2 + " = ?";
    115.         //设置随机颜色
    116.         g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
    117.         //绘制表达式
    118.         g.drawString(calc, 5, 25);
    119.         //结束绘制
    120.         try {
    121.             //输出图片到页面
    122.             ImageIO.write(image, "jpg", response.getOutputStream());
    123.             return String.valueOf(result);
    124.         } catch (Exception e) {
    125.             e.printStackTrace();
    126.             return null;
    127.         }
    128.     }
    129.     public static Color getRandomColor(int fc, int bc) {
    130.         //利用随机数
    131.         Random random = new Random();
    132.         //随机颜色,了解颜色-Color(red,green,blue).rgb三元色0-255
    133.         if (fc > 255) fc = 255;
    134.         if (bc > 255) bc = 255;
    135.         int r = fc + random.nextInt(bc - fc);
    136.         int g = fc + random.nextInt(bc - fc);
    137.         int b = fc + random.nextInt(bc - fc);
    138.         return new Color(r, g, b);
    139.     }}

    5.3.2注册操作

    本程序设计的权限管理中,支持学生的注册账号功能。若点击注册,则进行注册的映射请求,输入对应信息,进行注册,所示选项全部为必填项,否则无法进行注册,两次输入密码会进行判断,不同则注册失败。输入的学号会事先判断是否已经存在,存在则返回已存在。

     

    图6-3注册界面

    注册部分代码

    1. Dao层:
    2. @Insert("insert into student(stdid, stdname, stdpassword, stdclass) value(#{stdid}, #{stdname}, #{stdpassword},#{stdclass})")
    3. public int insertStudent(Student student);//注册用户
    4. @Select("SELECT COUNT(stdid) from student where stdid=#{stdid}")
    5. public int findStudentExist(String stdid);//用户id查看是否已经存在
    6. Controller层:
    7.   //注册页面显示
    8.     @RequestMapping("/register")
    9.     public String registerUser() {
    10.         return "register";
    11.     }
    12.     //注册功能实现
    13.     @RequestMapping(value = "/registerStudent", method = RequestMethod.POST)
    14.     public String registerUser(Student student, HttpServletRequest request) throws Exception {
    15.         if (!studentService.findStudentExist(student.getStdid())) {
    16.             request.setAttribute("error", "用户ID已经存在!");
    17.             request.setAttribute("stdid", student.getStdid());
    18.             request.setAttribute("stdname", student.getStdname());
    19.             request.setAttribute("stdpassword", student.getStdpassword());
    20.             request.setAttribute("stdclass", student.getStdclass());
    21.             return "register";
    22.         } else {
    23.             studentService.register(student);
    24.         }
    25.         return "tips";}

    5.3.3后台选择操作

    学生登录完毕后,将会进入图6-3-1所示的学生界面,学生可以查看个人信息,查看选课信息以及退课,修改密码,查询课设信息以及选课,成绩查询和报告的上传,下载,预览和删除;教师登录完毕后,将会进入图6-3-2所示的教师界面,教师可以查看个人信息,查看自己的开课信息以及加课,修改密码,查询学生选择课程的信息以及给出学生成绩。

     

    图6-3-1 学生后台界面

     

    图6-3-2教师后台界面

    5.3.4个人中心界面

    点击个人中心的下拉框后,进入如图6-4-1的界面,通过session取出在登录时存入的姓名密码等信息,将查到的信息返回到前端界面进行展示。用户可以点击小眼睛查看自己的密码。

    关键代码:

    1. Dao层:
    2. @Select("SELECT * FROM `student`where stdname =#{stdname}")
    3. public Student findStudentByName(String stdname) throws IOException;//根据用户id查找用户
    4. Controller层:
    5.     @RequestMapping("udentcenter")
    6.     public String center(HttpSession session) throws IOException {
    7.    String stdname = (String) session.getAttribute("studentname");
    8.    Student student = studentService.findStudentByName(stdname);
    9.    session.setAttribute("stu", student);
    10.    return "studentcenter";
    11. }

    图6-4学生个人中心界面

    5.3.5学生课设中心界面

    点击课设中心下拉框,请求映射到对应的地址,通过登录时存放的session,取出登录用户的id,调用service层函数,service层调用dao层的函数,操作数据库取出所需的值,并存放在list集合中,返回该页面。对于还没出成绩的课程,学生有权选择推掉这门课设。同时点击课设名可以查看课设的详细信息,点击组号可以查看小组的详细信息。

    主要代码:

    1. Dao层:
    2. @Select("SELECTquesion.queid,quesion.quename,teacher.teaname,choque.gruid,choque.score from quesion,teacher,choque\n" +
    3.             "where quesion.queid = choque.queid  and teacher.teaid = quesion.teaid and choque.stdid = #{stdid}")
    4. public List<ChooseCourseInfo> findChooseCourseInfo(String stdid);
    5. @Select("SELECT queid,quename,quemess FROM quesion where queid = #{queid}")
    6.     public Quesion showcourse(String queid);
    7. @Select("SELECT `group`.grubody ,`group`.gruhead from `group` where gruid =#{gruid}")
    8.     public Group showgroup(String gruid);
    9. Controller层:
    10. @RequestMapping("dcourse")
    11.     public String stdcourse(HttpSession session, Model model) throws IOException {
    12.         Student stu = (Student) session.getAttribute("stulogin");
    13.         List<CourseInfo> courseInfos = studentService.findCourseInfo(stu.getStdid());
    14.         model.addAttribute("courseinfos", courseInfos);
    15.         return "stdcourse";
    16.     }
    17.  @RequestMapping("owcourse")
    18.     public String showcourse(String queid, Model model) throws IOException {
    19.         Quesion quesion = studentService.showcourse(queid);
    20.         model.addAttribute("quesion", quesion);
    21.         return "showcourse";
    22.     }
    23.     @RequestMapping("owgroup")
    24.     public String showgroup(String gruid, Model model) throws IOException {
    25.         Group group = studentService.showgroup(gruid);
    26.         model.addAttribute("group", group);
    27.         return "showgroup";
    28.     }

     

    图6-5学生课设中心界面

    5.3.6教师课设中心界面

    点击课设中心下拉框,请求映射到对应的地址,通过登录时存放的session,取出登录用户的id,调用service层函数,service层调用dao层的函数,操作数据库取出所需的值,并存放在list集合中,返回该页面,教师还可以对已经添加的课设进行删除,也可以通过点击添加课程设计按钮,输入信息后,添加新的课程设计。

    主要代码:

    1. Dao层:
    2.  @Select("select quesion.queid,quesion.quename,\n" +
    3. "(select COUNT(*) from choque where choque.queid = quesion.queid) as stucount, \n" +"(SELECT COUNT(*) FROM choque where choque.queid = quesion.queid and choque.score<60) as passfail\n" +"FROM quesion WHERE teaid = #{teaid}")
    4. public List<TeacherCourse> findteachercourse(String teaid);
    5.  @Insert("insert into quesion (queid, quename, quemess, teaid) VALUES (#{queid},#{quename},#{quemess},#{teaid}) ")
    6.  @Delete("delete from quesion where queid= #{queid}")
    7.     public void dlelete(String queid);
    8.     public void insertteacourse(Quesion quesion);
    9. Controller层:
    10.  @GetMapping("/topasscourse")
    11.     public String topasscourse() {
    12.         return "teachercourse";}
    13.     @GetMapping("owcourse")
    14.     @ResponseBody
    15.     public Map<String, Object> coursecenter(HttpSession session,HttpServletRequest request) throws IOException {
    16. Teacher teacher = (Teacher) session.getAttribute("tealogin");
    17. List<TeacherCourse> teacherCourses = teacherService.FindTeacherCourse(teacher.getTeaid());
    18.         Map<String, Object> res = new HashMap<>();
    19.         res.put("code"0);
    20.         res.put("data", teacherCourses);
    21.         return res;}
    22.   @RequestMapping("/toaddcourse")
    23.     public String toaddcou() {
    24.         return "addteacourse";
    25.     }
    26.     @RequestMapping("/addteachercourse")
    27.     @ResponseBody
    28.     public Map<String, String> addcourse(Model model, HttpServletRequest request, Quesion quesion, HttpSession session) throws IOException {
    29.         Teacher teacher = (Teacher) session.getAttribute("tealogin");
    30.         System.out.println(quesion);
    31.         Map<String, String> res = new HashMap<>();
    32.         if (!teacherService.findqueexist(quesion.getQueid())) {
    33.             res.put("code","200");
    34.             res.put("msg", "课号已存在,不能重复添加");
    35.         }
    36.         else
    37.             {
    38.                 quesion.setTeaid(teacher.getTeaid());
    39.                 teacherService.insertteacourse(quesion);
    40.                 res.put("code", "0");
    41.                 res.put("msg", "添加成功");
    42.             }
    43.         return res;
    44. }
    45.  @RequestMapping("/delcourse")
    46.     public String delcourse(String gruid) throws IOException {
    47.         studentService.delcourse(gruid);
    48.         return "redirect:coursecenter"; }

     

    图6-6学生课设中心界面

    5.3.7修改密码界面

    点击修改密码下拉框,请求映射到对应的地址,通过登录时存放的session,取出登录用户的id,调用service层函数,service层调用dao层的函数,通过传入的参数进行对学生信息的更新,页面部分,系统会对输入的信息进行判断,包括原密码和新密码,判断原密码是否正确,判断两次输入的密码是否一致。

    关键代码:

    1. Dao层
    2. @Update("UPDATE student set stdpassword = #{stdpassword} where stdid = #{stdid}")
    3.     public int updatestudent(Student student);
    4. Controller层
    5.  @RequestMapping("/pass")
    6.     public String pass() {
    7.         return "studentpass";
    8.     }
    9.     @RequestMapping("/topass")
    10.     public String topass(String stdpassword, HttpSession session) throws IOException {
    11.         Student stu = (Student) session.getAttribute("stulogin");
    12.         String stdid = stu.getStdid();
    13.         System.out.println(stdid + " " + stdpassword);
    14.         Student studd = new Student();
    15.         studd.setStdid(stdid);
    16.         studd.setStdpassword(stdpassword);
    17.         studentService.updateStudent(studd);
    18.         return "redirect:tologin";}

     

    图6-7管理员修改界面

    5.3.8选课查询界面

    点击选课查询下拉框,请求映射到对应的地址,通过映射到JSP界面后,JSP界面通过Ajax异步请求,访问映射地址,通过通过自定义的Json类将数据通过Json数据格式返回到JSP界面,以leyui渲染的数据表格形式展现出来。用户可以对查出来的课设信息进行选择,填写同组成员信息后,将该课程设计加入到自己的课设中心。系统还会判断收入的组号是不是已经存在,存在则返回改界面。

    主要代码:

    1. Dao层:
    2.  @Select("select * from quesion limit #{page},#{size}")
    3. public List<Quesion> FindQuesionByPage(Map<String, Object> params);//分页查询用户
    4.  @Select("select count(gruid) from  `group` where gruid =#{gruid}")
    5.     public int findGroupExist(String gruid);
    6.  @Insert("insert into `group`(gruid, grubody, gruhead) VALUES (#{gruid},#{grubody},#{gruhead})")
    7.     public int addgroup(Group group);
    8.     @Insert("insert into choque(stdid, queid, gruid) VALUES (#{stdid},#{queid},#{gruid})")
    9.     public int addchoque(Choque choque);
    10.     @Select("SELECT COUNT(*) FROM quesion")
    11. public int count() throws SQLException;//查询用户总数
    12. Controller层:
    13.  @RequestMapping(value = "/findquesionPage", method = RequestMethod.GET)
    14.     public void findStudentPage(Quesion quesion, HttpServletRequest request, HttpServletResponse response) throws Exception {
    15.         request.setCharacterEncoding("utf-8");
    16.         response.setContentType("application/json;charset=UTF-8");//设置编码,防止json中文乱码
    17.         int index = 1;
    18.         String sindex = request.getParameter("page");
    19.         if (sindex != null && !"".equals(sindex)) {
    20.             index = Integer.parseInt(sindex);
    21.         }
    22.         int size = 5;
    23.         String ssize = request.getParameter("limit");
    24.         if (ssize != null && !"".equals(ssize)) {
    25.             size = Integer.parseInt(ssize);
    26.         }
    27.         PageBean<Quesion> pageBean = new PageBean<Quesion>();
    28.         pageBean.setLimit(size);
    29.         pageBean.setPage(index);
    30.         System.out.println(pageBean.getLimit());
    31.         try {
    32.             pageBean = studentService.FindQuesionByPage(index, size);
    33.         } catch (IOException | SQLException e) {
    34.             e.printStackTrace();
    35.         }
    36.         JsonResult<Quesion> jr = new JsonResult<Quesion>("0", "", String.valueOf(pageBean.getTotalCount()), pageBean.getList());//交互json格式所需的对象
    37.         String jsonStr = JSON.toJSONString(jr);//将JsonResult对象转换为json串
    38.         PrintWriter out = response.getWriter();
    39.         out.print(jsonStr);
    40.         out.close();}
    41.  @RequestMapping("/toaddcourse")
    42.     public String addcoursejsp(String queid, Model model, HttpSession session) throws IOException {
    43.         Student stud = (Student) session.getAttribute("stulogin");
    44.         model.addAttribute("stqueid", queid);
    45.         model.addAttribute("ststdid", stud.getStdid());
    46.         return "addcourses";
    47.     }
    48.     @RequestMapping("/addinfo")
    49.     public String addgroup(Model model, HttpServletRequest request, Group group, HttpSession session) throws IOException {
    50.         String stdid = request.getParameter("stdid");
    51.         String queid = request.getParameter("queid");
    52.         System.out.println(stdid +" " + queid);
    53.         if (!studentService.findGroupExist(group.getGruid())) {
    54.             Student stud = (Student) session.getAttribute("stulogin");
    55.             model.addAttribute("stqueid", queid);
    56.             model.addAttribute("ststdid", stud.getStdid());
    57.             request.setAttribute("gruid", "该组号已经存在");
    58.             request.setAttribute("grubody", group.getGrubody());
    59.             request.setAttribute("gruhead", group.getGruhead());
    60.             return "addcourses";
    61.         } else {
    62.             Choque choque = new Choque();
    63.             choque.setStdid(stdid);
    64.             choque.setGruid(group.getGruid());
    65.             choque.setQueid(queid);
    66.             studentService.addgroup(group);
    67.             studentService.addchoque(choque);
    68.             return "tips";
    69.         }}

     

    图6-8-1 学生查看课设信息界面

     

    图6-8-2学生填写同组信息添加课设界面

    5.3.9成绩查询界面

    点击成绩查询下拉框,请求映射到对应的地址,获取session中的用户id后,调用service层的函数,在调用dao层的函数,通过数据库查到的信息,封装在一个list集合中返回到jsp界面。

    主要代码:

    1. Dao层:
    2.  @Select("SELECT quesion.queid,quesion.quename,teaname,score from quesion,teacher,choque \n" +
    3.             "where quesion.queid=choque.queid and quesion.teaid = teacher.teaid and choque.stdid = #{stdid}")
    4. public List<CourseInfo> findCourseInfo(String stdid);
    5. Controller层:
    6.   @RequestMapping("dcourse")
    7.     public String stdcourse(HttpSession session, Model model) throws IOException {
    8.         Student stu = (Student) session.getAttribute("stulogin");
    9.         List<CourseInfo> courseInfos = studentService.findCourseInfo(stu.getStdid());
    10.         model.addAttribute("courseinfos", courseInfos);
    11.         return "stdcourse";
    12.     }

    图6-9成绩查询界面

     

    5.3.10文件上传操作

    用户点击文件上传按钮后,请求映射到对应的地址,通过异步Ajax请求,通过session中的id值,调用service层和dao层函数,查询该用户上传的全部文件,封账成一个list集合,最后封装成Map映射,返回的jsp界面,通过layui table对于表格的渲染,展示数据。

    主要代码:

    1. Dao层:
    2.  @Select("SELECT * FROM `files` where files.stdid= #{stdid} order by files.stdid limit #{begin}, #{offset}")
    3.     public List<UserFile> queryByUserId (@Param("stdid") String stdid , @Param("begin") Integer begin , @Param("offset") Integer offset);
    4.     @Select("SELECT COUNT(files.stdid) FROM files where files.stdid = #{stdid}")
    5.     public int queryFileCounts(String stdid);
    6.     @Insert("insert into files(filename, ext, path, size, type, downcount, uploadtime, stdid)\n" +
    7.             " VALUES (#{filename},#{ext},#{path},#{size},#{type},#{downcount},#{uploadtime},#{stdid});")
    8.     public  void savefile(UserFile userFile);
    9.     @Delete(" delete from files where fileid=#{fileid}")
    10.     public void delete(Integer fileid);
    11.     @Select("select * FROM files where fileid =#{fileid}")
    12.     public UserFile queryByUserFileId(Integer fileid);
    13.     @Update("update files set downcount = #{downcount} where fileid = #{fileid}")
    14.     public int updateflie(UserFile userFile);
    15. Controller层:
    16.  // 展示所有的文件
    17.     @GetMapping("/index")
    18.     public String fileIndex() {
    19.         return "filelist";
    20.     }
    21.     @PostMapping("showfiles")
    22.     @ResponseBody
    23.     public Map<String, Object> queryAllFile(HttpSession session, HttpServletRequest request) {
    24.         int page = Integer.parseInt(request.getParameter("page"));
    25.         int limit = Integer.parseInt(request.getParameter("limit"));
    26.         Student student = (Student) session.getAttribute("stulogin");
    27.         List<UserFile> userFiles = userFileService.queryByUserId(student.getStdid(), page, limit);
    28.         Map<String, Object> res = new HashMap<>();
    29.         res.put("code", 0);
    30.         res.put("count", userFileService.queryFileCounts(student.getStdid()));
    31.         res.put("data", userFiles);
    32.         return res;
    33.     }
    34.     // 上传文件到数据库
    35.     @PostMapping("upload")
    36.     @ResponseBody
    37.     public Map<String, String> uploadFile(@RequestParam("file") MultipartFile file, HttpSession session) {
    38.         System.out.println("uploadFile trigger");
    39.         Map<String, String> res = new HashMap<>();
    40.         try {
    41.             // 获取上传用户的Id
    42.             Student student = (Student) session.getAttribute("stulogin");
    43.             // 获取文件的原始名称
    44.             String fileName = file.getOriginalFilename();
    45.             // 获取文件的后缀
    46.             String extension = FilenameUtils.getExtension(file.getOriginalFilename());
    47.             // 获取文件的大小
    48.             long size = file.getSize();
    49.             // 获取文件的类型
    50.             String type = file.getContentType();
    51.             // 根据日期动态的生成目录
    52.             String localContainer = "/fileContainer";
    53.             String uploadPath = ResourceUtils.getURL("classpath").getPath() + localContainer;
    54.             String dateFormat = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    55.             File dateDirPath = new File(uploadPath + File.separator + dateFormat);
    56.             if (!dateDirPath.exists()) {
    57.                 dateDirPath.mkdirs();
    58.             }
    59.             // 处理文件上传
    60.             file.transferTo(new File(dateDirPath, fileName));
    61.             // 将文件信息存入数据库中
    62.             UserFile userFile = new UserFile();
    63.             userFile.setFilename(fileName);
    64.             userFile.setExt('.' + extension);
    65.             userFile.setPath(Paths.get(localContainer, dateFormat, fileName).toString());
    66.             userFile.setSize(size);
    67.             userFile.setType(type);
    68.             userFile.setStdid(student.getStdid());
    69.             userFileService.save(userFile);
    70.             res.put("code", "0");
    71.             res.put("msg", "上传成功");
    72.             res.put("url", "index");
    73.         } catch (IOException e) {
    74.             e.printStackTrace();
    75.             res.put("code", "-1");
    76.             res.put("msg", "上传失败");
    77.             res.put("url", "index");
    78.         }
    79.         return res;
    80.     }
    81.     @GetMapping("delete")
    82.     @ResponseBody
    83.     public Map<String, Object> delete(Integer fileid) {//@PathVariable:接收请求路径中占位符的值
    84.         Map<String, Object> map = new HashMap<>();
    85.         try {
    86.             UserFile fileInfo = userFileService.queryByUserFileId(fileid);
    87.             final String realPath = ResourceUtils.getURL("classpath").getPath() + fileInfo.getPath();
    88.             File file = new File(realPath);
    89.             if (file.exists()) {
    90.                 file.delete();  //立即删除
    91.             }
    92.             userFileService.delete(fileid);
    93.             map.put("code", 0);
    94.             map.put("msg", "删除成功!");
    95.         } catch (FileNotFoundException e) {
    96.             e.printStackTrace();
    97.             map.put("code", -1);
    98.             map.put("msg", "删除成功!");
    99.         }
    100.         return map;
    101.     }
    102.     public void getFile(String openStyle, Integer fileid, HttpServletResponse response) throws IOException {
    103.         UserFile file = userFileService.queryByUserFileId(fileid);
    104.         if(openStyle.equals("inline"))
    105.         {
    106.             // 获取文件信息
    107.             final String realPath = ResourceUtils.getURL("classpath").getPath() + file.getPath();
    108.             // 获取文件输入流
    109.             FileInputStream is = new FileInputStream(new File(realPath));
    110.             // 附件下载
    111.             response.setHeader("content-disposition", openStyle+";filename=" + URLEncoder.encode(file.getFilename(), "UTF-8"));
    112.             // 获取响应response输出流
    113.             ServletOutputStream os = response.getOutputStream();
    114.             // 文件拷贝
    115.             IOUtils.copy(is, os);
    116.             IOUtils.closeQuietly(is);
    117.             IOUtils.closeQuietly(os);
    118.         }
    119.         // 更新下载次数
    120.         if(openStyle.equals("attachment")){
    121.             file.setDowncount(file.getDowncount() + 1);
    122.             userFileService.update(file);
    123.         }
    124.     }
    125.     @GetMapping("preview")
    126.     public void preview(Integer fileid, HttpServletResponse response) throws IOException {
    127.         String openStyle = "inline";
    128.         getFile(openStyle,fileid,response);
    129.     }
    130.     @GetMapping("download")
    131.     public void download( Integer fileid, HttpServletResponse response){
    132.         String openStyle = "attachment";
    133.         try{
    134.             getFile(openStyle,fileid,response);
    135.         } catch (IOException e) {
    136.             e.printStackTrace();
    137.         }}

     

    图6-10-1文件界面

     

    图6-10-2文件上传界面

     

    图6-10-3文件预览界面

    5.3.10教师端信息查询

    老师点击信息查询后,在session中获取老师的信息,传入controller层中,调用service和dao层函数,将得到的结果以封装到list集合,将集合和一些其他信息一同封装到map集合中,通过json串的形式一同传回jsp界面,通过leyui table表格渲染展示。教师也可以对选择了该课设的学生给出成绩,也可以修改成绩,方法类似,这里不再赘述。

    主要代码:

    1. Dao层:
    2.  @Select("SELECT student.stdid,student.stdname,quesion.queid,quesion.quename,choque.score\n" +
    3.             "FROM student,choque,quesion\n" +
    4.             "WHERE student.stdid = choque.stdid AND choque.queid = quesion.queid AND quesion.teaid = #{teaid}")
    5.     public List<Studentlist> showstudent(String teaid);
    6.     @Update("UPDATE choque SET score = #{score} WHERE stdid = #{stdid} AND queid = #{queid}")
    7.     public void upscore(Studentlist studentlist);
    8. Controller层:
    9.  @RequestMapping("owstudents")
    10.     @ResponseBody
    11.     public Map<String, Object> showstudent(HttpSession session, Model model) throws IOException {
    12.         Teacher teacher = (Teacher) session.getAttribute("tealogin");
    13.         List<Studentlist> studentlists = teacherService.showstudent(teacher.getTeaid());
    14.         Map<String, Object> res = new HashMap<>();
    15.         res.put("code"0);
    16.         res.put("data", studentlists);
    17.         return res; }
    18.     @RequestMapping("/addscore")
    19.     @ResponseBody
    20.     public Map<String, Object> addscore(HttpSession session, Studentlist studentlist) throws IOException {
    21.         teacherService.addscore(studentlist);
    22.         Map<String, Object> res = new HashMap<>();
    23.         res.put("code"0);
    24.         res.put("msg""添加成功!");
    25.         return res;}

     

    图6-11教师端信息查询界面

     

    图6-12教师端条件查询界面

    5.3.11拦截器操作

    登录拦截器,默认拦截/student 路径下的所有请求,若session中存在student,则放行,如不存在则跳转到登录界面。

    1. public class LoginConfig implements WebMvcConfigurer {
    2.     @Override
    3.     public void addInterceptors(InterceptorRegistry registry) {
    4.         //注册TestInterceptor拦截器
    5.         InterceptorRegistration registration = registry.addInterceptor(new LoginInterceptor());
    6.         registration.addPathPatterns("/student/*");
    7.         registration.excludePathPatterns(
    8.                 "udent/tologin",
    9.                 "udent/register",
    10.                 "udent/registerStudent",
    11.                 "udent/login",
    12.                 "udent/code",
    13.                 "/css/**",
    14.                 "/images/**",
    15.                 "/imagess/**",
    16.                 "/js/**",
    17.                 "/lib/**" );}}
    18.     @Override
    19. public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
    20.      System.out.println("====================拦截前=================");
    21. HttpSession session = request.getSession();
    22. Student user = (Student) session.getAttribute("stulogin");
    23. if (user != null) {
    24. System.out.println("====================用户已登录================="+user.toString());
    25. return true;
    26. }
    27. if (!request.getContextPath().equals(request.getContextPath() + "student/tologin")) {
    28.             response.sendRedirect(request.getContextPath() + "udent/tologin");
    29. }
    30.  return false;

    项目完整源码+说明书+软件说明+数据库表设计 :

    点他:🍞正在为您运送作品详情

  • 相关阅读:
    Linux常用命令学习3
    优先队列(priority_queue)用法详解
    判断期末挂科问题
    调优zuul1.x(基于arthas)
    vue 01 创建一个简单vue页面
    Spring学习笔记—JDK动态代理
    计算机网络:3数据链路层
    LeetCode 热题 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个正序数组的中位数 5. 最长回文子串
    修改node_modules中安装的依赖(如第三方ui组件样式)并在下次安装时保留
    Android 12(S) 图像显示系统 - SurfaceFlinger GPU合成/CLIENT合成方式 - 随笔1
  • 原文地址:https://blog.csdn.net/qq_47982709/article/details/125469037