⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginaction.java

📁 hibernate应用测试,初学hibernate 的会员可以看看.
💻 JAVA
字号:
package com.action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

import com.dao.AcountTableDAO;
import com.bean.AcountTable;
import com.bean.AccountAndGroup;
import com.dao.AccountGroupRoleViewDAO;
import com.dao.AcountSelfRoleViewDAO;
import com.bean.AccountGroupRoleView;
import com.bean.AcountSelfRoleView;

public class LoginAction extends Action{
    public ActionForward execute(ActionMapping mapping,
                        ActionForm form,
                        HttpServletRequest request,
                        HttpServletResponse response)
            throws Exception{

        String flag = request.getParameter("flag");
        ActionErrors actionErrors = new ActionErrors();
        String target="loginManager_1";
        if(flag==null){//登陆的时候
            String acount = request.getParameter("acount");
            String pwd = request.getParameter("pwd");
            if(acount==null||pwd==null){//账号名或密码为空,不允话登陆
                ActionError actionError = new ActionError(
                        "errors.generally.failed","非法的操作");
                actionErrors.add("addGroupSuccese", actionError);
                saveErrors(request, actionErrors);
                return mapping.findForward("loginManager_1");
            }
            AcountTable acountTable = new AcountTable();
            acountTable.setAccountName(acount);
            acountTable.setPwd(pwd);
           AccountAndGroup acountAndGroup = AcountTableDAO.selectAcountAndGroupByAcountNAndP(acountTable);
           if(acountAndGroup==null){//查不到相应记录,说明账号或密码错误
               ActionError actionError = new ActionError(
                        "errors.generally.failed","用户名或密码错误");
                actionErrors.add("addGroupSuccese", actionError);
                saveErrors(request, actionErrors);
                return mapping.findForward("loginManager_1");
           }
           else{//当账号存在的时候
               if(acountAndGroup.getOpenFlag()==1){//账号已开通,允许进入系统
                   //加载权限列表并以字符数组形式放于session
                   List allGroupRole = AccountGroupRoleViewDAO.selectAccountGroupRoleViewByacountId(acountAndGroup.getAcountId());
                   List allSelfRole = AcountSelfRoleViewDAO.selectAcountSelfRoleViewByacountId(acountAndGroup.getAcountId());
                   String[] allRoleUrl = new String[allGroupRole.size()+allSelfRole.size()];
                   int i = 0;
                   for(;i<allGroupRole.size();i++){
                       AccountGroupRoleView tmp = (AccountGroupRoleView)allGroupRole.get(i);
                       allRoleUrl[i]=tmp.getUrlPath()==null?"asdfa15awe5fa6s8d@#$^&@%^@#$%!#$%ga6s5df6as":tmp.getUrlPath();
                   }
                   for(int j=0;j<allSelfRole.size();j++){
                       AcountSelfRoleView tmp = (AcountSelfRoleView)allSelfRole.get(j);
                       allRoleUrl[i+j]=tmp.getUrlPath()==null?"asdfa15awe5fa6s@$%@%%%^&%^&654sdg8rr5g#$%34":tmp.getUrlPath();
                   }
                   request.getSession().setAttribute("loginAllRoleUrl",allRoleUrl);
                   request.getSession().setAttribute("loginAcountAndGroup",acountAndGroup);
                   if(acountAndGroup.getFirstLogFlag()==0){//如未修改初始化密码,则跳转到密码修改页面
                       request.setAttribute("firstLogin","true");
                       target="modifyPwd";
                   }else
                       target="loginManager_2";
                   return mapping.findForward(target);
               }
               //账号未开通不允许进入系统
               ActionError actionError = new ActionError(
                        "errors.generally.failed","账号未开通,请与管理员联系");
                actionErrors.add("addGroupSuccese", actionError);
                saveErrors(request, actionErrors);
                return mapping.findForward("loginManager_1");
           }
        }else if(flag.equals("logout")){//退出的时候
            request.getSession().invalidate();
            return mapping.findForward("loginManager_1");
        }else if(flag.equals("selfModify")){//修改账号
            String acountName = request.getParameter("account");
            String pwd = request.getParameter("pwd");
            String acountIdStr = request.getParameter("acountId");
            try{
                int acountId = Integer.parseInt(acountIdStr);
                AcountTable acount = new AcountTable();
                acount.setAccountName(acountName);
                acount.setPwd(pwd);
                acount.setAcountId(acountId);
                acount.setFirstLogFlag(1);
                AcountTableDAO.updateNameAndPwd(acount);
                ActionError actionError = new ActionError(
                       "errors.generally.succese");
               actionErrors.add("addGroupSuccese", actionError);
               saveErrors(request, actionErrors);
               //重新设置session中的账号属性值
               AccountAndGroup acountAndGroup = (AccountAndGroup)(request.getSession().getAttribute("loginAcountAndGroup"));
               acountAndGroup.setAccountName(acount.getAccountName());
               acountAndGroup.setPwd(acount.getPwd());
               acountAndGroup.setFirstLogFlag(acount.getFirstLogFlag());
               request.getSession().setAttribute("loginAcountAndGroup",acountAndGroup);
               //初次修改账号
               String firstLogin = request.getParameter("firstLogin");
               if(firstLogin.equals("null")) target="modifyPwd";
               else if(firstLogin.equals("true")) target="loginManager_1";
               else target="loginManager_1";
            }catch(Exception e){
                e.printStackTrace();
                ActionError actionError = new ActionError(
                        "errors.generally.failed","系统出错了,请待会再试");
                actionErrors.add("addGroupSuccese", actionError);
                saveErrors(request, actionErrors);
            }
            return mapping.findForward(target);
        }
        return null;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -