jquery ajax验证用户名是否存在(后台spring mvc)

controller层

@ResponseBody
@RequestMapping(value = "/user/isExist", produces = "application/json") 
public boolean isExist(Model model,String userName) {
	
	boolean flag = userPrivilegeServiceFacade
			.isExistUserName(userName);
	if(flag) {
		return true;
	}
	return false;
}


jsp页面

<form:form class="form-horizontal" id="form" method="post" modelAttribute="user" 
         enctype="multipart/form-data" >
         <div class="control-group">
   			<form:label path="userName" class="control-label">
   				<spring:message code="system.user.userName" />
   			</form:label>
   			<div class="controls">
   				<form:input path="userName" οnblur="checkIsExist();" οnfοcus="clearCss();" />
   				<site:required />
   				<span id="showResult"></span>
				<form-extension:errors path="userName" cssClass="error" />
   			</div>
        </div>
</form:form>


<script type="text/javascript">
	function checkIsExist() {
	    var userName = $.trim($("#userName").val());
	    $.ajax({
	        type:"POST",   //http请求方式
	        url:"<spring:url value="/system/userprivilege/user/isExist" />", //发送给服务器的url
	        data:"userName="+userName, //发送给服务器的参数
	        dataType:"json",  //告诉JQUERY返回的数据格式(注意此处数据格式一定要与提交的controller返回的数据格式一致,不然不会调用回调函数complete)
	        complete:function(msg) {
				if (eval("(" + msg.responseText + ")")) {
					$("#showResult").html("<font color='red'>用户名已存在</font>");
				} else {
					$("#showResult").html("");
				}
	        }//定义交互完成,并且服务器正确返回数据时调用回调函数 
	    });
	}
   function clearCss() {
	   $("#showResult").html("");  
   }	
</script>

错误的例子(controller层)


@ResponseBody
@RequestMapping(value = "/user/isExist", produces = "application/json") 
public boolean isExist(Model model,String userName) {
	
	boolean flag = userPrivilegeServiceFacade
			 .isExistUserName(userName);
		
	return flag;
}

优化的例子


controller层

@ResponseBody
@RequestMapping(value = "/user/isExist", produces = "application/json") 
public ResultDTO isExist(Model model,String userName) {
	    
	ResultDTO result = new ResultDTO();
		 
	boolean success = userPrivilegeServiceFacade
				 .isExistUserName(userName);
		
	result.setSuccess(success);
		
	if(success) {
		result.setMessage("<font color='red'>用户名已存在</font>");
	} else {
		result.setMessage("<font color='green'>用户名合法</font>");
	}
	return result;
}

domain层


public class ResultDTO implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private  boolean success;
	private  String message;
	public boolean isSuccess() {
		return success;
	}
	public void setSuccess(boolean success) {
		this.success = success;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}

}

jsp页面

<script type="text/javascript">

	function checkIsExist() {
	    var userName = $.trim($("#userName").val());
	    $.ajax({
	        type:"POST",   //http请求方式
	        url:"<spring:url value="/system/userprivilege/user/isExist" />", //发送给服务器的url
	        data:"userName="+userName, //发送给服务器的参数
	        dataType:"json",  //告诉JQUERY返回的数据格式(注意此处数据格式一定要与提交的controller返回的数据格式一致,不然不会调用回调函数complete)
	        complete : function(msg) {
	          var result = eval("(" + msg.responseText + ")");
	    	  if(result.success) {
	    		  $("#showResult").html(result.message);
				} else {
					$("#showResult").html(result.message);
				} 
	       } 
	    });
	}
   function clearCss() {
	   $("#showResult").html("");  
   }	
</script>



评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值