关于面试问到ajax与SSM框架,登陆验证
不比比,直接上代码
JSP页面,
<body>
<form action="" method="post" >
<input name="username" type="text" id="username" />
<input name="password" type="password" id="password" />
<button id="login" onclick="requestJson()" type="button">登录</button>
</form>
<script type="text/javascript">
function requestJson(){
var name =$("#username").val();
var pwd =$("#password").val();
if(name==""){
alert("用户名不能为空!");
return false;
}
else if(pwd==""){
alert("密码不能为空!");
return false;
}else{
$.ajax({
type:'post',
url:'login.do',
dataType:"json",//注意使用的是打他dataType,而不是Content-Type
async: true,
data:{username:name,password:pwd},
success:function(data){
if(data==null){
alert("没有这个用户!")
}
else{
alert("登录成功")
window.location.href ="index.jsp";
}
}
});
}
}
</script>
</body>
Controlle层
@RequestMapping("login")
@ResponseBody
public User loginto(User user) {
System.out.println("username:"+user.getUsername());
System.out.println("password:"+user.getPassword());
User users=userservice.login(user.getUsername(), user.getPassword());
System.out.println(users.getUsername());
return users;
}
}