?? loginaction.java
字號(hào):
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為空,返回錯(cuò)誤信息
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或密碼為空,返回錯(cuò)誤信息
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());
}
//如果數(shù)據(jù)庫(kù)中的用戶密碼不存在
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");
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -