基于javaweb+mysql的ssm在线考试系统(java+ssm+mysql+jsp)
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM在线考试系统(java+ssm+mysql+jsp)
运行环境:jdk1.8、Mysql5.7、Tomcat8.5、IDEA/Eclipse
功能简介:在线考试、历史回顾、个人成绩查询等。
管理员和教师功能有:学院管理、班级管理、课程管理、教师、学生管理、统计分析、试卷试题管理、考试安排管理、历史考试管理等
/** 当前选择答案和之前选择答案不同 修改答案记录 */
examChooseInfoService.updateChooseWithIds(examChoose);
} else {
logger.info("考生选择了相同答案,不做修改操作");
}
} else {
response.getWriter().print("f");
return;
}
response.getWriter().print("t");
}
}
/**
*
* <p>
* Title: ExamHistoryInfoHandler
* </p>
* <p>
* Description:
* </p>
*/
@Controller
public class ExamHistoryInfoHandler {
@Autowired
* @return
*/
@RequestMapping(value = "/del/class/{classId}", method = RequestMethod.DELETE)
public String isDelClass(@PathVariable("classId") Integer classId, HttpServletRequest request) {
logger.info("删除班级 " + classId);
// 将删除班级对于之前班主任改为 非班主任状态
// 需要在删除班级之前修改,如果先删除了班级,再根据班级获取教师编号,就不能获取
ClassInfo delClass = classInfoService.getClassById(classId);
String returnMsg = isChangeTeacherWork(0, delClass.getTeacher().getTeacherId());
if (returnMsg != null) {
request.setAttribute("error", "修改教师班主任状态 对应教师编号有误");
return "error";
}
// 删除
int row = classInfoService.isDelClass(classId);
if (row < 1) {
logger.error("班级 " + classId + " 删除失败");
request.setAttribute("error", "班级删除失败,请稍后再试!");
return "../error";
}
return "redirect:/classes";
}
/**
* 预修改班级处理
*
* @param classId
* 班级编号
* @return
*/
@RequestMapping(value = "edit/class/{classId}", method = RequestMethod.GET)
public ModelAndView preUpdateClass(@PathVariable("classId") Integer classId) {
logger.info("预修改班级处理");
ModelAndView model = new ModelAndView();
// 获取要修改班级
ClassInfo classInfo = classInfoService.getClassById(classId);
model.setViewName("/admin/clazz/classedit");
model.addObject("editClass", classInfo);
List<GradeInfo> grades = gradeInfoService.getGrades();
// 获取不是班主任的教师
CourseInfo course = new CourseInfo();
if (gradeId != null)
course.getGrade().setGradeId(gradeId);
if (division != null)
course.setDivision(division);
List<CourseInfo> courses = courseInfoService.getCourses(course);
model.addObject("courses", courses);
return model;
}
/**
* 根据科目编号获取学科信息
*
* @param courseId
* 科目编号
* @return
*/
@RequestMapping("/course/{courseId}")
public ModelAndView getCourseById(@PathVariable("courseId") Integer courseId) {
ModelAndView model = new ModelAndView();
model.setViewName("/admin/course/courseedit");
CourseInfo course = courseInfoService.getCourseById(courseId);
model.addObject("course", course);
/** 获取所有系部列表 */
List<GradeInfo> grades = gradeInfoService.getGrades();
model.addObject("grades", grades);
return model;
}
/**
* 添加/修改科目信息
*
* @param courseId
* 科目编号
* @param isUpdate
* 标识是否为修改操作
* @param courseName
* 科目名称
* @param division
* 分科情况
response.getWriter().print("t");
} else {
response.getWriter().print("f");
}
}
}
/**
*
* <p>
* Title: SubjectInfoHandler
* </p>
* <p>
* Description: 试题
* </p>
*/
@Controller
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 处理 试题 添加到 试卷
*
* @param subjects
* 试题集合
* @param examPaperId
* 对应试卷编号
*/
private void dispatcherExamPaperAndSubject(List<SubjectInfo> subjects, Integer examPaperId) {
List<Integer> subjectIds = new ArrayList<Integer>();
// 试题总量统计
int count = 0;
// 试题总分统计
int score = 0;
/** 添加试题 */
for (SubjectInfo subjectInfo : subjects) {
int row1 = subjectInfoService.isAddSubject(subjectInfo);
score += subjectInfo.getSubjectScore();
subjectIds.add(subjectInfo.getSubjectId());
count++;
}
logger.info("添加试题 SIZE " + count);
/** 添加试题到试卷 */
Map<String, Object> esmMap = new HashMap<String, Object>();
esmMap.put("examPaperId", examPaperId);
esmMap.put("subjectIds", subjectIds);
esmService.isAddESM(esmMap);
logger.info("添加试题 SIZE " + count + " SCORE " + score + " 到试卷 " + examPaperId);
// 修改试卷信息
Map<String, Object> scoreWithNum = new HashMap<String, Object>();
scoreWithNum.put("subjectNum", count);
scoreWithNum.put("score", score);
scoreWithNum.put("examPaperId", examPaperId);
subject.setOptionB(trimChar(subject.getOptionB()));
subject.setOptionC(trimChar(subject.getOptionC()));
subject.setOptionD(trimChar(subject.getOptionD()));
}
int row = subjectInfoService.isAddSubject(subject);
response.getWriter().print("试题添加成功!");
}
/**
* 删除试题
*
* @param subjectId
* @param response
* @throws IOException
*/
@RequestMapping(value = "/delSubject", method = RequestMethod.POST)
public void delSubject(@RequestParam("subjectId") Integer subjectId, HttpServletResponse response)
throws IOException {
logger.info("删除试题 " + subjectId);
int row = subjectInfoService.isDelSubject(subjectId);
if (row > 0) {
response.getWriter().print("t");
} else {
response.getWriter().print("f");
}
}
/**
* 修改试题 -- 获取待修改试题信息
*
* @param subjectId
* @return
*/
@RequestMapping("/subject/{subjectId}")
public ModelAndView updateSubject(@PathVariable("subjectId") Integer subjectId) {
logger.info("修改试题 " + subjectId + " 的信息(获取试题信息)");
SubjectInfo subject = subjectInfoService.getSubjectWithId(subjectId);
ModelAndView model = new ModelAndView("/admin/subject-test");
model.addObject("subject", subject);
List<GradeInfo> grades = gradeInfoService.getGrades();
model.addObject("grades", grades);
* @return
*/
@RequestMapping("/selfinfo/{teacherId}")
public ModelAndView loginTeacherSelf(@PathVariable("teacherId") Integer teacherId) {
ModelAndView model = new ModelAndView();
logger.error("教师 " + teacherId + " 查看自己的信息");
if (teacherId == null) {
model.setViewName("../error");
return model;
} else {
List<TeacherInfo> teachers = new ArrayList<TeacherInfo>();
TeacherInfo teacher = teacherInfoService.getTeacherById(teacherId);
teachers.add(teacher);
model.addObject("teachers", teachers);
model.setViewName("/admin/teacher/teachers");
return model;
}
}
/**
* 教师退出登录
*
* @throws IOException
*/
@RequestMapping("/exitTeacher")
public void exitTeacher(HttpSession session, HttpServletResponse response) throws IOException {
session.removeAttribute("loginTeacher");
session.removeAttribute("adminPower");
response.sendRedirect("admin/login.jsp");
}
/**
* 查询教师集合
*
* @param startPage
* @param pageShow
* @return
*/
@RequestMapping(value = "/teachers", method = RequestMethod.GET)
public ModelAndView getTeachers(
@RequestParam(value = "startPage", required = false, defaultValue = "1") Integer startPage, // 当前页码,默认第一页
@RequestParam(value = "pageShow", required = false, defaultValue = "10") Integer pageShow /*
* 每页显示数据量
* ,
}
/**
* 预添加试题
*
* @return
*/
@RequestMapping("/preAddSubject")
public ModelAndView preAddStudent() {
logger.info("预添加试卷信息");
ModelAndView model = new ModelAndView();
model.setViewName("/admin/subject-test");
List<GradeInfo> grades = gradeInfoService.getGrades();
model.addObject("grades", grades);
model.addObject("courses", courseInfoService.getCourses(null));
return model;
}
private String trimChar(String str) {
if (str != null) {
return str.replaceAll("^,*|,*$", "");
}
return str;
}
}
public class ExamPlanInfoHandler {
@Autowired
private ExamPlanInfoService examPlanInfoService;
@Autowired
private ClassInfoService classInfoService;
@Autowired
private CourseInfoService courseInfoService;
@Autowired
private ExamPaperInfoService examPaperInfoService;
@Autowired
private ExamPaperInfo examPaper;
@Autowired
private GradeInfo grade;
private Logger logger = Logger.getLogger(ExamPlanInfoHandler.class);
/**
* 获取所有待考记录
*
* @return
*/
@RequestMapping("/examPlans")
public ModelAndView getExamPlans() {
ModelAndView model = new ModelAndView();
model.setViewName("admin/examPlans");
logger.info("获取待考考试信息");
List<ExamPlanInfo> examPlans = examPlanInfoService.getExamPlans(null);
model.addObject("examPlans", examPlans);
return model;
}
/**
* 预添加
*
* @return
*/
@RequestMapping("/preAddep")
public ModelAndView preAddep() {
ModelAndView model = new ModelAndView();
model.setViewName("admin/examPlanedit");
// 获取所有班级信息
List<ClassInfo> classes = classInfoService.getClasses(null);
Map<String, Object> map = new HashMap<String, Object>();
// 计算当前查询起始数据索引
int startIndex = (startPage - 1) * pageShow;
// map.put("subject", subject);
map.put("startIndex", startIndex);
map.put("pageShow", pageShow);
List<SubjectInfo> subjects = subjectInfoService.getSubjects(map);
model.addObject("subjects", subjects);
// 获取试题总量
int subjectTotal = subjectInfoService.getSubjectTotal();
// 计算总页数
int pageTotal = 1;
if (subjectTotal % pageShow == 0)
pageTotal = subjectTotal / pageShow;
else
pageTotal = subjectTotal / pageShow + 1;
model.addObject("pageTotal", pageTotal);
model.addObject("pageNow", startPage);
// 是否为需要进行手动添加试题到试卷而发起的请求
if (handAdd != null && handAdd == 1) {
model.addObject("handAdd", "1");
}
// 如果是手动添加试题到试卷,则需要返回试卷编号, 且返回当前已经选择试题数量
if (examPaperId != null) {
model.addObject("examPaperId", examPaperId);
List<String> ids = (List<String>) session.getAttribute("ids");
if (ids == null) {
model.addObject("choosed", 0);
} else {
model.addObject("choosed", ids.size());
}
}
return model;
}
/**
* 添加试题
*
* @param subject
* @param response
* @throws IOException
*/
@RequestMapping(value = "/addSubject", method = RequestMethod.POST)
public void addSubject(SubjectInfo subject, HttpServletResponse response) throws IOException {
if (subject != null) {
subject.setSubjectName(trimChar(subject.getSubjectName()));
subject.setRightResult(trimChar(subject.getRightResult()));
@RequestMapping(value = "/teacherlogin", method = RequestMethod.POST)
public String teacherLogin(@RequestParam("teacherAccount") String teacherAccount, HttpServletRequest request) {
if (teacherAccount == null || "".equals(teacherAccount)) {
logger.error("教师账号为空");
request.setAttribute("error", "登录信息有误");
return "/error";
}
logger.info("教师 " + teacherAccount + " 登录");
// 获取当前登录教师
TeacherInfo teacherInfo = teacherInfoService.getTeacherByAccount(teacherAccount);
if (teacherInfo == null) {
logger.error("教师账号为空");
request.setAttribute("error", "账号不存在!");
return "/error";
}
String teacherPwd = request.getParameter("teacherPwd");
if (!teacherInfo.getTeacherPwd().equals(teacherPwd)) {
logger.error("密码错误");
request.setAttribute("error", "密码错误!");
return "/error";
}
// 将当前登录教师 后台权限存入 Session
request.getSession().setAttribute("adminPower", teacherInfo.getAdminPower());
request.getSession().setAttribute("loginTeacher", teacherInfo);
return "redirect:admin/index.jsp";
}
/**
* 教师查看自己的信息
*
* @param teacherId
* @return
*/
@RequestMapping("/selfinfo/{teacherId}")
public ModelAndView loginTeacherSelf(@PathVariable("teacherId") Integer teacherId) {
ModelAndView model = new ModelAndView();
logger.error("教师 " + teacherId + " 查看自己的信息");
if (teacherId == null) {
model.setViewName("../error");
return model;
} else {
List<TeacherInfo> teachers = new ArrayList<TeacherInfo>();
TeacherInfo teacher = teacherInfoService.getTeacherById(teacherId);
teachers.add(teacher);
model.addObject("teachers", teachers);
model.setViewName("/admin/teacher/teachers");
return model;
}
@RequestMapping("/stuexam")
public void getStudentExamInfoById(@RequestParam("stuId") Integer studentId, HttpServletResponse response)
throws IOException {
// 获取学生考试信息
List<StudentExamInfo> stuExamInfos = studentExamInfoService.getStudentExamInfo(studentId);
response.getWriter().print(StudentExamInfoCharts.createStudentExamLineJson(stuExamInfos));
}
}
/**
*
* <p>
* Title: ExamChooseInfoHandler
* </p>
* <p>
* Description: 试卷试题答案选择
* </p>
*/
@Controller
public class ExamChooseInfoHandler {
@Autowired
private ExamChooseInfoService examChooseInfoService;
@Autowired
model.setViewName("admin/clazz/classedit");
model.addObject("grades", grades);
// 获取不是班主任的教师
teacher.setIsWork(0);
Map<String, Object> map = new HashMap<String, Object>();
map.put("startIndex", null);
map.put("pageShow", null);
map.put("teacher", teacher);
List<TeacherInfo> teachers = teacherInfoService.getTeachers(map);
model.addObject("teachers", teachers);
model.addObject("editClass", new ClassInfo());
model.addObject("gid",gid);
return model;
}
/**
* 添加班级
*
* @param classInfo
* 班级信息
* @param request
* @return
*/
@RequestMapping(value = "/class", method = RequestMethod.POST)
public String isAddClass(ClassInfo classInfo, HttpServletRequest request) {
//判断班级名是否存在
ClassInfo byClassName = classInfoService.findByClassName(classInfo.getClassName());
if(byClassName!=null){
request.setAttribute("error", "该班级名称已存在!");
return "error";
}
// 修改教师班主任状态
String returnMsg = isChangeTeacherWork(1, classInfo.getTeacher().getTeacherId());
if (returnMsg != null) {
request.setAttribute("error", "修改教师班主任状态 对应教师编号有误");
return "error";
}
// 添加
int row = classInfoService.isAddClass(classInfo);
if (row < 1) {
request.setAttribute("error", "班级 " + classInfo.getClassName() + " 添加失败,请稍后再试!");
return "error";
}
return "redirect:/classes";
}
/**
* 删除班级
*
* @param classId
* 班级编号
* @param request
/**
*
* <p>Title: StudentInfoHandler</p>
* <p>Description: </p>
*/
@Controller
@SuppressWarnings("all")
public class StudentInfoHandler {
@Autowired
private StudentInfoService studentInfoService;
@Autowired
private ClassInfoService classInfoService;
@Autowired
private ExamSubjectMiddleInfoService examSubjectMiddleInfoService;
@Autowired
private ExamHistoryPaperService examHistoryPaperService;
@Autowired
private ExamChooseInfoService examChooseInfoService;
@Autowired
private ExamSubjectMiddleInfo esm;
@Autowired
private ClassInfo classInfo;
@Autowired
private ExamPaperInfo examPaper;
@Autowired
private GradeInfo grade;
@Autowired
private StudentInfo student;
@Autowired
private ExamPaperInfoService examPaperInfoService;
private Logger logger = Logger.getLogger(StudentInfoHandler.class);
/**
* 获取学生集合
* @param studentId 学生编号
* @param classId 班级编号
* @param gradeId 系部编号
* @param startPage 起始页 default=1
* @param pageShow 页容量 default=10
* @return
*/
@RequestMapping("/students")
}
/**
* 处理 试题 添加到 试卷
*
* @param subjects
* 试题集合
* @param examPaperId
* 对应试卷编号
*/
private void dispatcherExamPaperAndSubject(List<SubjectInfo> subjects, Integer examPaperId) {
List<Integer> subjectIds = new ArrayList<Integer>();
// 试题总量统计
int count = 0;
// 试题总分统计
int score = 0;
/** 添加试题 */
for (SubjectInfo subjectInfo : subjects) {
int row1 = subjectInfoService.isAddSubject(subjectInfo);
score += subjectInfo.getSubjectScore();
subjectIds.add(subjectInfo.getSubjectId());
count++;
}
logger.info("添加试题 SIZE " + count);
/** 添加试题到试卷 */
Map<String, Object> esmMap = new HashMap<String, Object>();
esmMap.put("examPaperId", examPaperId);
esmMap.put("subjectIds", subjectIds);
esmService.isAddESM(esmMap);
logger.info("添加试题 SIZE " + count + " SCORE " + score + " 到试卷 " + examPaperId);
// 修改试卷信息
Map<String, Object> scoreWithNum = new HashMap<String, Object>();
scoreWithNum.put("subjectNum", count);
scoreWithNum.put("score", score);
scoreWithNum.put("examPaperId", examPaperId);
/** 修改试卷总分 */
examPaperInfoService.isUpdateExamPaperScore(scoreWithNum);
/** 修改试卷试题总量 */
examPaperInfoService.isUpdateExamPaperSubjects(scoreWithNum);
}
// 条件处理
if (subjectId != null)
subject.setSubjectId(subjectId);
if (courseId != null)
course.setCourseId(courseId);
if (gradeId != null)
grade.setGradeId(gradeId);
Map<String, Object> map = new HashMap<String, Object>();
// 计算当前查询起始数据索引
int startIndex = (startPage - 1) * pageShow;
// map.put("subject", subject);
map.put("startIndex", startIndex);
map.put("pageShow", pageShow);
List<SubjectInfo> subjects = subjectInfoService.getSubjects(map);
model.addObject("subjects", subjects);
// 获取试题总量
int subjectTotal = subjectInfoService.getSubjectTotal();
// 计算总页数
int pageTotal = 1;
if (subjectTotal % pageShow == 0)
pageTotal = subjectTotal / pageShow;
else
pageTotal = subjectTotal / pageShow + 1;
model.addObject("pageTotal", pageTotal);
model.addObject("pageNow", startPage);
// 是否为需要进行手动添加试题到试卷而发起的请求
if (handAdd != null && handAdd == 1) {
model.addObject("handAdd", "1");
}
// 如果是手动添加试题到试卷,则需要返回试卷编号, 且返回当前已经选择试题数量
if (examPaperId != null) {
model.addObject("examPaperId", examPaperId);
List<String> ids = (List<String>) session.getAttribute("ids");
if (ids == null) {
model.addObject("choosed", 0);
} else {
}
@RequestMapping("/error")
public ModelAndView error(ModelAndView modelAndView){
modelAndView.addObject("error","该试卷有关联数据,不可删除");
modelAndView.setViewName("/error");
return modelAndView;
}
/**
* 预添加试卷
*
* @return
*/
@RequestMapping("/preAddExamPaper")
public ModelAndView preAddStudent() {
logger.info("预添加试卷信息");
ModelAndView model = new ModelAndView();
model.setViewName("/admin/examPaperedit");
List<GradeInfo> grades = gradeInfoService.getGrades();
model.addObject("grades", grades);
return model;
}
}
/**
*
* <p>
@RequestMapping("/examPlans")
public ModelAndView getExamPlans() {
ModelAndView model = new ModelAndView();
model.setViewName("admin/examPlans");
logger.info("获取待考考试信息");
List<ExamPlanInfo> examPlans = examPlanInfoService.getExamPlans(null);
model.addObject("examPlans", examPlans);
return model;
}
/**
* 预添加
*
* @return
*/
@RequestMapping("/preAddep")
public ModelAndView preAddep() {
ModelAndView model = new ModelAndView();
model.setViewName("admin/examPlanedit");
// 获取所有班级信息
List<ClassInfo> classes = classInfoService.getClasses(null);
model.addObject("classes", classes);
// 获取所有科目信息
List<CourseInfo> courses = courseInfoService.getCourses(null);
model.addObject("courses", courses);
// 获取所有的试卷信息 -- 纯净的
List<ExamPaperInfo> examPapers = examPaperInfoService.getExamPapersClear();
model.addObject("examPapers", examPapers);
return model;
}
/**
* 添加待考信息
*
* @param examPlan
* 考试安排记录信息
* @return
*/
@RequestMapping(value = "examPlan", method = RequestMethod.POST)