StudentAction.java
Upload User: nbxinmin
Upload Date: 2021-10-09
Package Size: 46k
Code Size: 5k
Category:

ISAPI-IE

Development Platform:

Java

  1. package com.xdf.exams.web.action;
  2. import java.util.List;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import javax.servlet.http.HttpSession;
  6. import org.apache.struts.action.ActionForm;
  7. import org.apache.struts.action.ActionForward;
  8. import org.apache.struts.action.ActionMapping;
  9. import com.xdf.exams.bean.Student;
  10. import com.xdf.exams.bo.BOFactory;
  11. import com.xdf.exams.bo.IStudentService;
  12. import com.xdf.exams.util.Constant;
  13. import com.xdf.exams.util.PageUtil;
  14. import com.xdf.exams.web.form.QueryStudentForm;
  15. import com.xdf.exams.web.form.StudentForm;
  16. /** 
  17.  * MyEclipse Struts
  18.  * Creation date: 04-05-2007
  19.  * 
  20.  * XDoclet definition:
  21.  * @struts.action validate="true"
  22.  */
  23. public class StudentAction extends BaseDispatchAction {
  24. public ActionForward show(ActionMapping mapping, ActionForm form,
  25. HttpServletRequest request, HttpServletResponse response) {
  26. QueryStudentForm qsf = (QueryStudentForm)form;
  27. String name = qsf.getQname();
  28. String spageno = request.getParameter("pageno");
  29. IStudentService ser = BOFactory.getStudentService();
  30. int num =ser.findStudentnumByName(name);
  31. PageUtil pu = new PageUtil(spageno,num,Constant.PAGESIZE); 
  32. List list = ser.findStudentByName(name,pu.getPageno(),pu.getPagesize());
  33. request.setAttribute("studentlist",list);
  34. request.setAttribute("pageutil",pu);
  35. return mapping.findForward("show");
  36. }
  37. public ActionForward add(ActionMapping mapping, ActionForm form,
  38. HttpServletRequest request, HttpServletResponse response) {
  39. this.saveToken(request);
  40. return mapping.findForward("add");
  41. }
  42. public ActionForward adddo(ActionMapping mapping, ActionForm form,
  43. HttpServletRequest request, HttpServletResponse response) {
  44. StudentForm studentForm = (StudentForm)form;
  45. ActionForward forward = null;
  46. if(this.isTokenValid(request)) {
  47. IStudentService ser = BOFactory.getStudentService();
  48. try {
  49. if(ser.checkStudentNameExists(studentForm.getUsername(),null)) {
  50. request.setAttribute("message","用户名已经存在");
  51. forward = mapping.findForward("add");
  52. }else {
  53. Student s = new Student();
  54. s.setAddress(studentForm.getAddress());
  55. s.setEmail(studentForm.getEmail());
  56. s.setName(studentForm.getName());
  57. s.setPassword(studentForm.getNewpassword());
  58. s.setSex(studentForm.getSex());
  59. s.setUsername(studentForm.getUsername());
  60. ser.addStudent(s);
  61. request.setAttribute("message","增加成功");
  62. forward = mapping.findForward("show");
  63. this.resetToken(request);
  64. }
  65. } catch (RuntimeException e) {
  66. e.printStackTrace();
  67. request.setAttribute("message","注册失败");
  68. forward = mapping.findForward("add");
  69. }
  70. }else {
  71. request.setAttribute("message","请不要重复提交");
  72. forward = mapping.findForward("show");
  73. }
  74. return forward;
  75. }
  76. public ActionForward delete(ActionMapping mapping, ActionForm form,
  77. HttpServletRequest request, HttpServletResponse response) {
  78. StudentForm studentForm = (StudentForm)form;
  79. IStudentService ser = BOFactory.getStudentService();
  80. try {
  81. ser.deleteStudent(ser.findStudent(new Long(studentForm.getStudentid())));
  82. request.setAttribute("message","删除成功");
  83. } catch (RuntimeException e) {
  84. e.printStackTrace();
  85. request.setAttribute("message","删除失败");
  86. }
  87. return mapping.findForward("show");
  88. }
  89. public ActionForward update(ActionMapping mapping, ActionForm form,
  90. HttpServletRequest request, HttpServletResponse response) {
  91. StudentForm studentForm = (StudentForm)form;
  92. IStudentService ser = BOFactory.getStudentService();
  93. Student s = ser.findStudent(new Long(studentForm.getStudentid()));
  94. if(s!=null) {
  95. studentForm.setAddress(s.getAddress());
  96. studentForm.setEmail(s.getEmail());
  97. studentForm.setName(s.getName());
  98. studentForm.setSex(s.getSex());
  99. studentForm.setUsername(s.getUsername());
  100. studentForm.setStudentid(s.getStudentid());
  101. }
  102. return mapping.findForward("update");
  103. }
  104. public ActionForward updatedo(ActionMapping mapping, ActionForm form,
  105. HttpServletRequest request, HttpServletResponse response) {
  106. StudentForm studentForm = (StudentForm)form;
  107. IStudentService ser = BOFactory.getStudentService();
  108. ActionForward forward = null;
  109. try {
  110. if(ser.checkStudentNameExists(studentForm.getUsername(),studentForm.getStudentid())) {
  111. request.setAttribute("message","用户名已经存在");
  112. forward = mapping.findForward("update");
  113. }else {
  114. Student s = new Student();
  115. s.setAddress(studentForm.getAddress());
  116. s.setEmail(studentForm.getEmail());
  117. s.setName(studentForm.getName());
  118. s.setPassword(studentForm.getNewpassword());
  119. s.setSex(studentForm.getSex());
  120. s.setUsername(studentForm.getUsername());
  121. s.setStudentid(studentForm.getStudentid());
  122. ser.updateStudent(s);
  123. request.setAttribute("message","修改成功");
  124. forward = mapping.findForward("show");
  125. }
  126. } catch (RuntimeException e) {
  127. e.printStackTrace();
  128. request.setAttribute("message","修改失败");
  129. forward = mapping.findForward("update");
  130. }
  131. return forward;
  132. }
  133. }