源码获取:俺的博客首页 "资源" 里下载!
项目介绍
这个项目是一个基于SSM的在线考试系统,分为考试用户和管理员两种角色。
考试用户功能包括:
个人信息中心
成绩查询
在线答题
我的错题本
我的试卷
管理员功能包括:
试题管理
题型管理
用户管理
年级管理
课程管理
试卷管理
环境需要
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版本;
6.是否maven项目:是;
技术栈
1. 后端:Spring+SpringMVC+Mybatis
2. 前端:JSP+css+javascript+jQuery+bootstrap
使用说明
1. 使用Navicat或者其它工具,在mysql中创建对应名称的数据库,并导入项目的sql文件;
2. 使用IDEA/Eclipse/MyEclipse导入项目,Eclipse/MyEclipse导入时,若为maven项目请选择maven;
若为maven项目,导入成功后请执行maven clean;maven install命令,然后运行;
3. 将项目中jdbc.properties配置文件中的数据库配置改为自己的配置;
4. 运行项目,在浏览器中输入http://localhost:8080/online_exam_ssm/toLogin.action 登录
学生账户:zhangsan/123456
后管登录地址:http://localhost:8080/online_exam_ssm/admin/login.action
管理员账户:admin/123456
学生管理控制层:
@Controller
public class StuController {
@Autowired
UserService userService;
@Autowired
GradeService gradeService;
//跳转到前台登录页面
@RequestMapping("/toLogin.action")
public String toUserLogin(User user, Model model, HttpSession session){
if(session.getAttribute("userName")!= null){
return "/user/index.jsp";
}
if(session.getAttribute("user")== null){
session.setAttribute("user", userService.get(user.getUserId()));
}
List<User> dataList = userService.find(user);
model.addAttribute("dataList", dataList);
return "/user/login.jsp";
}
/**
* 前台用户登录
* @param user
* @param model
* @param session
* @return
*/
@RequestMapping("/user/toIndex.action")
public String toIndex(User user, Model model, HttpSession session){
if(session.getAttribute("userName")!= null){
return "/user/index.jsp";
}else{
return "forward:/toLogin.action";
}
}
/**
* 用户账号密码检查
* @param user
* @param model
* @param session
* @return
*/
@RequestMapping("/checkPwd.action")
@ResponseBody
public MsgItem checkPwd(User user, Model model, HttpSession session){
MsgItem item = new MsgItem();
User loginUser = userService.login(user);
if(loginUser!=null && loginUser.getUserType() ==0){
if(loginUser.getUserState()==0 ){
item.setErrorNo("1");
item.setErrorInfo("该账号尚未通过审核!");
}else{
item.setErrorNo("0");
item.setErrorInfo("登录成功!");
session.setAttribute("userName", loginUser.getUserName());
session.setAttribute("user", loginUser);
}
}else{
item.setErrorNo("1");
item.setErrorInfo("账号不存在或用户名密码错误!");
}
return item;
}
@RequestMapping("/toRegistPage.action")
public String toRegistPage(Model model, HttpSession session){
List<Grade> list = gradeService.find(new Grade());
model.addAttribute("grade", list);
return "/user/regist.jsp";
}
/**
* 添加用户信息
* @param user
* @param model
* @return
*/
@RequestMapping("/addUserInfo.action")
public String addUserInfo(User user, Model model, HttpSession session){
userService.insert(user);
return "redirect:/toLogin.action";
}
//跳转到前台登录页面
@RequestMapping("/toUserInfo.action")
public String toUserInfo(User user, Model model, HttpSession session){
User loginUser = (User) session.getAttribute("user");
user = userService.getStu(loginUser);
Grade grade = gradeService.get(Integer.parseInt(user.getGrade()));
user.setGrade(grade.getGradeName());
model.addAttribute("user", user);
return "/user/userinfo.jsp";
}
/**
* 更新学生信息
* @param user
* @param model
* @param session
* @return
*/
@RequestMapping("/updateUserInfo.action")
public String updateUserInfo(String newPwd,User user, Model model, HttpSession session){
if(newPwd!= null && newPwd.trim().length()>0){
user.setUserPwd(newPwd);
}
userService.update(user);
user = userService.get(user.getUserId());
if(session.getAttribute("user")== null){
session.setAttribute("user", userService.getStu(user));
}
return "redirect:/user/toIndex.action";
}
//跳转到登录页面
@RequestMapping("/user/exitSys.action")
public String exitSystem(User user, Model model, HttpSession session){
if(session.getAttribute("userName")!= null){
session.removeAttribute("userName");
return "redirect:/toLogin.action";
}
return "redirect:/toLogin.action";
}
//跳转到前台登录页面
@RequestMapping("/toAbout.action")
public String toAbout(User user, Model model, HttpSession session){
User loginUser = (User) session.getAttribute("user");
model.addAttribute("user", loginUser);
return "/user/about.jsp";
}
}
试卷综合管理控制层:
/**
* 试卷综合管理
*
*/
@Controller
public class PaperMgController {
@Autowired
UserService userService;
@Autowired
GradeService gradeService;
@Autowired
PaperService paperService;
@Autowired
CourseService courseService;
@Autowired
QuestionService questionService;
@Autowired
ErrorBookService bookService;
//跳转到成绩查询页面
@RequestMapping("/toScoreQry.action")
public String toScoreQry(User user, Model model, HttpSession session){
if("".equals(user.getUserId()) || user==null){
user = (User) session.getAttribute("user");
}
if(session.getAttribute("user")== null){
session.setAttribute("user", userService.get(user.getUserId()));
}
user = userService.getStu(user);
List<Paper> paper = paperService.getUserPaperById(user.getUserId());
Course course = null;
for(Paper p : paper){
course = courseService.get(Integer.parseInt(p.getCourseId()));
p.setCourseId(course.getCourseName());
}
model.addAttribute("user", user);
model.addAttribute("paper", paper);
return "/user/scorequery.jsp";
}
/**
* 查看试卷详情
* @param paperId
* @param userId
* @param model
* @param session
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping("/qrypaper.action")
public String qrypaper(String paperId,String userId,Model model, HttpSession session){
Map map = new HashMap();
map.put("paperId", paperId);
map.put("userId", userId);
Paper paper = paperService.getPaperDetail(map);
Question question = null;
String []ids = paper.getQuestionId().split(",");
List<Question> selList = new ArrayList<Question>();
List<Question> inpList = new ArrayList<Question>();
List<Question> desList = new ArrayList<Question>();
for(int i = 0;i<ids.length;i++){
question = questionService.get(Integer.parseInt(ids[i]));
if("1".equals(question.getTypeId())){//单选
selList.add(question);
}
if("4".equals(question.getTypeId())){//填空
inpList.add(question);
}
if("5".equals(question.getTypeId())){//简答题
desList.add(question);
}
}
if(selList.size()>0){
model.addAttribute("selectQ", "单项选择题(每题5分)");
model.addAttribute("selList", selList);
}
if(inpList.size()>0){
model.addAttribute("inpQ", "填空题(每题5分)");
model.addAttribute("inpList", inpList);
}
if(desList.size()>0){
model.addAttribute("desQ", "简答题(每题5分)");
model.addAttribute("desList", desList);
}
model.addAttribute("paper", paper);
return "/user/qrypaper.jsp";
}
/**
* 自动评分
* @param paper
* @param model
* @param session
* @return
* @throws UnsupportedEncodingException
*/
@SuppressWarnings("unchecked")
@RequestMapping("/dealPaper.action")
@ResponseBody
public MsgItem dealPaper(Paper paper, Model model, HttpSession session) throws UnsupportedEncodingException{
String paperId = paper.getPaperId();
//答案临时存放
String ans = paper.getScore();
ans = URLDecoder.decode(ans,"UTF-8");
String [] answer = null;
if(ans.contains("&")){
answer = ans.split("&");
}
Map map = new HashMap();
User user = (User) session.getAttribute("user");
map.put("paperId", paperId);
map.put("userId", user.getUserId());
Paper paperInfo = paperService.getPaperDetail(map);
String []ids = paperInfo.getQuestionId().split(",");
List<Question> question = new ArrayList<Question>();
Question ques = null;
int endScore = 0;
ErrorBook book = new ErrorBook();
book.setUserId(user.getUserId());
for(int i = 1 ;i<answer.length;i++){
String[] str = answer[i].split("=");
//题号
String str1 = str[0];
ques = questionService.get(Integer.parseInt(str1));
//数据库对应的答案
String answer1 = ques.getAnswer();
if(str.length>1){
//学生的答案
String str2 = str[1];
if(!"5".equals(ques.getTypeId())){//判断是否为简答题
if(str2.equals(answer1)){//如果用户答案和数据库中的答案一致
endScore+=5;
}else{//插入错题本
book.setQuestion(ques);
book.setCourseId(ques.getCourseId());
book.setGradeId(ques.getGradeId());
book.setUserAnswer(str2);
bookService.insert(book);
}
}
if("5".equals(ques.getTypeId())){//为简答题的时候
String strA = answer1;
String strB = URLDecoder.decode(str2, "UTF-8");//转码
//计算相似
double d = Computeclass.SimilarDegree(strA, strB);
BigDecimal bg = new BigDecimal(d*5).setScale(1, RoundingMode.DOWN);
d = bg.doubleValue();
endScore+=d;
if(d<=2){//如果小于2分,认定错误
book.setQuestion(ques);
book.setCourseId(ques.getCourseId());
book.setGradeId(ques.getGradeId());
book.setUserAnswer(str2);
bookService.insert(book);
}
}
}
}
System.out.println("最后得分:"+endScore);
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentTime = new Date();//得到当前系统时间
String endTime = formatter.format(currentTime); //将日期时间格式化
map.put("beginTime", paper.getBeginTime());
map.put("endTime", endTime);
map.put("score", endScore);
//将考试的试卷状态改为2
map.put("paperState", "2");
paperService.updateUserPaper(map);
if(session.getAttribute("user")== null){
session.setAttribute("user", user);
}
MsgItem msgItem = new MsgItem();
msgItem.setErrorNo("1");
msgItem.setErrorInfo("试卷提交成功,本次考试得分:"+endScore +"分");
return msgItem;
}
/**
* 考试页面
* @param paperId
* @param userId
* @param model
* @param session
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
@RequestMapping("/qryPaperDetail.action")
public String qryPaperDetail(String paperId,String userId,Model model, HttpSession session){
Map map = new HashMap();
map.put("paperId", paperId);
map.put("userId", userId);
Paper paper = paperService.getPaperDetail(map);
Question question = null;
String []ids = paper.getQuestionId().split(",");
List<Question> selList = new ArrayList<Question>();
List<Question> inpList = new ArrayList<Question>();
List<Question> desList = new ArrayList<Question>();
for(int i = 0;i<ids.length;i++){
question = questionService.get(Integer.parseInt(ids[i]));
if("1".equals(question.getTypeId())){//单选
selList.add(question);
}
if("4".equals(question.getTypeId())){//填空
inpList.add(question);
}
if("5".equals(question.getTypeId())){//简答题
desList.add(question);
}
}
if(selList.size()>0){
model.addAttribute("selectQ", "单项选择题(每题5分)");
model.addAttribute("selList", selList);
}
if(inpList.size()>0){
model.addAttribute("inpQ", "填空题(每题5分)");
model.addAttribute("inpList", inpList);
}
if(desList.size()>0){
model.addAttribute("desQ", "简答题(每题5分)");
model.addAttribute("desList", desList);
}
model.addAttribute("paper", paper);
return "/user/paperdetail.jsp";
}
/**
* 获取未考试试卷,并将为考试的试卷添加用户信息
* @param user
* @param model
* @param session
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@RequestMapping("/toMyPaperPage.action")
public String toMyPaperPage(User user,Model model, HttpSession session){
if("".equals(user.getUserId()) || user.getUserId()==null){
user = (User) session.getAttribute("user");
}
if(session.getAttribute("user")== null){
session.setAttribute("user", userService.get(user.getUserId()));
}
user = userService.getStu(user);
Map map =new HashMap();
map.put("userId", user.getUserId());
//List<Paper> paper = paperService.getUserPaperById(user.getUserId());
List<Paper> paper1 = paperService.getUndoPaper(map);
Course course = null;
for(Paper p : paper1){
course = courseService.get(Integer.parseInt(p.getCourseId()));
p.setUserId(user.getUserId());
p.setPaperstate("1");
paperService.insert(p);
p.setCourseId(course.getCourseName());
}
List<Paper> paper = paperService.qryUndoPaper(map);
for(Paper p : paper){
course = courseService.get(Integer.parseInt(p.getCourseId()));
p.setCourseId(course.getCourseName());
}
model.addAttribute("user", user);
model.addAttribute("paper", paper);
return "/user/mypaper.jsp";
}
}
课程管理控制层:
@Controller
public class CourseController {
@Autowired
CourseService courseService;
/**
* 跳转到课程页面
* @param course
* @param model
* @param session
* @return
*/
@RequestMapping("/toCoursePage.action")
public String toCoursePage(@RequestParam(value="page", defaultValue="1") int page,
Course course,Model model, HttpSession session){
//List<Course> dataList = courseService.find(course);
PageInfo<Course> pageInfo = courseService.findByPage(course, page, 5);
List<Course> dataList = pageInfo.getList();
model.addAttribute("dataList", dataList);
model.addAttribute("pageInfo", pageInfo);
return "/admin/course-mgt.jsp";
}
@RequestMapping("/qryCoursePage.action")
@ResponseBody
public List<Course> qryCoursePage(@RequestParam(value="page", defaultValue="1") int page,
Course course,Model model, HttpSession session){
//List<Course> dataList = courseService.find(course);
PageInfo<Course> pageInfo = courseService.findByPage(course, page, 5);
List<Course> dataList = pageInfo.getList();
model.addAttribute("dataList", dataList);
model.addAttribute("pageInfo", pageInfo);
return dataList;
}
/**
* 删除课程信息
* @param courseId 课程编号,删除多个是,id用逗号分隔开
* @param model
* @return
*/
@RequestMapping("/deleteCourse.action")
public String deleteCourse(String courseId, Model model){
if(courseId != null){
String ids[] = courseId.split(",");
for(int i=0;i<ids.length;i++){
courseService.delete(Integer.parseInt(ids[i]));
}
}
return "redirect:/toCoursePage.action";
}
/**
* 跳转到添加课程信息页面
* @param course
* @param model
* @param session
* @return
*/
@RequestMapping("/toAddCourse.action")
public String toAddType(Course course, Model model, HttpSession session){
List<Course> dataList = courseService.find(course);
model.addAttribute("dataList", dataList);
return "/admin/course-reg.jsp";
}
/**
* 添加课程信息
* @param user
* @param model
* @return
*/
@RequestMapping("/addCourse.action")
public String addType(Course course, Model model){
course.setCourseState("0");
courseService.insert(course);
return "redirect:/toCoursePage.action";
}
/**
* 查看题型信息
* @param type
* @param model
* @param session
* @return
*/
@RequestMapping("/toQryCourse.action")
public String toQryType(int courseId, Model model, HttpSession session){
Course courseInfo = courseService.get(courseId);
model.addAttribute("course", courseInfo);
return "/admin/course-qry.jsp";
}
/**
* 跳转到更新题型信息页面
* @param type
* @param model
* @param session
* @return
*/
@RequestMapping("/toUpdCourse.action")
public String toUpdType(int courseId, Model model, HttpSession session){
Course courseInfo = courseService.get(courseId);
model.addAttribute("course", courseInfo);
return "/admin/course-upd.jsp";
}
/**
* 更新题型信息
* @param type
* @param model
* @param session
* @return
*/
@RequestMapping("/updCourse.action")
public String updType(Course course, Model model, HttpSession session){
courseService.update(course);
return "redirect:/toCoursePage.action";
}
/**
* 删除题型信息
* @param type
* @param model
* @param session
* @return
*/
@RequestMapping("/delCourse.action")
public String delCourse(int courseId, Model model, HttpSession session){
courseService.delete(courseId);
return "redirect:/toCoursePage.action";
}
}
源码获取:俺的博客首页 "资源" 里下载!