在线考试系统
该项目用struts2,spring,hibernate框架,搭建在线考试系统。网站支持(1)老师创建题库,创建题目,查看题目对题目进行增删改,发布考试(选择考试难中易比例),批改学生试卷,查看学生成绩。(2)学生可以根据科目进行选课,选课后可在线参加考试,查看成绩。(3)院长(所有老师功能院长均有)对老师发布考试的难易程度进行审核。 网站中的人员信息、题库存储在mysql数据库中,每个学生所做的题目写的答案存储在XML文件中。
1、写入接口,对于每一个类都写一个接口,其中写入相对应的方法:
(1)AdminDao.java
public interface AdminDao {
public Admin findAdmin(Admin admin);
}
(2)DeanDao.java
public interface DeanDao {
public Dean findDean(Dean dean);
public List<Exam> getExambyDnumber(String dnumber);
public boolean addExam(Exam exam);
public Exam findById(Integer eid);
public boolean savequestion(Question question);
public List<Exam> getExambyStatus(String status);
public Bdegree getBdegree(Integer eid);
public void UpdateExam(Exam exam);
public void UpdateBdegree(Bdegree bdegree);
public boolean saveBdegree(Bdegree bdegree);
public List<Exam> getDoingExam(String dnumber);
public List<Course> CourseByExam(Exam exam);
public Course CourseBysumtnum(String course,String snumber, String dnumber);
public void CourseUpdate(Course course);
public List<Exam> getEndExam(String dnumber);
public void deanUpdate(Dean dean);
public List<Question> getQuestions(Integer eid);
public Question getQuestionById(Integer qid);
public void updateQuestion(Question q);
public void DeleteQuestion(Question q);
}
(3) StudentDao.java
public interface StudentDao {
public Student findStudent(Student student);
public void saveStudent(Student student);
public List<Exam> findExam();
public Bdegree findBdegreeByEid(Integer eid);
public List<Question> getQuestion(Integer eid, String qdegree);
public Exam findExamById(Integer eid);
public String findQanswer(Integer qid);
public List<Integer> ExamXML(List<String> dlist, List<Question> blist, Exam exam, Student stu);
public List<Course> findCourseBySnumber(String snumber, String status);
public List<Exam> findExamByCourse(Course course,String status);
public List<Exam> findExamCourse(String course)
public void saveCourse(Course cou);
public List<Course> findCourseByStudent(String snumber, String course);
public void CourseUpdate(Course course);
public List<Exam> GetExamCourse(List<Course> course1);
public List<Course> CourseBySnumber(String snumber);
}
(4)TeacherDao.java
public interface TeacherDao {
public Teacher findTeacher(Teacher teacher);
public boolean addExam(Exam exam);
public List<Exam> getExambyTnumber(String tnumber);
public Exam findById(Integer eid);
public boolean savequestion(Question question);
public boolean saveDegree(Bdegree bdegree);
public void ExamUpdate(Exam exam);
public List<Exam> getUnaudExam(String tnumber);
public List<Exam> getUnpassExam(String tnumber);
public Bdegree findBdegreeByEid(Integer eid);
public void BdegreeUpdate(Bdegree bdegree);
public List<Exam> getDoingExam(String tnumber);
public List<Course> CourseByExam(Exam exam);
public Course CourseBysumtnum(String course,String snumber, String tnumber);
public void UpdateCourse(Course course);
public List<Exam> getEndExam(String tnumber);
public Teacher findTeacherbyID(Integer tid);
public void teacherUpdate(Teacher tea);
public List<Question> getQuestions(Integer eid);
public Question getQuestionById(Integer qid);
public void updateQuestion(Question q);
public void DeleteQuestion(Question q);
}
2.写出接口相对应的实现类
(1) AddExamAction
public class AddExamAction extends ActionSupport implements ModelDriven<Exam>{
private Exam exam=new Exam();
public Exam getModel() {
// TODO Auto-generated method stub
return exam;
}
private TeacherService teacherService;
public void setTeacherService(TeacherService teacherService) {
this.teacherService = teacherService;
}
public String execute() throws Exception {
Teacher tea=(Teacher)ActionContext.getContext().getSession().get("teacher");
exam.setEstatus("未发布");
exam.setTname(tea.getTname());
exam.setTnumber(tea.getTnumber());
if(this.teacherService.addExam(exam)==true) {
return SUCCESS;
}else {
this.addActionError("");
return INPUT;
}
}
}
(2)DeanFindAction类
public class DeanFindAction extends ActionSupport implements ModelDriven<Exam>{
private Exam exam=new Exam();
@Override
public Exam getModel() {
// TODO Auto-generated method stub
return exam;
}
private DeanService deanService;
public void setDeanService(DeanService deanService) {
this.deanService = deanService;
}
public String list() {
Dean dean = (Dean)ActionContext.getContext().getSession().get("dean");
String dnumber = dean.getDnumber();
List<Exam> exa = this.deanService.getExambyDnumber(dnumber);
ActionContext.getContext().put("exams", exa);
return "listsuccess";
}
public String add() {
exam=this.deanService.finById(exam.getEid());
ActionContext.getContext().getSession().put("e", exam);
return "addsuccess";
}
public String pub() {
exam=this.deanService.finById(exam.getEid());
ActionContext.getContext().getSession().put("e", exam);
return "pubsuccess";
}
public String unaud() {
ActionContext.getContext().put("exams", exa);
return "unaudsuccess";
}
public String find() {
exam=this.deanService.finById(exam.getEid());
ActionContext.getContext().getSession().put("e", exam);
Bdegree bdegree=this.deanService.getBdegree(exam.getEid());
ActionContext.getContext().getSession().put("bdegree",bdegree);
return "findsuccess";
}
public String doing() {
Dean dean=(Dean)ActionContext.getContext().getSession().get("dean");
String dnumber=dean.getDnumber();
List<Exam> dexam=this.deanService.getDoingExam(dnumber);
ActionContext.getContext().put("exams", dexam);
if(dexam!=null)
this.deanService.CourseByExam(dexam);
return "doing";
}
public String piy() {
Dean dean=(Dean)ActionContext.getContext().getSession().get("dean");
String dnumber=dean.getDnumber();
System.out.println(exam.getCourse());
System.out.println(exam.getEname());
List<Course> co=this.deanService.ListCourseby(exam.getCourse(),dnumber);
String cour=exam.getEname();
Integer eid=exam.getEid();
ActionContext.getContext().getSession().put("eid", eid);
ActionContext.getContext().put("cour",cour);
ActionContext.getContext().put("course",co);
return "piy";
}
public String end() {
Exam e=this.deanService.finById(exam.getEid());
this.deanService.UpdateExam(e);
return "end";
}
public String history() {
Dean dean=(Dean)ActionContext.getContext().getSession().get("dean");
String dnumber=dean.getDnumber();
List<Exam> unaud = this.deanService.getEndExam(dnumber);
ActionContext.getContext().put("exams", unaud);
return "history";
}
public String lookhistory() {
Dean dean=(Dean)ActionContext.getContext().getSession().get("dean");
String dnumber=dean.getDnumber();
List<Course> course=this.deanService.ListCourseby(exam.getCourse(), dnumber);
ActionContext.getContext().put("cour",exam.getEname());
ActionContext.getContext().put("course", course);
return "lookhistory";
}
public String que() {
exam=this.deanService.finById(exam.getEid());
ActionContext.getContext().getSession().put("e", exam);
List<Question> q=this.deanService.getQuestions(exam.getEid());
ActionContext.getContext().put("questions", q);
return "que";
}
}
(3) teaQuestion.java
public class teaQuestion extends ActionSupport implements ModelDriven<Question>{
private Question question= new Question();
@Override
public Question getModel() {
// TODO Auto-generated method stub
return question;
}
private TeacherService teacherService;
public void setTeacherService(TeacherService teacherService) {
this.teacherService = teacherService;
}
public String mod() {
Question q=this.teacherService.getQuestionById(question.getQid());
ActionContext.getContext().getSession().put("q", q);
return "mod";
}
public String update() {
Question q=(Question)ActionContext.getContext().getSession().get("q");
q.setQanswer(question.getQanswer());
q.setQcontent(question.getQcontent());
q.setQdegree(question.getQdegree());
q.setQscore(question.getQscore());
q.setQtitle(question.getQtitle());
q.setQtype(question.getQtype());
this.teacherService.updateQuestion(q);
return "update";
}
public String del() {
Question q=this.teacherService.getQuestionById(question.getQid());
this.teacherService.DeleteQuestion(q);
return "del";
}
}
3.Impl的类
(1)AdminServiceImpl类
public class AdminServiceImpl implements AdminService {
private AdminDao adminDao;
public AdminDao getAdminDao() {
return adminDao;
}
public void setAdminDao(AdminDao adminDao) {
this.adminDao = adminDao;
}
public Admin findAdmin(Admin admin) {
// TODO Auto-generated method stub
Admin firstuser = this.adminDao.findAdmin(admin);
return firstuser;
}
}
(2) DeanServiceImpl类
public class DeanServiceImpl implements DeanService {
private DeanDao deanDao;
public Dean findDean(Dean dean) {
Dean firstuser = this.deanDao.findDean(dean);
return firstuser;
}
public void CourseByExam(List<Exam> dexam) {
List<Integer> m=new ArrayList<Integer>();
List<Integer> n=new ArrayList<Integer>();
for(int i = 0;i<dexam.size();i++) {
List<Course> c1=this.deanDao.CourseByExam(dexam.get(i));
List<Course> c2=this.deanDao.CourseDone1(dexam.get(i));
List<Course> c3=this.deanDao.CourseDone2(dexam.get(i));
m.add(c1.size());
n.add(c2.size()+c3.size());
}
ActionContext.getContext().getSession().put("number1", m);
ActionContext.getContext().getSession().put("number2", n);
}
public List<Course> ListCourseby(String course, String dnumber) {
Exam exam=new Exam();
exam.setCourse(course);
exam.setTnumber(dnumber);
List<Course> c1=this.deanDao.CourseDone1(exam);
List<Course> c2=this.deanDao.CourseDone2(exam);
c1.addAll(c2);
return c1;
}
public void xmlaudit(String name) {
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
List<Question> question=new ArrayList<Question>();
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
document =reader.read(file);
Node node=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node;
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element que = (Element)i.next();
//System.out.println(que.getTextTrim());
if(que.getTextTrim().equalsIgnoreCase("简答题")) {
//System.out.println(que.getQualifiedName());
q.setQcontent(que.elementText("qcontent"));
q.setQanswer(que.elementText("sanswer"));
q.setQid(Integer.parseInt(que.elementText("qid")));
question.add(q);
}
}
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
ActionContext.getContext().getSession().put("question", question);
}
private static int writeXML(Document document,File file) {
int value=0;
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer=null;
try {
writer =new XMLWriter(new FileOutputStream(file),format.createPrettyPrint());
writer.write(document);
value=1;
}catch(Exception e) {
e.printStackTrace();
}
return value;
}
public void updateXML(String name, List<String> blist, List<Integer> dlist) {
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
//System.out.println(" eid: "+eid);
//System.out.println(" name: "+name);
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
System.out.println(" name: "+name);
document =reader.read(file);
Node node=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node;
int m=0;
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element que = (Element)i.next();
System.out.println(que.getTextTrim());
if(que.getTextTrim().equalsIgnoreCase("简答题")) {
Element qscore=que.addElement("qscore");
qscore.setText(blist.get(m));
m++;
}
}
root.element("status").setText("已阅卷");
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
}
public int stuScore(String name) {
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
int result=0;
List<String> score=new ArrayList<String>();
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
document =reader.read(file);
Node node1=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node1;
for (Iterator i =root.elementIterator(); i.hasNext(); ) {
Element element = (Element)i.next();
if(element.getQualifiedName().equalsIgnoreCase("question"))
score.add(element.elementText("qscore"));
}
System.out.println(score.size());
for(int k=0;k<score.size();k++) {
int re=Integer.parseInt(score.get(k));
result=re+result;
}
Node node2=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root2=(Element)node1;
Element score1=root2.addElement("score");
score1.setText(result+"");
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
return result;
}
}
(3) StudentServiceImpl类
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public void saveStudent(Student student){
this.studentDao.saveStudent(student);
}
//查找验证用户信息
public Student findStudent(Student student){
Student firstuser = this.studentDao.findStudent(student);
//在UserDAO查询中已经判断了只有当用户名和密码都存在时才返回firstuser
//所以在这里只用判断firstuser里面用户名或者密码中的一个是否存在就可以
return firstuser;
}
public List<Exam> findExam() {
// TODO Auto-generated method stub
return this.studentDao.findExam();
}
public void joinExam(Student stu, Integer eid) {
// TODO Auto-generated method stub
Bdegree bdegree=new Bdegree();
System.out.print(eid);
bdegree=this.studentDao.findBdegreeByEid(eid);
List<Question> qeasy= this.studentDao.getQuestion(eid,"易");
List<Question> qmiddle= this.studentDao.getQuestion(eid,"中");
List<Question> qdiff= this.studentDao.getQuestion(eid,"难");
int a=(int)(bdegree.getEasy()*10);
int b=(int)(bdegree.getMiddle()*10);
int c=(int)(bdegree.getDifficult()*10);
if(a<qeasy.size()){
Collections.shuffle(qeasy);
List<Question> qea = new ArrayList<Question>();
for(int i=0;i<a;i++) {
qea.add(qeasy.get(i));
}
qeasy=qea;
}
if(b<qmiddle.size()){
Collections.shuffle(qmiddle);
List<Question> qmi = new ArrayList<Question>();
for(int i=0;i<b;i++) {
qmi.add(qmiddle.get(i));
}
qmiddle=qmi;
}
if(c<qdiff.size()) {
Collections.shuffle(qdiff);
List<Question> qdi = new ArrayList<Question>();
for(int i=0;i<c;i++) {
qdi.add(qdiff.get(i));
}
qdiff=qdi;
}
qeasy.addAll(qmiddle);
qeasy.addAll(qdiff);
System.out.println(qeasy.size());
ActionContext.getContext().getSession().put("question", qeasy);
}
public Exam findExamById(Integer eid) {
// TODO Auto-generated method stub
return this.studentDao.findExamById(eid);
}
public List<String> findQanswer(List<Integer> clist) {
// TODO Auto-generated method stub
List<String> m=new ArrayList<String>();
for(int i=0;i<clist.size();i++) {
m.add(this.studentDao.findQanswer(clist.get(i)));
}
return m;
}
public List<Integer> ExamXML(List<String> dlist, List<Question> blist, Exam exam, Student stu) {
return this.studentDao.ExamXML(dlist,blist,exam,stu);
}
public List<Course> findCourseBySnumber(String snumber,String status) {
return this.studentDao.findCourseBySnumber(snumber,status);
}
public List<Exam> findExamByCourse(List<Course> cou,String status) {
List<Exam> exam=new ArrayList<Exam>();
for(int i=0;i<cou.size();i++) {
List<Exam> e=this.studentDao.findExamByCourse(cou.get(i),status);
if(e!=null)
exam.addAll(e);
}
if(exam.size()>0) {
return exam;
}
return null;
}
}
(4) TeacherServiceImpl类
public class TeacherServiceImpl implements TeacherService {
private TeacherDao teacherDao;
public TeacherDao getTeacherDao() {
return teacherDao;
}
public void setTeacherDao(TeacherDao teacherDao) {
this.teacherDao = teacherDao;
}
public Teacher findTeacher(Teacher teacher) {
Teacher firstuser = this.teacherDao.findTeacher(teacher);
return firstuser;
}
public boolean addExam(Exam exam) {
return this.teacherDao.addExam(exam);
}
public List<Exam> getExambyTnumber(String tnumber) {
return this.teacherDao.getExambyTnumber(tnumber);
}
//业务层根据试卷id查询试卷
public Exam findById(Integer eid) {
return teacherDao.findById(eid);
}
public void CourseByExam(List<Exam> unaud) {
List<Integer> m=new ArrayList<Integer>();
List<Integer> n=new ArrayList<Integer>();
for(int i = 0;i<unaud.size();i++) {
List<Course> c1=this.teacherDao.CourseByExam(unaud.get(i));
List<Course> c2=this.teacherDao.CourseDone1(unaud.get(i));
List<Course> c3=this.teacherDao.CourseDone2(unaud.get(i));
m.add(c1.size());
n.add(c2.size()+c3.size());
}
ActionContext.getContext().getSession().put("number1", m);
ActionContext.getContext().getSession().put("number2", n);
}
public List<Course> ListCourseby(String course, String tnumber) {
Exam exam=new Exam();
exam.setCourse(course);
exam.setTnumber(tnumber);
List<Course> c1=this.teacherDao.CourseDone1(exam);
List<Course> c2=this.teacherDao.CourseDone2(exam);
c1.addAll(c2);
return c1;
}
public void xmlaudit(String name) {
// TODO Auto-generated method stub
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
List<Question> question=new ArrayList<Question>();
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
document =reader.read(file);
Node node=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node;
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element que = (Element)i.next();
//System.out.println(que.getTextTrim());
if(que.getTextTrim().equalsIgnoreCase("简答题")) {
//System.out.println(que.getQualifiedName());
q.setQcontent(que.elementText("qcontent"));
q.setQanswer(que.elementText("sanswer"));
q.setQid(Integer.parseInt(que.elementText("qid")));
question.add(q);
}
}
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
ActionContext.getContext().getSession().put("question", question);
}
private static int writeXML(Document document,File file) {
int value=0;
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer=null;
try {
writer =new XMLWriter(new FileOutputStream(file),format.createPrettyPrint());
writer.write(document);
value=1;
}catch(Exception e) {
e.printStackTrace();
}
return value;
}
public void updateXML(String name,List<String> blist, List<Integer> dlist) {
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
document =reader.read(file);
Node node=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node;
int m=0;
for (Iterator i = root.elementIterator(); i.hasNext(); ) {
Element que = (Element)i.next();
//System.out.println(que.getTextTrim());
if(que.getTextTrim().equalsIgnoreCase("简答题")) {
Element qscore=que.addElement("qscore");
qscore.setText(blist.get(m));
m++;
}
}
root.element("status").setText("已阅卷");
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
}
public int stuScore(String name) {
Integer eid=(Integer)ActionContext.getContext().getSession().get("eid");
String id=eid+"";
SAXReader reader=new SAXReader();
Document document=null;
int result=0;
List<String> score=new ArrayList<String>();
Question q=new Question();
try {
File file=new File("E://exam/"+name+".xml");
if(file.exists()) {
document =reader.read(file);
Node node1=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root=(Element)node1;
for (Iterator i =root.elementIterator(); i.hasNext(); ) {
Element element = (Element)i.next();
if(element.getQualifiedName().equalsIgnoreCase("question"))
score.add(element.elementText("qscore"));
}
System.out.println(score.size());
for(int k=0;k<score.size();k++) {
int re=Integer.parseInt(score.get(k));
result=re+result;
}
Node node2=document.selectSingleNode("/exams/exam[@eid='"+id+"']");
Element root2=(Element)node1;
Element score1=root2.addElement("score");
score1.setText(result+"");
writeXML(document, file);
}
}catch(DocumentException e) {
e.printStackTrace();
}
return result;
}
}
4.配置hibernate.hbm.xml
5.截图
(1)登录页界面
(2)管理员试题管理界面
(3)管理员学生管理界面
(4)管理员考试试卷管理界面
(5)成绩查询界面
(6)考试界面
(7)修改密码