Java项目:SSH学生网络选课管理系统

114 篇文章 4 订阅
111 篇文章 0 订阅

作者主页:夜未央5788

 简介:Java领域优质创作者、Java项目、学习资料、技术互助

文末获取源码

项目介绍

管理员角色包含以下功能:
管理员登录,专业管理,课程管理,统计信息,修改密码等功能。

用户角色包含以下功能:
用户角色,查询基本信息,选择新课程,查询已选课程,修改登录密码等功能。

由于本程序规模不大,可供课程设计,毕业设计学习演示之用

环境需要

1.运行环境:最好是java jdk 1.8,我们在这个平台上运行的。其他版本理论上也可以。
2.IDE环境:IDEA,Eclipse,Myeclipse都可以。推荐IDEA;
3.tomcat环境:Tomcat 7.x,8.x,9.x版本均可
4.硬件环境:windows 7/8/10 1G内存以上;或者 Mac OS;

5.数据库:MySql 5.7版本;

技术栈

1. 后端:mysql+Spring+hibernate+spring mcv

2. 前端:HTML+CSS+JavaScript+jsp

使用说明

1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中application.yml配置文件中的数据库配置改为自己的配置;

4. 运行项目,输入localhost:8080/ 登录

运行截图

 

 

 

 

 

 

 

相关代码 

管理控制器

@Controller
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IStudyService studyService;
    
    public static SimpleDateFormat df = new SimpleDateFormat("yyyyMM");

    @RequestMapping(value = "/changeStudent", method = RequestMethod.GET)
    public String changeStu(@RequestParam String id, HttpServletRequest req) {
        Student student = adminService.getStudentById(id);
        req.getSession().setAttribute("student", student);
        return "WEB-INF/pages/student/changeStu";
    }

    @RequestMapping(value = "/changeStudent", method = RequestMethod.POST)
    public String changeStudent(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Student student = new Student();
        student.setId(req.getParameter("id"));
        student.setName(req.getParameter("name"));
        student.setPwd(req.getParameter("pwd"));
        student.setMajor(req.getParameter("major"));
        student.setYear(req.getParameter("year"));
        student.setSex(req.getParameter("sex").charAt(0));

        String msg = null;
        if (adminService.updateStudent(student)) {
            msg = "更新成功";
        } else {
            msg = "更新失败";
        }

        req.getSession().setAttribute("msg", msg);

        return "redirect:/studentManage";
    }

    @RequestMapping(value = "addStudent", method = RequestMethod.POST)
    public String addStudent(HttpServletRequest req) {
        try {
            req.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        Student student = new Student();
        student.setId(req.getParameter("id"));
        student.setName(req.getParameter("name"));
        student.setPwd(req.getParameter("pwd"));
        student.setMajor(req.getParameter("major"));
        student.setYear(req.getParameter("year"));
        student.setSex(req.getParameter("sex").charAt(0));

        String msg = null;
        if (adminService.addStudent(student)) {
            msg = "添加成功";
        } else {
            msg = "添加失败";
        }

        req.getSession().setAttribute("msg", msg);

        return "redirect:/studentManage";
    }

    @RequestMapping("delStudent")
    public String delStudent(@RequestParam String id, HttpServletRequest req) {
        adminService.delStudent(id);
        req.getSession().setAttribute("msg", "删除成功");
        return "redirect:/studentManage";
//        if (id != null) {
//            adminService.delStudent(id);
//            req.getSession().setAttribute("msg", "删除学生成功");
//        } else {
//            req.getSession().setAttribute("msg", "删除学生失败");
//        }
//
//        return "redirect:/studentManage";

    }


    //新增课程
    @RequestMapping(value = "addCourse", method = RequestMethod.POST)
    public String addCourse(HttpServletRequest req,
    		@RequestParam("file") MultipartFile file) {
        String msg = null;
        String year_moth = df.format(new Date());
        try {
            req.setCharacterEncoding("utf-8");

            Course course = new Course();
            if(!file.isEmpty()){
		    	ServletContext sc = req.getSession().getServletContext();
		        String dir = sc.getRealPath("/upload/imgurl/"+year_moth+"");    //设定文件保存的目录
		        String filename = file.getOriginalFilename();    //得到上传时的文件名
		        String tempfilename = Tools.getRndFilename()+Tools.getFileExtName(filename);          
		      			
				try {
					FileUtils.writeByteArrayToFile(new File(dir,tempfilename), file.getBytes());
				} catch (IOException e) {
					
					e.printStackTrace();
				}
				course.setImgurl("/upload/imgurl/"+year_moth+"/"+tempfilename); //设置图片所在路径
		        
		    }
            course.setName(req.getParameter("name"));
            course.setSelected(0);
            course.setAmount(Integer.parseInt(req.getParameter("amount")));
            course.setBelong(req.getParameter("belong"));
            course.setCredit(Integer.parseInt(req.getParameter("credit")));
            course.setPlace(req.getParameter("place"));
            course.setDetail(req.getParameter("detail"));
            course.setTime(req.getParameter("time"));
            if (adminService.addCourse(course)) {
                msg = "添加成功";
            } else {
                msg = "添加失败";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            msg = "添加失败";
        } finally {
            req.getSession().setAttribute("msg", msg);
            return "redirect:/courseManage";
        }
    }

    @RequestMapping("adminDelCourse")
    public String delCourse(@RequestParam String id, HttpServletRequest req) {
        adminService.delCourse(Integer.parseInt(id));
        req.getSession().setAttribute("msg", "删除成功");
        return "redirect:/courseManage";
    }

    @RequestMapping(value = "/changeCourse", method = RequestMethod.GET)
    public String changeCourse(@RequestParam String id, HttpServletRequest req) {
        Course course = adminService.getCourseById(Integer.parseInt(id));
        req.getSession().setAttribute("course", course);
        return "WEB-INF/pages/course/changeClz";
    }

    @RequestMapping(value = "/changeCourse", method = RequestMethod.POST)
    public String changeCourse(HttpServletRequest req,
    		@RequestParam("file") MultipartFile file) {
        String msg = null;
        String year_moth = df.format(new Date());
        try {
            req.setCharacterEncoding("utf-8");


            Integer id = (Integer) req.getSession().getAttribute("id");

            Course course = adminService.getCourseById(id);
            
            if(!file.isEmpty()){
		    	ServletContext sc = req.getSession().getServletContext();
		        String dir = sc.getRealPath("/upload/imgurl/"+year_moth+"");    //设定文件保存的目录
		        String filename = file.getOriginalFilename();    //得到上传时的文件名
		        String tempfilename = Tools.getRndFilename()+Tools.getFileExtName(filename);          
		      			
				try {
					FileUtils.writeByteArrayToFile(new File(dir,tempfilename), file.getBytes());
				} catch (IOException e) {
					
					e.printStackTrace();
				}
				course.setImgurl("/upload/imgurl/"+year_moth+"/"+tempfilename); //设置图片所在路径
		        
		    }

            course.setName(req.getParameter("name"));
            course.setAmount(Integer.parseInt(req.getParameter("amount")));
            course.setBelong(req.getParameter("belong"));
            course.setCredit(Integer.parseInt(req.getParameter("credit")));
            course.setPlace(req.getParameter("place"));
            course.setDetail(req.getParameter("detail"));
            course.setTime(req.getParameter("time"));

            if (adminService.updateCourse(course)) {
                msg = "更新成功";
            } else {
                msg = "更新失败";
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            msg = "更新失败";
            e.printStackTrace();
        } finally {
            req.getSession().setAttribute("msg", msg);
            return "redirect:/courseManage";
        }
    }

    //功能描述:选课管理
    @RequestMapping("/chooseManage")
    public String chooseManage(@RequestParam(defaultValue="1")Integer page, 
			@RequestParam(defaultValue="5")Integer rows,HttpServletRequest req,Model model) {
    	Page<StudyInfo> records = adminService.getAllStudyInfo(page,rows);
        req.getSession().setAttribute("records", records.getRows());
        model.addAttribute("page", records);
        return "WEB-INF/pages/student/allChoose";
    }

    @RequestMapping("/delStudyInfo")
    public String delStudyInfo(HttpServletRequest req, @RequestParam String id) {
        String msg = null;
        try {
            Integer stdId = Integer.parseInt(id);
            StudyInfo info = adminService.getStudyById(stdId);
            Course c = adminService.getCourseById(info.getC_id());
            c.setSelected(c.getSelected() - 1);
            adminService.updateCourse(c);
            adminService.delStudyInfo(stdId);
            msg = "删除成功";
        } catch (Exception e) {
            msg = "删除失败";
            e.printStackTrace();
        } finally {
            req.getSession().setAttribute("msg", msg);
            return "redirect:/chooseManage";
        }
    }


    @RequestMapping("/addChoose")
    public String addChoose(HttpServletRequest req, @RequestParam String stuId, @RequestParam String clzId) {
        String msg = null;
        try {
            Integer cId = Integer.parseInt(clzId);
            Student s = adminService.getStudentById(stuId);
            Course c = adminService.getCourseById(cId);
            if (s != null && c != null) {
                if(c.getAmount()>c.getSelected()) {
                    int rst = studentService.selectCource(stuId, cId);
                    if (rst==0) {
                        msg = "添加成功";
                    } else if(rst==1){
                        msg = "已经选过此课!";
                    } else if(rst==2){
                        msg="该课程已选满!";
                    }else{
                        msg="未知错误!";
                    }
                }
            }else{
                msg="添加失败";
            }
        } catch (Exception e) {
            msg = "添加失败";
            e.printStackTrace();
        } finally {
            req.getSession().setAttribute("msg", msg);
            return "redirect:/chooseManage";
        }
    }

}

登录管理控制器

//系统用户登录
@Controller
public class LoginController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IAdminService adminService;

    @RequestMapping("/login")
    public String userLogin(@RequestParam String id, @RequestParam String pwd, HttpServletRequest req) {
        Student student = null;
        if (id.length() > 0 && id.length() < 20 && pwd.length() > 0 && pwd.length() < 20) {
            student = studentService.login(id, pwd);
        }
        if (student != null) {
            req.getSession().setAttribute("user", student.getName());
            req.getSession().setAttribute("userId", student.getId());
//            req.getSession().setAttribute("msg","登录成功!欢迎您 "+student.getName()+"!");
        } else {
            req.getSession().setAttribute("msg", "登录失败!用户名或密码错误!");
        }
        return "redirect:/index";
    }

    @RequestMapping("/logout")
    public String userLogout(HttpServletRequest req) {
        req.getSession().setAttribute("user", null);
        req.getSession().setAttribute("userId", null);
        return "redirect:/index";
    }

    @RequestMapping("/changePwd")
    public String changePwd(HttpServletRequest req, @RequestParam String old,
                            @RequestParam String newpwd, @RequestParam String newagain) {

        String stuId = (String) req.getSession().getAttribute("userId");
        if (newpwd.equals(newagain) && studentService.changePwd(stuId, old, newpwd)) {
            req.getSession().setAttribute("msg", "修改成功!");
        } else {
            req.getSession().setAttribute("msg", "修改失败!");
        }
        return "redirect:/index";
    }

    @RequestMapping("/adminLogin")
    public String adminLogin(@RequestParam String username,@RequestParam String pwd,HttpServletRequest req){
        if (username.length() > 0 && username.length() < 20 && pwd.length() > 0 && pwd.length() < 20) {
            if(adminService.login(username, pwd)){
                req.getSession().setAttribute("id",username);
                return "redirect:adminIndex";
            }
        }
        return "adminLogin";
    }

}

选课管理控制器

@Controller
public class SelectController {

    @Autowired
    private IStudentService studentService;

    @Autowired
    private IStudyService studyService;

    @Autowired
    private IClzService clzService;


    @RequestMapping("/selectClz")
    public String selectClz(HttpServletRequest req) {
        String sId = (String) req.getSession().getAttribute("userId");
        Course c = (Course) req.getSession().getAttribute("course");

        Integer cId = c.getId();

        String msg = null;
        int rst = studentService.selectCource(sId, cId);
        if (rst==0) {
            msg = "选课成功!";
        } else if(rst==1){
            msg = "已经选过此课!";
        } else if(rst==2){
            msg="该课程已选满!";
        }else{
            msg="未知错误!";
        }
        req.getSession().setAttribute("msg", msg);
        return "WEB-INF/pages/course/detail";
    }

    @RequestMapping("/delCourse")
    public synchronized String delClz(HttpServletRequest req){
        try {
            String stuId = (String) req.getSession().getAttribute("userId");
            Integer clzId =Integer.parseInt(req.getParameter("id"));

            studyService.delCourse(stuId,clzId);
            Course c=clzService.getClzById(clzId);
            c.setSelected(c.getSelected()-1);

            clzService.update(c);

        }catch (NumberFormatException e){
            e.printStackTrace();
            return "404";
        }
        req.getSession().setAttribute("msg","删除成功!");
        return "redirect:/showMyClasses";
    }

}

如果也想学习本系统,下面领取。关注并回复:088ssh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜未央5788

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值