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

📄 loginaction.java

📁 这是《Struts网络编程实例》一书zhogn 的源代码部分
💻 JAVA
字号:
package org.ithinking.strutsExample.Action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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 org.ithinking.strutsExample.ActionForm.LoginActionForm;
import org.ithinking.strutsExample.entity.Userinfo;
import org.ithinking.strutsExample.service.ServiceFactory;
import org.ithinking.strutsExample.service.UserInfoService;
import org.ithinking.strutsExample.util.SiteContance;
import org.ithinking.utils.MessageDigestUtil;
import org.ithinking.utils.StringUtil;


public class LoginAction extends Action {
	 public ActionForward execute(ActionMapping actionMapping,
             ActionForm actionForm,
             HttpServletRequest httpServletRequest,
             HttpServletResponse httpServletResponse) {
		 
		 LoginActionForm form=(LoginActionForm)actionForm;
		 //如果提交的ActionForm为空,返回错误信息
		 if(form==null)
		 {
			 ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.dataerrors"));
				saveErrors(httpServletRequest, errors);
				return new ActionForward(actionMapping.getInput());
		 }
		 //如果登录id或密码为空,返回错误信息
		 if(form.getUserLoginid()==null||form.getUserLoginid().trim().length()<=0||form.getPassword()==null||form.getPassword().trim().length()<=0)
		 {
			 ActionErrors errors = new ActionErrors();
			 if(form.getUserLoginid()==null||form.getUserLoginid().trim().length()<=0)
			 {
				 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
					"global.userId.emptyerror"));
			     saveErrors(httpServletRequest, errors);
			 }
			 if(form.getPassword()==null||form.getPassword().trim().length()<=0)
			 {
				 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
					"global.userpassword.emptyerror"));
			     saveErrors(httpServletRequest, errors);
			 }
			 return new ActionForward(actionMapping.getInput());
		 }
		 
		 UserInfoService userInfoservice=ServiceFactory.getUserinfService();
		 Userinfo userInfo=userInfoservice.getUserInfoByLoginId(form.getUserLoginid());
		 
		 //如果登录id不存在
		 if(userInfo==null)
		 {
			 ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.errors.usernotexit"));
				saveErrors(httpServletRequest, errors);
				return new ActionForward(actionMapping.getInput());
		 }
		 //如果数据库中的用户密码不存在
		 if(userInfo.getPassword()==null||userInfo.getPassword().trim().length()<=0)
		 {
			    ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.userPasswrod.databaseerror"));
				saveErrors(httpServletRequest, errors);
				return new ActionForward(actionMapping.getInput());
		 }
		 
		 MessageDigestUtil messageDigest=new MessageDigestUtil(MessageDigestUtil.MD5);
		 String passwordMD5=StringUtil.convertBytesToString(messageDigest.computeDigest(form.getPassword()));
		//如果信息摘要失败
		 if(passwordMD5==null||passwordMD5.length()<=0)
		 {
			    ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.userPasswrod.digesterror"));
				saveErrors(httpServletRequest, errors);
				return new ActionForward(actionMapping.getInput());
		 }
		 //如果密码不匹配
		 if(!userInfo.getPassword().equals(passwordMD5))
		 {
			 ActionErrors errors = new ActionErrors();
				errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(
						"global.userinfo.passworderror"));
				saveErrors(httpServletRequest, errors);
				return new ActionForward(actionMapping.getInput());
		 }
		 httpServletRequest.getSession().setAttribute(SiteContance.CURRENT_USER,userInfo);
		 return actionMapping.findForward("success");
		 
	 }

}

⌨️ 快捷键说明

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