日期:2014-05-16  浏览次数:20614 次

关于mybatis的使用之AJAX操作总结

《一》AJAX实现,对用户名唯一性的检测

<tr><td align="center">注册名</td>
<td><input type="text" name="user.userName" id="zhuceming"
style="width: 160px;" onblur="checkOnlyUser(this.value);">
</td>
</tr>
<tr class="wrong_msg nodisplay">
<td colspan="2" align="center">
<span style="color:red">&nbsp;${onlyUserWord}</span>
</td>
</tr>

?

	function checkOnlyUser(username){
		if(username == ""){
			alert("不能为空")
			return false;
		}
	$.ajax({
			type : "post",
			url : "doOnlyUser",
			beforeSend: function(XMLHttpRequest){
		            },
			data : {
				username : username ,
				random : Math.random()
			},
			success : function(data) {
				$(".wrong_msg span").html(data);
				$(".wrong_msg").show();
			}
		});
	}

?

提交方法,$(".wrong_msg span").html()可得其值

	function   saveUser(){
		var objForm = document.getElementById("addUserForm");
		if (document.getElementById("zhenshixingming").value == "") {
			alert("真实姓名不允许为空");
			return false;
		}
		if (document.getElementById("zhuceming").value == "") {
			alert("注册名也不允许为空");
			return false;
		}
		alert($(".wrong_msg span").html());
		alert($(".wrong_msg span").html());
		
		if($.trim($(".wrong_msg span").html()) == "用户名重复" ){
			return ;
			
		}
		
		if (document.getElementById("mima").value == "") {
			alert("密码也不允许为空");
			return false;
		}

		objForm.action = "doAddUser.action"; 
		objForm.method = "post";
		objForm.submit();
	}

?

返回的字符串

	/**
	 * 验证唯一
	 */
	public String onlyUser() {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			System.out.println(username);
			user = this.userService.getOnlyUser(username);
			
			if (user != null) {
				onlyUserWord = "用户名重复";
			} else {
				onlyUserWord = "恭喜你,可以注册";
			}
			out.println(onlyUserWord);
			out.flush();
			out.close();
			return "success";
		} catch (IOException e) {
			e.printStackTrace();
			return "nonono";
		}
	}

?