簡單的spring mvc 異步登陸驗證

最近接觸到Spring+SpringMVC+Mybatis框架,在登錄驗證方面遇到了一些問題,與struts2有一些區別
1.@Resource private Service service ;非控制器中不能注入
2.不能通過ajax實現頁面跳轉(控制器中可以實現頁面跳轉,但是不能通過控制器+ajax實現頁面跳轉)
以下是異步登錄的實現,對才接觸ssm框架的新手可能有一些幫助
login.jsp頁面表單

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();
          }
      }
        });
}
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;
        }   

    }
發佈了25 篇原創文章 · 獲贊 21 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章