Ajax Jsp+jquery+struts2 详细注册ajax验证

Ajax  Jsp+jquery+struts2 详细注册ajax验证

Dao层




/**
*
* 查询数据库中信息是否重复
*/
@Override
public boolean isExist(String hql) {
Query queryObject = getSession().createQuery(hql);
return queryObject.list().size() > 0 ? false : true;
}

 
Action中
[code=”java”]
private String msg;//提示信息
//msg以及Service get set方法省略

/**
* ajax检查数据库中所需信息是否重复
*/
public void checkCord() {
String cord = this.getRequest().getParameter("Cord");
try {
//使用hql语句查询数据库 检查是否重复
boolean flag= membercardService.isExist("from Membercard where cord='"+cord+"'");
if(flag){
this.getResponse().getWriter().write("true");
}else{
this.getResponse().getWriter().write("");
}
} catch (Exception e) {
e.printStackTrace();
}
}
protected HttpServletRequest getRequest() {
return ServletActionContext.getRequest();
}

protected HttpServletResponse getResponse() {
return ServletActionContext.getResponse();
}
  

Jsp页面


[code=”html”]
<script type="text/javascript" src="${contextPath}/js/jquery-1.3.2.js"></script>

<script type="text/javascript">

//检查会员帐号是否重复
function checkCord() {
var job = document.getElementById("cord").value;
if (job.length <= 0) {
alert("会员卡号不能为空!");
//this.foucs();
return false;
} else {
$.ajax({
url :这里是需要跳转的Action地址,
type : 'post',
async : 'false',
data : {
'Cord' : $('#cord').val()
},
success : function(msg) {
if(msg){
alert("会员卡号可以注册!");
return true;
                    }else{
                    alert("会员卡号已被注册!");
                    $('#cord').val("");
                    $('#cord').focus();
return false;
                    }
},
error : function() {
alert("程序出现错误!");
}
});
}
}

</script>
<tr height="35">
<td width="172">会员卡卡号</td>
<td class="last"><input type="text"
id="cord" onblur="checkCord()"
name="memberc.cord" check="^\S{1,30}$" warning="会员卡卡号不能为空长度为1-30之间!"/>
</td>
</tr>
  


你可能感兴趣的:(struts2)