AdminAction.java
上传用户:yunfeng252
上传日期:2022-07-23
资源大小:12403k
文件大小:4k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package org.yufeng.struts.action;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. import java.util.List;
  5. import java.util.Map;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. import org.apache.struts.action.ActionForm;
  10. import org.apache.struts.action.ActionForward;
  11. import org.apache.struts.action.ActionMapping;
  12. import org.yufeng.domains.Admin;
  13. import org.yufeng.service.IAdminServiceDao;
  14. import org.yufeng.utils.*;
  15. public class AdminAction extends BaseAction {
  16. IAdminServiceDao adminService = (IAdminServiceDao) this.getAdminService();
  17. Admin admin = new Admin();
  18. public ActionForward admin_show(ActionMapping mapping, ActionForm form,
  19. HttpServletRequest request, HttpServletResponse response) {
  20. HttpSession session = request.getSession();
  21. List admin_list = this.getAdminService().browseAdmin();
  22. session.setAttribute("admin_list", admin_list);
  23. return mapping.findForward("admin");
  24. }
  25. public ActionForward loginIn(ActionMapping mapping, ActionForm form,
  26. HttpServletRequest request, HttpServletResponse response) {
  27. HttpSession session = request.getSession();
  28. String tourl = "toAdmin";
  29. String username = request.getParameter("username");
  30. String password = request.getParameter("password");
  31. if (getAdminService().validate(username, password)) {
  32. tourl = "toAdmin";
  33. session.setAttribute("AdminLock", "AdminLock");
  34. }
  35. return mapping.findForward(tourl);
  36. }
  37. public ActionForward loginOut(ActionMapping mapping, ActionForm form,
  38. HttpServletRequest request, HttpServletResponse response) {
  39. HttpSession hs = request.getSession();
  40. PrintWriter out;
  41. try {
  42. out = response.getWriter();
  43. if (hs.getAttribute("AdminLock") != null)
  44. {
  45. hs.removeAttribute("AdminLock");
  46. out.print("<script language="javascript">alert("退出登录!");window.location.href='admin_login.jsp';</script>");
  47. }
  48. out.print("<script language="javascript">alert("退出登录!");window.location.href='admin_login.jsp';</script>");
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. return mapping.findForward("login");
  53. }
  54. public ActionForward admin_manage(ActionMapping mapping, ActionForm form,
  55. HttpServletRequest request, HttpServletResponse response) {
  56. Admin admin = null;
  57. HttpSession session = request.getSession();
  58. String operate = request.getParameter("operate");
  59. String url = "admin_manage";
  60. if ("add".equals(operate)) {
  61. String username = request.getParameter("Username");
  62. admin = this.getAdminService().loadAdmin(username);
  63. if (admin != null) {
  64. session.setAttribute("message", "操作失败,用户账号[" + username
  65. + "]以存在");
  66. } else {
  67. admin = new Admin();
  68. admin.setUsername(username);
  69. admin.setPassword(Tools.md5(request.getParameter("Password")));
  70. admin.setAdd_date(Tools.getDataByMD(Tools.getData()));
  71. admin.setLast_login_date(Tools.getData());
  72. admin.setLast_login_ip(request.getRemoteAddr());
  73. adminService.addAdmin(admin);
  74. session.setAttribute("message", "操作成功,用户账号["
  75. + admin.getUsername() + "]创建成功!");
  76. }
  77. } else if ("update".equals(operate)) {
  78. String username = request.getParameter("Username");
  79. String password = Tools.md5(request.getParameter("Password"));
  80. admin = this.getAdminService().loadAdmin(username);
  81. // 数据库里没有这个账号
  82. if (admin == null) {
  83. session.setAttribute("message", "操作失败,用户账号[" + username
  84. + "]不存在");
  85. } else {
  86. admin.setPassword(password);
  87. this.getAdminService().updateAdmin(admin);
  88. session.setAttribute("message", "操作成功,用户账号["
  89. + admin.getUsername() + "]的修改完成");
  90. }
  91. }
  92. return mapping.findForward(url);
  93. }
  94. public ActionForward delete(ActionMapping mapping, ActionForm form,
  95. HttpServletRequest request, HttpServletResponse response) {
  96. String[] idss = request.getParameterValues("ManagerID");
  97. for (int i = 0; i < idss.length; i++) {
  98. admin.setId(new Integer(idss[i]));
  99. this.getAdminService().delAdmin(admin);
  100. this.admin_show(mapping, form, request, response);
  101. }
  102. return mapping.findForward("admin");
  103. }
  104. }