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

Internet/IE编程

开发平台:

Java

  1. package com.xdf.exams.web.action;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Set;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.apache.struts.action.ActionForm;
  9. import org.apache.struts.action.ActionForward;
  10. import org.apache.struts.action.ActionMapping;
  11. import com.xdf.exams.bean.Options;
  12. import com.xdf.exams.bean.Question;
  13. import com.xdf.exams.bean.Subject;
  14. import com.xdf.exams.bo.BOFactory;
  15. import com.xdf.exams.bo.IQuestionService;
  16. import com.xdf.exams.util.Constant;
  17. import com.xdf.exams.util.PageUtil;
  18. import com.xdf.exams.util.Tools;
  19. import com.xdf.exams.web.form.QuestionForm;
  20. import com.xdf.exams.web.form.SubjectForm;
  21. /** 
  22.  * MyEclipse Struts
  23.  * Creation date: 04-05-2007
  24.  * 
  25.  * XDoclet definition:
  26.  * @struts.action validate="true"
  27.  */
  28. public class SubjectAction extends BaseDispatchAction {
  29. /** 
  30.  * Method execute
  31.  * @param mapping
  32.  * @param form
  33.  * @param request
  34.  * @param response
  35.  * @return ActionForward
  36.  */
  37. public ActionForward show(ActionMapping mapping, ActionForm form,
  38. HttpServletRequest request, HttpServletResponse response) {
  39. IQuestionService ser = BOFactory.getQuestionService();
  40. String spageno = request.getParameter("pageno");
  41. PageUtil pu = new PageUtil(spageno,ser.findAllSubjectsnum(),Constant.PAGESIZE);
  42. List list = ser.findAllSubjects(pu.getPageno(),pu.getPagesize());
  43. request.setAttribute("subjectlist",list);
  44. request.setAttribute("pageutil",pu);
  45. return mapping.findForward("show");
  46. }
  47. public ActionForward add(ActionMapping mapping, ActionForm form,
  48. HttpServletRequest request, HttpServletResponse response) {
  49. this.saveToken(request);
  50. SubjectForm sf = (SubjectForm)form;
  51. return mapping.findForward("add");
  52. }
  53. public ActionForward adddo(ActionMapping mapping, ActionForm form,
  54. HttpServletRequest request, HttpServletResponse response) {
  55. SubjectForm sf = (SubjectForm)form;
  56. ActionForward af = null;
  57. if(this.isTokenValid(request)) {
  58. IQuestionService ser = BOFactory.getQuestionService();
  59. Subject s = new Subject();
  60. s.setIntro(sf.getIntro());
  61. s.setName(sf.getName());
  62. s.setSdate(Tools.d2sshort(new Date()));
  63. s.setState(sf.getState());
  64. s.setTime(new Long(sf.getTime()));
  65. try {
  66. ser.addSubject(s);
  67. request.setAttribute("message","增加科目成功");
  68. af = mapping.findForward("show");
  69. this.resetToken(request);
  70. } catch (RuntimeException e) {
  71. e.printStackTrace();
  72. request.setAttribute("message","增加科目失败");
  73. af = mapping.findForward("add");
  74. }
  75. }else {
  76. request.setAttribute("message","请不要重复刷新");
  77. af = mapping.findForward("show");
  78. }
  79. return af;
  80. }
  81. public ActionForward delete(ActionMapping mapping, ActionForm form,
  82. HttpServletRequest request, HttpServletResponse response) {
  83. IQuestionService ser = BOFactory.getQuestionService();
  84. SubjectForm sf = (SubjectForm)form;
  85. try {
  86. Subject s = ser.findSubject(sf.getSubjectid());
  87. ser.deleteSubject(s);
  88. request.setAttribute("message","删除成功");
  89. } catch (RuntimeException e) {
  90. e.printStackTrace();
  91. request.setAttribute("message","删除失败");
  92. }
  93. return mapping.findForward("show");
  94. }
  95. public ActionForward update(ActionMapping mapping, ActionForm form,
  96. HttpServletRequest request, HttpServletResponse response) {
  97. IQuestionService ser = BOFactory.getQuestionService();
  98. SubjectForm sf = (SubjectForm)form;
  99. try {
  100. Subject s = ser.findSubject(sf.getSubjectid());
  101. sf.setName(s.getName());
  102. sf.setIntro(s.getIntro());
  103. sf.setState(s.getState());
  104. sf.setTime(s.getTime().intValue());
  105. } catch (RuntimeException e) {
  106. e.printStackTrace();
  107. request.setAttribute("message","查询失败");
  108. }
  109. return mapping.findForward("update");
  110. }
  111. public ActionForward updatedo(ActionMapping mapping, ActionForm form,
  112. HttpServletRequest request, HttpServletResponse response) {
  113. IQuestionService ser = BOFactory.getQuestionService();
  114. SubjectForm sf = (SubjectForm)form;
  115. try {
  116. Subject s = ser.findSubject(sf.getSubjectid());
  117. s.setName(sf.getName());
  118. s.setIntro(sf.getIntro());
  119. s.setState(sf.getState());
  120. s.setTime(new Long(sf.getTime()));
  121. ser.updateSubject(s);
  122. request.setAttribute("message","修改成功");
  123. } catch (RuntimeException e) {
  124. e.printStackTrace();
  125. request.setAttribute("message","修改失败");
  126. }
  127. return mapping.findForward("show");
  128. }
  129. public ActionForward showquestion(ActionMapping mapping, ActionForm form,
  130. HttpServletRequest request, HttpServletResponse response) {
  131. IQuestionService ser = BOFactory.getQuestionService();
  132. QuestionForm sf = (QuestionForm)form;
  133. String spageno = request.getParameter("pageno");
  134. try {
  135. Subject s = ser.findSubject(sf.getSubjectid());
  136. int num = ser.findQuestionnumBySubjects(sf.getSubjectid());
  137. PageUtil pu = new PageUtil(spageno,num,Constant.PAGESIZE);
  138. List list = ser.findQuestionBySubjects(sf.getSubjectid(),pu.getPageno(),pu.getPagesize());
  139. request.setAttribute("questionlist",list);
  140. request.setAttribute("pageutil",pu);
  141. request.setAttribute("subject",s);
  142. } catch (RuntimeException e) {
  143. e.printStackTrace();
  144. request.setAttribute("message","查询失败");
  145. }
  146. return mapping.findForward("show");
  147. }
  148. public ActionForward addquestion(ActionMapping mapping, ActionForm form,
  149. HttpServletRequest request, HttpServletResponse response) {
  150. QuestionForm qf= (QuestionForm)form;
  151. IQuestionService ser = BOFactory.getQuestionService();
  152. request.setAttribute("subject",ser.findSubject(qf.getSubjectid()));
  153. this.saveToken(request);
  154. return mapping.findForward("add");
  155. }
  156. public ActionForward addquestiondo(ActionMapping mapping, ActionForm form,
  157. HttpServletRequest request, HttpServletResponse response) {
  158. ActionForward af = null;
  159. if(this.isTokenValid(request)) {
  160. try {
  161. IQuestionService ser = BOFactory.getQuestionService();
  162. QuestionForm qf= (QuestionForm)form;
  163. Question que = new Question();
  164. que.setContent(qf.getContent());
  165. que.setQtype(qf.getQtype());
  166. String rans = "";
  167. if(qf.getRightanswers()!=null)
  168. for (int i=0;i<qf.getRightanswers().length;i++) {
  169. rans += qf.getRightanswers()[i];
  170. }
  171. que.setRightanswer(rans);
  172. que.setScore(qf.getScore());
  173. que.setSdate(Tools.d2sshort(new Date()));
  174. que.setSubject(ser.findSubject(qf.getSubjectid()));
  175. List oplist = new ArrayList();
  176. if(qf.getOptions()!=null)
  177. for (int i=0;i<qf.getOptions().length;i++) {
  178. if(qf.getOptions()[i]!=null&&!qf.getOptions()[i].equals("")) {
  179. Options ops = new Options();
  180. ops.setContent(qf.getOptions()[i]);
  181. ops.setQuestion(que);
  182. oplist.add(ops);
  183. }
  184. }
  185. ser.addQuestion(que,oplist);
  186. request.setAttribute("message","增加试题成功");
  187. af = mapping.findForward("show");
  188. this.resetToken(request);
  189. } catch (RuntimeException e) {
  190. e.printStackTrace();
  191. request.setAttribute("message","增加试题失败");
  192. af = mapping.findForward("add");
  193. }
  194. }else {
  195. request.setAttribute("message","不要重复提交");
  196. af = mapping.findForward("show");
  197. }
  198. return af;
  199. }
  200. public ActionForward updatequestion(ActionMapping mapping, ActionForm form,
  201. HttpServletRequest request, HttpServletResponse response) {
  202. QuestionForm qf= (QuestionForm)form;
  203. IQuestionService ser = BOFactory.getQuestionService();
  204. Question que = ser.findQuestion(qf.getQuestionid());
  205. qf.setSubjectid(qf.getSubjectid());
  206. qf.setQuestionid(que.getQuestionid());
  207. qf.setContent(que.getContent());
  208. qf.setQtype(que.getQtype());
  209. qf.setScore(que.getScore());
  210. qf.setRightanswers(Tools.tostringarray(que.getRightanswer()));
  211. Set s = que.getOptionses();
  212. String[] os = new String[8];
  213. int i=0;
  214. for (Object o:s) {
  215. Options opt = (Options)o;
  216. os[i] = opt.getContent();
  217. i++;
  218. }
  219. qf.setOptions(os);
  220. request.setAttribute("subject",ser.findSubject(qf.getSubjectid()));
  221. return mapping.findForward("update");
  222. }
  223. public ActionForward updatequestiondo(ActionMapping mapping, ActionForm form,
  224. HttpServletRequest request, HttpServletResponse response) {
  225. ActionForward af = null;
  226. try {
  227. IQuestionService ser = BOFactory.getQuestionService();
  228. QuestionForm qf= (QuestionForm)form;
  229. Question que = ser.findQuestion(qf.getQuestionid());
  230. que.setContent(qf.getContent());
  231. que.setQtype(qf.getQtype());
  232. String rans = "";
  233. if(qf.getRightanswers()!=null)
  234. for (int i=0;i<qf.getRightanswers().length;i++) {
  235. rans += qf.getRightanswers()[i];
  236. }
  237. que.setRightanswer(rans);
  238. que.setScore(qf.getScore());
  239. que.setSubject(ser.findSubject(qf.getSubjectid()));
  240. List oplist = new ArrayList();
  241. if(qf.getOptions()!=null)
  242. for (int i=0;i<qf.getOptions().length;i++) {
  243. if(qf.getOptions()[i]!=null&&!qf.getOptions()[i].equals("")) {
  244. Options ops = new Options();
  245. ops.setContent(qf.getOptions()[i]);
  246. ops.setQuestion(que);
  247. oplist.add(ops);
  248. }
  249. }
  250. ser.updateQuestion(que,oplist);
  251. request.setAttribute("message","修改试题成功");
  252. af = mapping.findForward("show");
  253. this.resetToken(request);
  254. } catch (RuntimeException e) {
  255. e.printStackTrace();
  256. request.setAttribute("message","修改试题失败");
  257. af = mapping.findForward("update");
  258. }
  259. return af;
  260. }
  261. public ActionForward deletequestion(ActionMapping mapping, ActionForm form,
  262. HttpServletRequest request, HttpServletResponse response) {
  263. QuestionForm qf = (QuestionForm)form;
  264. IQuestionService ser = BOFactory.getQuestionService();
  265. try {
  266. ser.deleteQuestion(ser.findQuestion(qf.getQuestionid()));
  267. request.setAttribute("message","删除成功");
  268. } catch (RuntimeException e) {
  269. e.printStackTrace();
  270. request.setAttribute("message","删除失败");
  271. }
  272. return mapping.findForward("show");
  273. }
  274. }