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

Internet/IE编程

开发平台:

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 org.apache.struts.action.ActionForm;
  6. import org.apache.struts.action.ActionForward;
  7. import org.apache.struts.action.ActionMapping;
  8. import com.xdf.exams.bean.Teacher;
  9. import com.xdf.exams.bo.BOFactory;
  10. import com.xdf.exams.bo.ITeacherService;
  11. import com.xdf.exams.util.Constant;
  12. import com.xdf.exams.util.PageUtil;
  13. import com.xdf.exams.web.form.TeacherForm;
  14. /** 
  15.  * MyEclipse Struts
  16.  * Creation date: 04-07-2007
  17.  * 
  18.  * XDoclet definition:
  19.  * @struts.action validate="true"
  20.  */
  21. public class TeacherAction extends BaseDispatchAction {
  22. public ActionForward show(ActionMapping mapping, ActionForm form,
  23. HttpServletRequest request, HttpServletResponse response) {
  24. ITeacherService ser = BOFactory.getTeacherService();
  25. String spageno = request.getParameter("pageno");
  26. int num = ser.findTeachernum();
  27. PageUtil pu = new PageUtil(spageno,num,Constant.PAGESIZE);
  28. List list = ser.findAllTeacher(pu.getPageno(),pu.getPagesize());
  29. request.setAttribute("pageutil",pu);
  30. request.setAttribute("teacherlist",list);
  31. return mapping.findForward("show");
  32. }
  33. public ActionForward add(ActionMapping mapping, ActionForm form,
  34. HttpServletRequest request, HttpServletResponse response) {
  35. this.saveToken(request);
  36. return mapping.findForward("add");
  37. }
  38. public ActionForward adddo(ActionMapping mapping, ActionForm form,
  39. HttpServletRequest request, HttpServletResponse response) {
  40. TeacherForm tf = (TeacherForm)form;
  41. ActionForward af = null;
  42. if (this.isTokenValid(request)) {
  43. ITeacherService ser = BOFactory.getTeacherService();
  44. Teacher t = new Teacher();
  45. t.setPassword(tf.getPassword());
  46. t.setUsername(tf.getUsername());
  47. try {
  48. ser.addTeacher(t);
  49. request.setAttribute("message","增加教师成功");
  50. this.resetToken(request);
  51. af = mapping.findForward("show");
  52. } catch (RuntimeException e) {
  53. e.printStackTrace();
  54. request.setAttribute("message","增加教师失败,可能是用户名已经存在");
  55. af = mapping.findForward("add");
  56. }
  57. }else {
  58. request.setAttribute("message","请不要重复提交");
  59. af = mapping.findForward("show");
  60. }
  61. return af;
  62. }
  63. public ActionForward update(ActionMapping mapping, ActionForm form,
  64. HttpServletRequest request, HttpServletResponse response) {
  65. TeacherForm tf = (TeacherForm)form;
  66. ITeacherService ser = BOFactory.getTeacherService();
  67. try {
  68. Teacher t = ser.findTeacher(tf.getTeacherid());
  69. tf.setPassword(t.getPassword());
  70. tf.setUsername(t.getUsername());
  71. tf.setTeacherid(t.getTeacherid());
  72. } catch (RuntimeException e) {
  73. e.printStackTrace();
  74. request.setAttribute("message","查询失败");
  75. }
  76. return mapping.findForward("update");
  77. }
  78. public ActionForward updatedo(ActionMapping mapping, ActionForm form,
  79. HttpServletRequest request, HttpServletResponse response) {
  80. TeacherForm tf = (TeacherForm)form;
  81. ActionForward af = null;
  82. ITeacherService ser = BOFactory.getTeacherService();
  83. Teacher t = ser.findTeacher(tf.getTeacherid());
  84. t.setPassword(tf.getPassword());
  85. try {
  86. ser.updateTeacher(t);
  87. request.setAttribute("message","修改教师成功");
  88. af = mapping.findForward("show");
  89. } catch (RuntimeException e) {
  90. e.printStackTrace();
  91. request.setAttribute("message","修改教师失败,可能是用户名已经存在");
  92. af = mapping.findForward("update");
  93. }
  94. return af;
  95. }
  96. public ActionForward delete(ActionMapping mapping, ActionForm form,
  97. HttpServletRequest request, HttpServletResponse response) {
  98. TeacherForm tf = (TeacherForm)form;
  99. ITeacherService ser = BOFactory.getTeacherService();
  100. try {
  101. Teacher t = ser.findTeacher(tf.getTeacherid());
  102. if(t.getUsername().equals("admin")) {
  103. request.setAttribute("message","请勿删除admin用户");
  104. }else {
  105. ser.deleteTeacher(t);
  106. request.setAttribute("message","删除教师成功");
  107. }
  108. } catch (RuntimeException e) {
  109. e.printStackTrace();
  110. request.setAttribute("message","删除教师失败");
  111. }
  112. return mapping.findForward("show");
  113. }
  114. }