AdminAction.java~64~
上传用户:zghglow
上传日期:2022-08-09
资源大小:27227k
文件大小:9k
源码类别:

WEB源码(ASP,PHP,...)

开发平台:

JavaScript

  1. package com.chinacannel.xlchemical.struts.Action;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.apache.struts.action.ActionForm;
  5. import org.apache.struts.action.ActionForward;
  6. import org.apache.struts.action.ActionMapping;
  7. import org.apache.struts.actions.DispatchAction;
  8. import com.chinacannel.entity.Admin;
  9. import com.chinacannel.xlchemical.struts.ActionForm.AdminActionForm;
  10. import com.chinacannel.xlchemical.service.AdminService;
  11. import com.chinacannel.entity.Role;
  12. import com.chinacannel.xlchemical.service.RoleService;
  13. import com.chinacannel.common.PageInfo;
  14. import java.util.Date;
  15. import java.util.List;
  16. import java.io.IOException;
  17. import java.net.URLEncoder;
  18. public class AdminAction extends DispatchAction {
  19.     AdminService as = new AdminService();
  20.     public ActionForward CheckLogin(ActionMapping actionMapping,
  21.                                     ActionForm actionForm,
  22.                                     HttpServletRequest servletRequest,
  23.                                     HttpServletResponse servletResponse) {
  24.         AdminActionForm aaf = (AdminActionForm) actionForm;
  25.         Admin admin = null;
  26.         try {
  27.             as.beginTran();
  28.             admin = as.CheckLogin(aaf.getUsername(), aaf.getPassword());
  29.             as.commit();
  30.             servletRequest.getSession().setAttribute("admin", admin);
  31.         } catch (Exception ex) {
  32.             log.error(ex.getMessage(), ex);
  33.             try {
  34.                 as.rollback();
  35.             } catch (Exception ex2) {
  36.                 log.error(ex2.getMessage(), ex2);
  37.             }
  38.         }
  39.         if (admin != null) {
  40.             return actionMapping.findForward("LoginOK");
  41.         } else {
  42.             servletRequest.setAttribute("Message", "登陆失败,请返回");
  43.             servletRequest.setAttribute("Url", "Manage/Index.jsp");
  44.             return actionMapping.findForward("LoginDefeat");
  45.         }
  46.     }
  47.     public ActionForward AddNewAdmin(ActionMapping actionMapping,
  48.                                      ActionForm actionForm,
  49.                                      HttpServletRequest servletRequest,
  50.                                      HttpServletResponse servletResponse) {
  51.         Admin admin = new Admin();
  52.         RoleService rs = new RoleService();
  53.         Role role = null;
  54.         AdminActionForm aaf = (AdminActionForm) actionForm;
  55.         String Message = "";
  56.         boolean b = false;
  57.         try {
  58.             as.beginTran();
  59.             if (!aaf.getId().equals(new Long(0))) {
  60.                 admin = as.GetAdminById(aaf.getId());
  61.             }
  62.             admin.setAdm_Name(aaf.getName());
  63.             role = rs.GetRoleById(aaf.getRole());
  64.             admin.setRole(role);
  65.             admin.setAdm_Pwd("123456");
  66.             admin.setAdm_Time(new Date());
  67.             if (aaf.getId().equals(new Long(0))) {
  68.                 b = as.AddAdmin(admin);
  69.                 Message = "添加新用户成功,默认密码为 123456 ,请返回!";
  70.             } else {
  71.                 b = as.UpdateAdmin(admin);
  72.                 Message = "更新用户角色成功,请返回!";
  73.             }
  74.             as.commit();
  75.         } catch (Exception ex) {
  76.             log.error(ex.getMessage(), ex);
  77.             try {
  78.                 as.rollback();
  79.             } catch (Exception ex2) {
  80.                 log.error(ex2.getMessage(), ex2);
  81.             }
  82.         }
  83.         if (b == false) {
  84.             Message = "操作失败,请返回!";
  85.         }
  86.         servletRequest.setAttribute("Message", Message);
  87.         servletRequest.setAttribute("Url",
  88.                                     "Manage/Admin.do?todo=OpenAdminList");
  89.         return actionMapping.findForward("AddNewAdmin");
  90.     }
  91.     public ActionForward OpenAdminInfo(ActionMapping actionMapping,
  92.                                        ActionForm actionForm,
  93.                                        HttpServletRequest servletRequest,
  94.                                        HttpServletResponse servletResponse) {
  95.         RoleService rs = new RoleService();
  96.         Admin admin = new Admin();
  97.         Long admID = new Long(0);
  98.         List roleList = null;
  99.         try {
  100.             if (servletRequest.getParameter("AdmID") != null) {
  101.                 admID = new Long(servletRequest.getParameter("AdmID"));
  102.             }
  103.             rs.beginTran();
  104.             if (!admID.equals(new Long(0))) {
  105.                 admin = as.GetAdminById(admID);
  106.             }
  107.             roleList = rs.GetAllRole();
  108.             rs.commit();
  109.         } catch (Exception ex) {
  110.             log.error(ex.getMessage(), ex);
  111.             try {
  112.                 as.rollback();
  113.             } catch (Exception ex2) {
  114.                 log.error(ex2.getMessage(), ex2);
  115.             }
  116.         }
  117.         servletRequest.setAttribute("roleList", roleList);
  118.         servletRequest.setAttribute("admin", admin);
  119.         return actionMapping.findForward("OpenAdminInfo");
  120.     }
  121.     public ActionForward OpenAdminList(ActionMapping actionMapping,
  122.                                        ActionForm actionForm,
  123.                                        HttpServletRequest servletRequest,
  124.                                        HttpServletResponse servletResponse) {
  125.         int pageNo = 0;
  126.         Role role = null;
  127.         RoleService rs = new RoleService();
  128.         AdminActionForm aaf = (AdminActionForm) actionForm;
  129.         PageInfo pageInfo = null;
  130.         String condition = "";
  131.         List roleList = null;
  132.         try {
  133.             pageNo = Integer.parseInt(servletRequest.getParameter("pageNo"));
  134.         } catch (Exception ex1) {}
  135.         try {
  136.             if (aaf.getCondition().equals("")) {
  137.                 if (!aaf.getHidCondition().equals("")) {
  138.                     condition = aaf.getHidCondition();
  139.                 }
  140.             } else {
  141.                 condition = aaf.getCondition();
  142.             }
  143.             as.beginTran();
  144.             role = rs.GetRoleById(aaf.getRole());
  145.             pageInfo = as.getPageInfo(condition, role, pageNo);
  146.             roleList = rs.GetAllRole();
  147.             as.commit();
  148.         } catch (Exception ex) {
  149.             log.error(ex.getMessage(), ex);
  150.             try {
  151.                 as.rollback();
  152.             } catch (Exception ex2) {
  153.                 log.error(ex2.getMessage(), ex2);
  154.             }
  155.         }
  156.         servletRequest.setAttribute("roleList", roleList);
  157.         servletRequest.setAttribute("role", role);
  158.         servletRequest.setAttribute("pageInfo", pageInfo);
  159.         return actionMapping.findForward("OpenAdminList");
  160.     }
  161.     public ActionForward DeleteAdmin(ActionMapping actionMapping,
  162.                                      ActionForm actionForm,
  163.                                      HttpServletRequest servletRequest,
  164.                                      HttpServletResponse servletResponse) {
  165.         Admin adm = new Admin();
  166.         boolean b = false;
  167.         String Message = "";
  168.         try {
  169.             as.beginTran();
  170.             adm = as.GetAdminById(new Long(servletRequest.getParameter("AdmID")));
  171.             b = as.DeleteAdmin(adm);
  172.             Message = "删除成功,请返回!";
  173.             as.commit();
  174.         } catch (Exception ex) {
  175.             log.error(ex.getMessage(), ex);
  176.             try {
  177.                 as.rollback();
  178.             } catch (Exception ex2) {
  179.                 log.error(ex2.getMessage(), ex2);
  180.             }
  181.         }
  182.         if (b == false) {
  183.             Message = "删除失败,请返回!";
  184.         }
  185.         servletRequest.setAttribute("Message", Message);
  186.         servletRequest.setAttribute("Url",
  187.                                     "Manage/Admin.do?todo=OpenAdminList");
  188.         return actionMapping.findForward("DeleteAdmin");
  189.     }
  190.     public ActionForward UpdatePwd(ActionMapping actionMapping,
  191.                                    ActionForm actionForm,
  192.                                    HttpServletRequest servletRequest,
  193.                                    HttpServletResponse servletResponse) throws
  194.             IOException {
  195.         AdminActionForm aaf = (AdminActionForm) actionForm;
  196.         Admin admin = null;
  197.         boolean b = false;
  198.         try {
  199.             if (servletRequest.getSession().getAttribute("admin") != null) {
  200.                 admin = (Admin) servletRequest.getSession().getAttribute(
  201.                         "admin");
  202.             }
  203.             if (admin.getAdm_Pwd().equals(aaf.getOldPwd())) {
  204.                 admin.setAdm_Pwd(aaf.getNewPwd1());
  205.                 as.beginTran();
  206.                 b = as.UpdateAdmin(admin);
  207.                 as.commit();
  208.             }
  209.         } catch (Exception ex) {
  210.             log.error(ex.getMessage(), ex);
  211.             try {
  212.                 as.rollback();
  213.             } catch (Exception ex2) {
  214.                 log.error(ex2.getMessage(), ex2);
  215.             }
  216.         }
  217.         if (b == true) {
  218.             String message = "修改成功,请重新登陆";
  219.                     servletResponse.setContentType("text/html");
  220.             servletResponse.setContentType("charset=gb2312");
  221.             servletResponse.getWriter().write(
  222.                     "<script>alert('" + message +
  223.                     "');parent.location='Index.jsp';</script>");
  224.         }
  225.         return null;
  226.     }
  227. }