简单的spring mvc 异步登陆验证

login.jsp页面表单

<form name="oForm" method="post" action="login.do?method=validate">
    <table>	
	<span>用户名:</span><input name="staffName" type="text" class="inputbox2" />
	<span>密 码:</span><input  name="password" type="password" class="inputbox2" />
	<input type="button" onclick="formSubmit()" id="loginbtn" >登 &nbsp;录</a></div></form>			

   login.jsp页面js

 function formSubmit() {
	var staffName = document.oForm.staffName.value;
	var password = document.oForm.password.value;
	if(staffName=="" || password ==""){
		alert("登陆账号和密码不能为空");
		return false;
	}	
//异步登录验证
    $.ajax({
            url:"login.do?method=check&staffName="+staffName+"&password="+password+"&" + "rd="+Math.random(),
            type:"post",
            success: function(response){	
   	    if(response=="false"){
   		alert("您输入的帐号或密码错误!");
   		return false;
	     }
   	     if(response=="true"){
  	        document.oForm.submit();
   	      }
	  }
        });
}
//loginController.java
public ModelAndView check(HttpServletRequest request
			,HttpServletResponse response) throws Exception{
		request.getSession().removeAttribute(STAFF_SESSION_NAME);
		PrintWriter out = response.getWriter();
		String staffName = request.getParameter("staffName");
		String password = request.getParameter("password");
		String hql = "FROM Staff WHERE loginName='"+StringUtils.sqlFormat(staffName)+"' AND loginPwd='"+StringUtils.sqlFormat(password)+"'";		
		List<Staff> staffList = staffDao.find(hql);
		if (staffList == null || staffList.size() == 0) {
			out.print("false");
			return null;
		}else{
			out.print("true");
			return null;
		}	
	
	}

你可能感兴趣的:(简单的spring mvc 异步登陆验证)