MemberAction.java
上传用户:toby828
上传日期:2015-06-26
资源大小:8558k
文件大小:9k
源码类别:

Jsp/Servlet

开发平台:

Java

  1. package com.webtier;
  2. import org.apache.struts.action.ActionMapping;
  3. import org.apache.struts.action.ActionForm;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. import org.apache.struts.action.ActionForward;
  7. import com.domain.MemberForm;
  8. import org.apache.struts.action.Action;
  9. import com.dao.MemberDao;
  10. import com.tool.Chinese;
  11. import java.util.List;
  12. import javax.servlet.http.HttpSession;
  13. import com.tool.ManagerList;
  14. import java.util.*;
  15. import com.tool.ManagerTrace;
  16. import com.tool.MemberList;
  17. //会员管理Action
  18. public class MemberAction
  19.     extends Action {
  20.   private int action;
  21.   private MemberDao dao = null;
  22.   private HttpSession session = null;
  23.   public ActionForward execute(ActionMapping mapping,
  24.                                ActionForm form,
  25.                                HttpServletRequest request,
  26.                                HttpServletResponse response) {
  27.     dao = new MemberDao();
  28.     this.action = Integer.parseInt(request.getParameter("action"));
  29.     switch (action) {
  30.       case 0: {
  31.         return insertMember(mapping, form, request, response); //添加员工信息
  32.       }
  33.       case 1: {
  34.         return checkMember(mapping, form, request, response); //会员登录
  35.       }
  36.       case 2: {
  37.         return selectMember(mapping, form, request, response); //查看会员信息
  38.       }
  39.       case 3: {
  40.         return selectOneMember(mapping, form, request, response); //查看会员的详细信息
  41.       }
  42.       case 4: {
  43.         return deleteMember(mapping, form, request, response); //删除会员信息
  44.       }
  45.       case 5: {
  46.         return selectOneMemberHead(mapping, form, request, response); //前台查询会员的属性
  47.       }
  48.       case 6: {
  49.         return updateMemberHead(mapping, form, request, response); //修改会员属性
  50.       }
  51.     }
  52.     // MemberForm memberForm = (MemberForm) form;
  53.     throw new java.lang.UnsupportedOperationException(
  54.         "Method $execute() not yet implemented.");
  55.   }
  56. //修改会员属性
  57.   public ActionForward updateMemberHead(ActionMapping mapping,
  58.                                         ActionForm form,
  59.                                         HttpServletRequest request,
  60.                                         HttpServletResponse response) {
  61.     MemberForm memberForm = (MemberForm) form;
  62.     String name = Chinese.chinese(request.getParameter("name")).trim();
  63.     memberForm.setId(Integer.valueOf(request.getParameter("id")));
  64.     memberForm.setName(name);
  65.     memberForm.setPassword(Chinese.chinese(request.getParameter("password")).
  66.                            trim());
  67.     memberForm.setAge(Integer.valueOf(request.getParameter("age").trim()));
  68.     memberForm.setEmail(request.getParameter("email"));
  69.     memberForm.setReallyName(Chinese.chinese(request.getParameter(
  70.         "reallyName")).trim());
  71.     memberForm.setProfession(Chinese.chinese(request.getParameter(
  72.         "profession")).trim());
  73.     memberForm.setQuestion(Chinese.chinese(request.getParameter("question")).
  74.                            trim());
  75.     memberForm.setResult(Chinese.chinese(request.getParameter("result")).trim());
  76.     dao.updateMember(memberForm);
  77.     request.setAttribute("success", "修改成功");
  78.     return mapping.findForward("operationMember");
  79.   }
  80.   //前台查询会员的属性
  81.   public ActionForward selectOneMemberHead(ActionMapping mapping,
  82.                                            ActionForm form,
  83.                                            HttpServletRequest request,
  84.                                            HttpServletResponse response) {
  85.     request.setAttribute("form",
  86.                          dao.selectOneMember(Integer.valueOf(request.
  87.         getParameter("id"))));
  88.     return mapping.findForward("selectOneMemberHead");
  89.   }
  90.   //删除操作
  91.   public ActionForward deleteMember(ActionMapping mapping,
  92.                                     ActionForm form,
  93.                                     HttpServletRequest request,
  94.                                     HttpServletResponse response) {
  95.     if (dao.deleteMember(Integer.valueOf(request.getParameter("id")))) {
  96.       request.setAttribute("success", "删除会员信息成功!!!");
  97.     }
  98.     else {
  99.       request.setAttribute("success", "删除失败,请先删除子表中的内容!!!");
  100.     }
  101.     return mapping.findForward("deleteMember");
  102.   }
  103. //查看会员详细信息
  104.   public ActionForward selectOneMember(ActionMapping mapping,
  105.                                        ActionForm form,
  106.                                        HttpServletRequest request,
  107.                                        HttpServletResponse response) {
  108.     request.setAttribute("form",
  109.                          dao.selectOneMember(Integer.valueOf(request.
  110.         getParameter("id"))));
  111.     return mapping.findForward("selectOneMember");
  112.   }
  113. //查看会员信息
  114.   public ActionForward selectMember(ActionMapping mapping,
  115.                                     ActionForm form,
  116.                                     HttpServletRequest request,
  117.                                     HttpServletResponse response) {
  118.     List list = dao.selectMember();
  119.     int pageNumber = list.size(); //计算出有多少条记录
  120.     int maxPage = pageNumber; //计算有多少页数
  121.     String number = request.getParameter("i");
  122.     if (maxPage % 6 == 0) {
  123.       maxPage = maxPage / 6;
  124.     }
  125.     else {
  126.       maxPage = maxPage / 6 + 1;
  127.     }
  128.     if (number == null) {
  129.       number = "0";
  130.     }
  131.     request.setAttribute("number", String.valueOf(number));
  132.     request.setAttribute("maxPage", String.valueOf(maxPage));
  133.     request.setAttribute("pageNumber", String.valueOf(pageNumber));
  134.     request.setAttribute("list", list);
  135.     return mapping.findForward("selectMember");
  136.   }
  137. //会员登录
  138.   public ActionForward checkMember(ActionMapping mapping,
  139.                                    ActionForm form,
  140.                                    HttpServletRequest request,
  141.                                    HttpServletResponse response) {
  142.     String name = Chinese.chinese(request.getParameter("name"));
  143.     String password = dao.selectPassword(name.trim());
  144.     //-----------------------------------------------------
  145.     boolean flag = true;
  146.     session = request.getSession();
  147.     MemberList member = MemberList.getInstance();
  148.     Vector vc = member.getList();
  149.     MemberForm memberForm = dao.selectMemberForm(name);
  150.     if (!vc.isEmpty() && vc != null) {
  151.       Enumeration en = vc.elements();
  152.       while (en.hasMoreElements()) {
  153.         MemberForm admin = (MemberForm) en.nextElement();
  154.         if (admin.getName().equals(name)) {
  155.           flag = false; //会员在线
  156.         }
  157.       }
  158.     }
  159.     else {
  160.       flag = true;
  161.     }
  162.     if (password.equals("")) {
  163.       request.setAttribute("result", "不存在此会员,请重新登录!!!");
  164.     }
  165.     else if (!password.equals(Chinese.chinese(request.getParameter("password").
  166.                                               trim()))) {
  167.       request.setAttribute("result", "密码错误,请重新登录!!!");
  168.     }
  169.     else if (!flag) {
  170.       request.setAttribute("result", "该会员已经登录!!!");
  171.     }
  172.     else {
  173.       int id = memberForm.getId().intValue();
  174.       ManagerTrace managerTrace = new ManagerTrace();
  175.       managerTrace.setId(id);
  176.       member.addMember(memberForm);
  177.       session.setAttribute("managerTrace", managerTrace);
  178.       session.setAttribute("id", String.valueOf(id));
  179.       request.setAttribute("id", String.valueOf(id));
  180.       request.setAttribute("memberForm", memberForm);
  181.     }
  182.     return mapping.findForward("checkMember");
  183.   }
  184. //添加员工信息
  185.   public ActionForward insertMember(ActionMapping mapping,
  186.                                     ActionForm form,
  187.                                     HttpServletRequest request,
  188.                                     HttpServletResponse response) {
  189.     MemberForm memberForm = (MemberForm) form;
  190.     String name = Chinese.chinese(request.getParameter("name")).trim();
  191.     if (dao.selectPassword(name) == null || dao.selectPassword(name).equals("")) {
  192.       memberForm.setName(name);
  193.       memberForm.setPassword(Chinese.chinese(request.getParameter("password")).
  194.                              trim());
  195.       memberForm.setAge(Integer.valueOf(request.getParameter("age").trim()));
  196.       memberForm.setEmail(request.getParameter("email"));
  197.       memberForm.setReallyName(Chinese.chinese(request.getParameter(
  198.           "reallyName")).trim());
  199.       memberForm.setProfession(Chinese.chinese(request.getParameter(
  200.           "profession")).trim());
  201.       memberForm.setQuestion(Chinese.chinese(request.getParameter("question")).
  202.                              trim());
  203.       memberForm.setResult(Chinese.chinese(request.getParameter("result")).trim());
  204.       dao.insertMember(memberForm);
  205.       request.setAttribute("success", "注册成功");
  206.     }
  207.     else {
  208.       request.setAttribute("success", "该会员名称已经存在!!!");
  209.     }
  210.     return mapping.findForward("operationMember");
  211.   }
  212. }