ExamAction.java
上传用户:nbxinmin
上传日期:2021-10-09
资源大小:46k
文件大小:6k
源码类别:

Internet/IE编程

开发平台:

Java

  1. package com.xdf.exams.web.action;
  2. import java.util.Date;
  3. import java.util.List;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import javax.servlet.http.HttpSession;
  7. import org.apache.struts.action.ActionForm;
  8. import org.apache.struts.action.ActionForward;
  9. import org.apache.struts.action.ActionMapping;
  10. import com.xdf.exams.bean.Examrecord;
  11. import com.xdf.exams.bean.Question;
  12. import com.xdf.exams.bean.Result;
  13. import com.xdf.exams.bean.Student;
  14. import com.xdf.exams.bean.Subject;
  15. import com.xdf.exams.bo.BOFactory;
  16. import com.xdf.exams.bo.IExamService;
  17. import com.xdf.exams.bo.IQuestionService;
  18. import com.xdf.exams.util.Tools;
  19. import com.xdf.exams.web.form.ExamForm;
  20. public class ExamAction extends BaseDispatchAction {
  21. public ActionForward list(ActionMapping mapping, ActionForm form,
  22. HttpServletRequest request, HttpServletResponse response) {
  23. IExamService ser = BOFactory.getExamService();
  24. HttpSession session = request.getSession();
  25. Student s = (Student)session.getAttribute("student");
  26. List list = ser.findExamable(s.getStudentid());
  27. request.setAttribute("subjectlist",list);
  28. return mapping.findForward("examlist");
  29. }
  30. public ActionForward exampre(ActionMapping mapping, ActionForm form,
  31. HttpServletRequest request, HttpServletResponse response) {
  32. IExamService ser = BOFactory.getExamService();
  33. IQuestionService qser = BOFactory.getQuestionService();
  34. String ssubjectid = request.getParameter("subjectid");
  35. Long subjectid = null;
  36. try {
  37. subjectid = new Long(ssubjectid);
  38. Subject s = qser.findSubject(subjectid);
  39. if(s.getTotalnum().equals(new Integer(0))) {
  40. throw new Exception();
  41. }
  42. } catch (Exception e) {
  43. request.setAttribute("message","科目有误或没有题目,不能参加这门课考试!");
  44. return  mapping.findForward("examlist");
  45. }
  46. HttpSession session = request.getSession();
  47. Student s = (Student)session.getAttribute("student");
  48. boolean b = ser.findcanExam(s.getStudentid(),new Long(subjectid));
  49. if(!b) {
  50. request.setAttribute("message","你不能参加这门课考试!");
  51. return mapping.findForward("examlist");
  52. }else {
  53. Subject sub = qser.findSubject(new Long(subjectid));
  54. session.setAttribute("subject",sub);
  55. return  mapping.findForward("exampre");
  56. }
  57. }
  58. public ActionForward start(ActionMapping mapping, ActionForm form,
  59. HttpServletRequest request, HttpServletResponse response) {
  60. IExamService ser = BOFactory.getExamService();
  61. HttpSession session = request.getSession();
  62. Subject subject = (Subject)session.getAttribute("subject");
  63. ActionForward af = null;
  64. if(subject==null) {
  65. af = mapping.findForward("examlist");
  66. }else {
  67. List list = ser.findinitExam(subject.getSubjectid());
  68. // 把答案和题目列表放到session中
  69. session.setAttribute("examinglist",list);
  70. session.setAttribute("starttime",new Date());
  71. af = mapping.findForward("go");
  72. }
  73. return af;
  74. }
  75. public ActionForward go(ActionMapping mapping, ActionForm form,
  76. HttpServletRequest request, HttpServletResponse response) {
  77. ExamForm ef = (ExamForm)form;
  78. IQuestionService qser = BOFactory.getQuestionService();
  79. HttpSession session = request.getSession();
  80. //得到session中的答案和题目列表
  81. List list = (List)session.getAttribute("examinglist");
  82. if(list==null) {
  83. request.setAttribute("message","你没有在考试");
  84. return mapping.findForward("examlist");
  85. }
  86. //得到刚才做得题目
  87. Examrecord old = (Examrecord)list.get(ef.getOldid());
  88. //得到提交过来的答案
  89. String[] answers = ef.getAnswers();
  90. String ans = "";
  91. if(answers!=null) {
  92. for (int i=0;i<answers.length;i++) {
  93. ans += answers[i];
  94. }
  95. }
  96. //把答得答案保存起来,仍然在session中的List中
  97. old.setAnswer(ans);
  98. //从session得到要显示的题号和答案(now)
  99. Examrecord now = (Examrecord)list.get(ef.getNowid());
  100. //从数据库中查出题目
  101. Question nowq = qser.findQuestion(now.getQuestion().getQuestionid());
  102. request.setAttribute("question",nowq);
  103. //得到要显示的答案
  104. ef.setAnswers(Tools.tostringarray(now.getAnswer()));
  105. ef.setOldid(ef.getNowid());
  106. //得到剩余秒数
  107. Subject subject = (Subject)session.getAttribute("subject");
  108. Date starttime = (Date)session.getAttribute("starttime");
  109. Date nowtime = new Date();
  110. int sec = (int)(subject.getTime()*60 - (nowtime.getTime()-starttime.getTime())/1000);
  111. request.setAttribute("remainsec",new Integer(sec));
  112. return mapping.findForward("exam");
  113. }
  114. public ActionForward end(ActionMapping mapping, ActionForm form,
  115. HttpServletRequest request, HttpServletResponse response) {
  116. ExamForm ef = (ExamForm)form;
  117. IExamService ser = BOFactory.getExamService();
  118. HttpSession session = request.getSession();
  119. //得到session中的答案和题目列表
  120. List list = (List)session.getAttribute("examinglist");
  121. if(list==null) {
  122. request.setAttribute("message","你没有在考试");
  123. return mapping.findForward("examlist");
  124. }
  125. //得到刚才做得题目
  126. Examrecord old = (Examrecord)list.get(ef.getOldid());
  127. //得到提交过来的答案
  128. String[] answers = ef.getAnswers();
  129. String ans = "";
  130. if(answers!=null) {
  131. for (int i=0;i<answers.length;i++) {
  132. ans += answers[i];
  133. }
  134. }
  135. //把答得答案保存起来,仍然在session中的List中
  136. old.setAnswer(ans);
  137. Result r = new Result();
  138. r.setEndtime(new Date());
  139. Date starttime = (Date)session.getAttribute("starttime");
  140. session.removeAttribute("starttime");
  141. r.setStarttime(starttime);
  142. Subject subject = (Subject)session.getAttribute("subject");
  143. session.removeAttribute("subject");
  144. r.setSubject(subject);
  145. Student s = (Student)session.getAttribute("student");
  146. r.setStudent(s);
  147. try {
  148. ser.submit(r,list);
  149. request.setAttribute("message","交卷成功!");
  150. } catch (RuntimeException e) {
  151. e.printStackTrace();
  152. request.setAttribute("message","交卷失败!");
  153. }
  154. session.removeAttribute("examinglist");
  155. return mapping.findForward("examlist");
  156. }
  157. }