功能逻辑实现之用户注册功能

前台页面

//用户名表单校验

function checkUsername(){
	var username=$("#username").val;
	var reg_username=正则表达式;
	var flag=reg_username.test(username);
	if(flag){
		$("#username").css("border","");
		}else{
			$("#username").css("border","1px solid red");
		}
		return flag;
}

//密码表单校验

function checkPassword(){
	var password=$("#password").val;
	var reg_password=正则表达式;
	 var flag=reg_password.test(password);
	 if(flag){
		 $("#password").css("border","");
	 }else{
		 $("#password").css("border","1px solid red");
	 }
}

//邮件表单校验

function checkEmail(){
	  var email=$("#email").val;
	  var reg_Email=正则表达式
	  var flag=reg_Email.test(email)
	  if(flag){
		  $("#email").css("border","");
	  }else{
		  $("#email").css("border","1px solid red")
	  }
}

当表单提交时去调用所有的校验方法

$(function(){
	$("registerForm").submit(function(){
		if(checkUsername()&&checkPassword()&&checkEmail()){
			//校验通过,发送ajax请求
			$.post("user/regist",$(this).serialize,function(data){
				if(data.flag){
					location.href="register_ok.html"
				}else{
					//注册失败 
					$("#errorMsg").html
				}
				
				
			});
			)
		}
		return false;
	}
	
	);
		//当某一个组件失去焦点时,调用对应的校验方法
                $("#username").blur(checkUsername);
                $("#password").blur(checkPassword);
                $("#email").blur(checkEmail);

	
})

**

服务器端代码

**
Dao层

定义两个方法
//根据用户名查找用户
public User findByUsername(String username){
	//定义sql
	User user=null;
	try{
		String sql="select * from tab_user where username=?";
		user=template.queryForObject(sql,new BeanPropertyRowMapper(User.class),username)
	}catch(DataAccessException e){
		
	}
	return user;
}
//保存用户
public void save(User user){
	//1.定义sql
	String sql="insert into tab_user(username,password,name,birthday,sex,telephone,email)"+"values(?,?,?,?,?,?,?,?)";
	//执行sql
	template.update(sql,user.getUsername(),
					user.getPassword(),
					user.getName(),
					user.getBirthday(),
					user.getSex(),
					user.getTelePhone(),
					user.getEmail());
}

service业务逻辑层
定义注册用户的方法

public boolean regist(User user){
	User u=userDao.findByUsername(user.getUsername);
	if(u!=null){
		return false;
	}else{
		userDao.save(user);
		return true;
	}
}

web层
servlet

public class registerUserServlet extends HttpServlet{
	public void doGet(HttpServletRequest req,HttpServletResonse resp) throws ServletException,IOException{
		//先获取验证码
		String check=req.getParameter("check");
		HttpSession session=req.getSession();
		String checkcode_server=(String)session.getAttribute("CHECKCODE_SERVER");
		if(check==null||!check.equalsIgnoreCase(checkcode_server)){
			ResultInfo info=new ResultInfo();
			info.setErrorMsg("验证码错误");
			info.setFlag(false);
			//将info对象序列化为json
			ObjectMapper mapper=new ObjectMapper();
			String json=mapper.writeValueAsString(info);
			//将json数据写回客户端
			resp.setContentType("application/json;charset=utf-8");
			resp.getWriter.where(json);
			return;
		}
		Map map=req.getParameterMap();
		User user=new User();
		//封装对象
		    UserService service=new UserServiceImpl();
        boolean flag = service.regist(user);
        ResultInfo info=new ResultInfo();
        if(flag){
            info.setFlag(true);
            info.setErrorMsg("注册成功");
        }else{
            info.setFlag(false);
            info.setErrorMsg("注册失败!");
        }
        ObjectMapper mapper=new ObjectMapper();
        String json = mapper.writeValueAsString(info);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
	}
	
}

你可能感兴趣的:(学习笔记,功能逻辑)