最近接触到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;
}
}