Java项目:SSH在线考试管理系统

作者主页:夜未央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. 后端:Spring+SpringMVC+Hibernate

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/ 登录

运行截图

 

 

 

 

 

 

 

 

 

 

相关代码 

ExamAction

package com.java1234.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.java1234.dao.ExamDao;
import com.java1234.dao.QuestionDao;
import com.java1234.model.Exam;
import com.java1234.model.PageBean;
import com.java1234.model.Question;
import com.java1234.model.Student;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 考试Action类
 * @author Administrator
 *
 */
public class ExamAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ExamDao examDao=new ExamDao();
	private QuestionDao questionDao=new QuestionDao();
	
	private HttpServletRequest request;
	
	private String mainPage;
	
	private Exam exam;
	private Exam s_exam;
	
	private List<Exam> examList;
	
	private String page;
	private int total;
	private String pageCode;

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public Exam getExam() {
		return exam;
	}

	public void setExam(Exam exam) {
		this.exam = exam;
	}

	public List<Exam> getExamList() {
		return examList;
	}

	public void setExamList(List<Exam> examList) {
		this.examList = examList;
	}

	public Exam getS_exam() {
		return s_exam;
	}

	public void setS_exam(Exam s_exam) {
		this.s_exam = s_exam;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		this.pageCode = pageCode;
	}

	/**
	 * 计算/添加考试成绩
	 * @return
	 * @throws Exception
	 */
	public String add()throws Exception{
		Map<String, String[]> keyMap = new HashMap<String, String[]>();
        keyMap = request.getParameterMap();
        Iterator<Entry<String,String[]>> it2 = keyMap.entrySet().iterator();
        int totalScore=0;
        int singleScore=0;
        int moreScore=0;
        while (it2.hasNext()) {
            Entry<String, String[]> entry = it2.next();  
            String keyStr=entry.getKey();
            String values[]=entry.getValue();
            String key;
            String value="";
            if(keyStr.equals("exam.student.id")||keyStr.equals("exam.paper.id")){
            	continue;
            }
            if(keyStr.split("-")[1].equals("r")){  // 单选
            	key=keyStr.split("-")[2];
            	value=values[0];
            	singleScore+=this.calScore(key, value, "1");
            }else{  // 多选
            	key=keyStr.split("-")[2];
            	for(String s:values){
            		value+=s+",";
            	}
            	value=value.substring(0,value.length()-1);
            	moreScore+=this.calScore(key, value, "2");
            }
        }
        totalScore=singleScore+moreScore;
        exam.setSingleScore(singleScore);
        exam.setMoreScore(moreScore);
        exam.setScore(totalScore);
        //exam.setExamDate(new Date());
        examDao.saveExam(exam);
		mainPage="exam/examResult.jsp";
		return SUCCESS;
	}
	
	/**
	 * 计算每道题目的得分
	 * @param questionId
	 * @param userAnswer
	 * @return
	 */
	private int calScore(String questionId,String userAnswer,String type)throws Exception{
		Question question=questionDao.getQuestion(questionId);
		if(userAnswer.equals(question.getAnswer())){
			if("1".equals(type)){
				return 20;
			}else{
				return 30;
			}
		}else{
			return 0;
		}
	}
	
	/**
	 * 获取考试成绩
	 * @return
	 * @throws Exception
	 */
	public String getExams()throws Exception{
		examList=examDao.getExams(s_exam,null);
		mainPage="exam/myExam.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取所有考试成绩
	 * @return
	 * @throws Exception
	 */
	public String examList()throws Exception{
		HttpSession session=request.getSession();
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		if(s_exam!=null){
			session.setAttribute("s_exam", s_exam);
		}else{
			Object o=session.getAttribute("s_exam");
			if(o!=null){
				s_exam=(Exam)o;
			}else{
				s_exam=new Exam();
			}
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		examList=examDao.getExams(s_exam,pageBean);
		total=examDao.examCount(s_exam);
		pageCode=PageUtil.genPagation(request.getContextPath()+"/exam!examList",total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		mainPage="exam/examList.jsp";
		return SUCCESS;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}
}

ManagerAction

package com.java1234.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.java1234.dao.ManagerDao;
import com.java1234.model.Manager;
import com.java1234.model.Student;
import com.opensymphony.xwork2.ActionSupport;

public class ManagerAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	
	private ManagerDao managerDao=new ManagerDao();
	
	private Manager manager;
	private String error;
	
	public Manager getManager() {
		return manager;
	}

	public void setManager(Manager manager) {
		this.manager = manager;
	}
	

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	/**
	 * 登录验证
	 * @return
	 * @throws Exception
	 */
	public String login()throws Exception{
		HttpSession session=request.getSession();
		Manager currentUser=managerDao.login(manager);
		if(currentUser==null){
			error="用户名或者密码错误!";
			return ERROR;
		}else{
			session.setAttribute("currentUser", currentUser);
			return SUCCESS;
		}
	}
	
	/**
	 * 注销用户
	 * @throws Exception
	 */
	public String logout()throws Exception{
		request.getSession().invalidate();
		return "logout";
	}
	
	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

}

PaperAction

package com.java1234.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.java1234.dao.PaperDao;
import com.java1234.dao.QuestionDao;
import com.java1234.model.Paper;
import com.java1234.model.Question;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 试卷Action类
 * @author Administrator
 *
 */
public class PaperAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private PaperDao paperDao=new PaperDao();
	private QuestionDao questionDao=new QuestionDao();
	
	private String mainPage;
	private String paperId;
	
	private List<Paper> paperList=new ArrayList<Paper>();
	private List<Question> squestionList=new ArrayList<Question>();
	private List<Question> mquestionList=new ArrayList<Question>();
	
	private String title; // 标题
	

	private Paper paper;
	
	public List<Paper> getPaperList() {
		return paperList;
	}

	public void setPaperList(List<Paper> paperList) {
		this.paperList = paperList;
	}
	
	

	public List<Question> getSquestionList() {
		return squestionList;
	}

	public void setSquestionList(List<Question> squestionList) {
		this.squestionList = squestionList;
	}

	public List<Question> getMquestionList() {
		return mquestionList;
	}

	public void setMquestionList(List<Question> mquestionList) {
		this.mquestionList = mquestionList;
	}

	public void setPaper(Paper paper) {
		this.paper = paper;
	}
	

	public Paper getPaper() {
		return paper;
	}

	

	public String getPaperId() {
		return paperId;
	}

	public void setPaperId(String paperId) {
		this.paperId = paperId;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * 获取所有试卷
	 * @return
	 * @throws Exception
	 */
	public String list()throws Exception{
		paperList=paperDao.getPapers();
		mainPage="exam/selectPaper.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取所有试卷(管理)
	 * @return
	 * @throws Exception
	 */
	public String paperList()throws Exception{
		paperList=paperDao.getPapers();
		mainPage="paper/paperList.jsp";
		return SUCCESS;
	}
	
	/**
	 * 通过id获取试卷实体
	 * @return
	 * @throws Exception
	 */
	public String getPaperById()throws Exception{
		paper=paperDao.getPaper(paperId);
		mainPage="paper/paperSave.jsp";
		return SUCCESS;
	}
	
	/**
	 * 保存预操作
	 * @return
	 * @throws Exception
	 */
	public String preSave()throws Exception{
		if(StringUtil.isNotEmpty(paperId)){
			paper=paperDao.getPaper(paperId);
			title="修改试卷";
		}else{
			title="添加试卷";
		}
		mainPage="paper/paperSave.jsp";
		return SUCCESS;
	}
	
	/**
	 * 保存试卷
	 * @return
	 * @throws Exception
	 */
	public String savePaper()throws Exception{
		if(StringUtil.isNotEmpty(paperId)){
			paper.setId(Integer.parseInt(paperId));
		}else{
			paper.setJoinDate(new Date());			
		}
		paperDao.savePaper(paper);
		return "save";
	}
	
	/**
	 * 删除试卷
	 * @return
	 * @throws Exception
	 */
	public String deletePaper()throws Exception{
		paper=paperDao.getPaper(paperId);
		JSONObject resultJson=new JSONObject();
		if(questionDao.existQuestionByPaperId(paperId)){
			resultJson.put("error","试卷下面有题目,不能删除");
		}else{
			paperDao.paperDelete(paper);
			resultJson.put("success",true);
		}
		ResponseUtil.write(resultJson,ServletActionContext.getResponse());
		return null;
	}
	
	/**
	 * 获取指定试卷
	 * @return
	 * @throws Exception
	 */
	public String getDetailPaper()throws Exception{
		paper=paperDao.getPaper(paperId);
		Set<Question> questionList=paper.getQuestions();
		Iterator<Question> it=questionList.iterator();
		while(it.hasNext()){
			Question q=it.next();
			if("1".equals(q.getType())){
				squestionList.add(q);
			}else{
				mquestionList.add(q);
			}
		}
		squestionList=this.getRandomQuestion(squestionList, 3);
		mquestionList=this.getRandomQuestion(mquestionList, 2);
		mainPage="exam/paper.jsp";
		return SUCCESS;
	}
	
	/**
	 * 获取随机试题
	 * @param questionList
	 * @param num
	 * @return
	 */
	private List<Question> getRandomQuestion(List<Question> questionList,int num){
		List<Question> resultList=new ArrayList<Question>();
		Random random=new Random();
		if(num>0){
			for(int i=1;i<=num;i++){
				int n=random.nextInt(questionList.size());
				Question q=questionList.get(n);
				if(resultList.contains(q)){
					i--;
				}else{
					resultList.add(questionList.get(n));					
				}
			}
		}
		return resultList;
	}

}

QuestionAction

package com.java1234.action;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.java1234.dao.PaperDao;
import com.java1234.dao.QuestionDao;
import com.java1234.model.PageBean;
import com.java1234.model.Paper;
import com.java1234.model.Question;
import com.java1234.util.PageUtil;
import com.java1234.util.PropertiesUtil;
import com.java1234.util.ResponseUtil;
import com.java1234.util.StringUtil;
import com.opensymphony.xwork2.ActionSupport;

public class QuestionAction extends ActionSupport implements ServletRequestAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private HttpServletRequest request;
	
	private QuestionDao questionDao=new QuestionDao();
	private PaperDao paperDao=new PaperDao();
	
	private List<Question> questionList;
	private List<Paper> paperList;
	private String mainPage;
	
	private String questionId;
	private Question question;
	private String title;
	
	private String page;
	private int total;
	private String pageCode;
	
	private Question s_question;
	
	

	public List<Paper> getPaperList() {
		return paperList;
	}

	public void setPaperList(List<Paper> paperList) {
		this.paperList = paperList;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public List<Question> getQuestionList() {
		return questionList;
	}

	public void setQuestionList(List<Question> questionList) {
		this.questionList = questionList;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public String getPageCode() {
		return pageCode;
	}

	public void setPageCode(String pageCode) {
		this.pageCode = pageCode;
	}
	
	

	public Question getS_question() {
		return s_question;
	}

	public void setS_question(Question s_question) {
		this.s_question = s_question;
	}
	
	

	public String getQuestionId() {
		return questionId;
	}

	public void setQuestionId(String questionId) {
		this.questionId = questionId;
	}

	public Question getQuestion() {
		return question;
	}

	public void setQuestion(Question question) {
		this.question = question;
	}

	/**
	 * 查询试题信息
	 * @return
	 * @throws Exception
	 */
	public String list()throws Exception{
		HttpSession session=request.getSession();
		if(StringUtil.isEmpty(page)){
			page="1";
		}
		if(s_question!=null){
			session.setAttribute("s_question", s_question);
		}else{
			Object o=session.getAttribute("s_question");
			if(o!=null){
				s_question=(Question)o;
			}else{
				s_question=new Question();
			}
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		questionList=questionDao.getQuestions(s_question,pageBean);
		total=questionDao.questionCount(s_question);
		pageCode=PageUtil.genPagation(request.getContextPath()+"/question!list",total, Integer.parseInt(page), Integer.parseInt(PropertiesUtil.getValue("pageSize")));
		mainPage="question/questionList.jsp";
		return SUCCESS;
	}
	
	/**
	 * 通过id获取试题
	 * @return
	 * @throws Exception
	 */
	public String getQuestionById()throws Exception{
		question=questionDao.getQuestion(questionId);
		mainPage="question/questionShow.jsp";
		return SUCCESS;
	}
	
	/**
	 * 预编辑操作
	 * @return
	 * @throws Exception
	 */
	public String preSave()throws Exception{
		paperList=paperDao.getPapers();
		if(StringUtil.isNotEmpty(questionId)){
			question=questionDao.getQuestion(questionId);
			title="修改试题信息";
		}else{
			title="添加试题信息";
		}
		mainPage="question/questionSave.jsp";
		return SUCCESS;
	}
	
	/**
	 * 删除试题
	 * @return
	 * @throws Exception
	 */
	public String delete()throws Exception{
		question=questionDao.getQuestion(questionId);
		questionDao.deleteQuestion(question);
		JSONObject resultJson=new JSONObject();
		resultJson.put("success",true);
		ResponseUtil.write(resultJson,ServletActionContext.getResponse());
		return null;
	}
	
	/**
	 * 保存试题
	 * @return
	 * @throws Exception
	 */
	public String saveQuestion()throws Exception{
		if(StringUtil.isNotEmpty(questionId)){
			question.setId(Integer.parseInt(questionId));
		}
		questionDao.saveQuestion(question);
		return "save";
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request=request;
	}

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

夜未央5788

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

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

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

打赏作者

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

抵扣说明:

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

余额充值