AdminAction.java
上传用户:yunfeng252
上传日期:2022-07-23
资源大小:12403k
文件大小:4k
- package org.yufeng.struts.action;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.List;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.servlet.http.HttpSession;
- import org.apache.struts.action.ActionForm;
- import org.apache.struts.action.ActionForward;
- import org.apache.struts.action.ActionMapping;
- import org.yufeng.domains.Admin;
- import org.yufeng.service.IAdminServiceDao;
- import org.yufeng.utils.*;
- public class AdminAction extends BaseAction {
- IAdminServiceDao adminService = (IAdminServiceDao) this.getAdminService();
- Admin admin = new Admin();
- public ActionForward admin_show(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- HttpSession session = request.getSession();
- List admin_list = this.getAdminService().browseAdmin();
- session.setAttribute("admin_list", admin_list);
- return mapping.findForward("admin");
- }
- public ActionForward loginIn(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- HttpSession session = request.getSession();
- String tourl = "toAdmin";
- String username = request.getParameter("username");
- String password = request.getParameter("password");
- if (getAdminService().validate(username, password)) {
- tourl = "toAdmin";
- session.setAttribute("AdminLock", "AdminLock");
- }
- return mapping.findForward(tourl);
- }
- public ActionForward loginOut(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- HttpSession hs = request.getSession();
- PrintWriter out;
- try {
- out = response.getWriter();
- if (hs.getAttribute("AdminLock") != null)
- {
- hs.removeAttribute("AdminLock");
- out.print("<script language="javascript">alert("退出登录!");window.location.href='admin_login.jsp';</script>");
- }
- out.print("<script language="javascript">alert("退出登录!");window.location.href='admin_login.jsp';</script>");
- } catch (Exception e) {
- e.printStackTrace();
- }
- return mapping.findForward("login");
- }
- public ActionForward admin_manage(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- Admin admin = null;
- HttpSession session = request.getSession();
- String operate = request.getParameter("operate");
- String url = "admin_manage";
- if ("add".equals(operate)) {
- String username = request.getParameter("Username");
- admin = this.getAdminService().loadAdmin(username);
- if (admin != null) {
- session.setAttribute("message", "操作失败,用户账号[" + username
- + "]以存在");
- } else {
- admin = new Admin();
- admin.setUsername(username);
- admin.setPassword(Tools.md5(request.getParameter("Password")));
- admin.setAdd_date(Tools.getDataByMD(Tools.getData()));
- admin.setLast_login_date(Tools.getData());
- admin.setLast_login_ip(request.getRemoteAddr());
- adminService.addAdmin(admin);
- session.setAttribute("message", "操作成功,用户账号["
- + admin.getUsername() + "]创建成功!");
- }
- } else if ("update".equals(operate)) {
- String username = request.getParameter("Username");
- String password = Tools.md5(request.getParameter("Password"));
- admin = this.getAdminService().loadAdmin(username);
- // 数据库里没有这个账号
- if (admin == null) {
- session.setAttribute("message", "操作失败,用户账号[" + username
- + "]不存在");
- } else {
- admin.setPassword(password);
- this.getAdminService().updateAdmin(admin);
- session.setAttribute("message", "操作成功,用户账号["
- + admin.getUsername() + "]的修改完成");
- }
- }
- return mapping.findForward(url);
- }
- public ActionForward delete(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response) {
- String[] idss = request.getParameterValues("ManagerID");
- for (int i = 0; i < idss.length; i++) {
- admin.setId(new Integer(idss[i]));
- this.getAdminService().delAdmin(admin);
- this.admin_show(mapping, form, request, response);
- }
- return mapping.findForward("admin");
- }
- }