ssm的搭建见上一章
1.数据协议层
public User selectByLoginnameAndPassword(@Param("loginname")String loginname,@Param("password") String password);
2.服务协议层
User login(String loginname,String password);
3.服务层
@Autowired
private UserDao userDao;
public User login(String loginname, String password) {
return userDao.selectByLoginnameAndPassword(loginname, password);
}
4.数据实现层(mybatis)
<mapper namespace="com.yongxing.manager.dao.UserDao"><select id="selectByLoginnameAndPassword" resultType="com.yongxing.manager.domain.User" >
select * from user_inf where loginname=#{loginname} and password=#{password}
</select>
5.控制层
@Controller
public class UserController {
@Autowired
private UserService userServcie;
@RequestMapping("/checklogin")
@ResponseBody
public User checklogin( String loginname,String paasword,HttpSession session){
System.out.println(loginname);
User user =userServcie.login(loginname, paasword);
if(user!=null) {
session.setAttribute("user_session", user);
return user;
}else {
return null;
}
}
后台搭建结束
6.前台搭建
<form>
<input name="loginname" type="text" id="loginname"/ >
<input name="password" type="password" id="password"/>
<input value="登录" style="width: 100%;" type="button" onclick="requestJson()" >
</form>
ajax异步登陆
<script type="text/javascript">
function requestJson(){
var name =$("#loginname").val();//#是id选择器
var pwd =$("#password").val();
if(name==""){
alert("用户名不能为空!");
return false;
}
else if(pwd==" "){
alert("密码不能为空!");
return false;
}
else
{
$.ajax({
type:'post',
url:'checklogin',
dataType:"json",//注意使用的是打他dataType,而不是Content-Type
async: true,
data:{loginname:name,paasword:pwd},
success:function(data){
if(data==null){
alert("没有这个用户!")
}
else{
alert("登录成功")
window.location.href ="main";
}
}
});
}
}
</script>