步骤:
1. js文件:
this.bIsExist = false;//判断是否已存在的标志
{
xtype : 'textfield',
fieldLabel : '合同号',
name : 'RegisterField_contractNum',
value:contractNum,
labelStyle : 'text-align:right;',
validator : this.checkUserName,//validator为自定义校验函数属性
invalidText : '该合同号已存在!',
allowBlank : false
}
/**
* 功能:验证合同号是否已存在
*/
,checkIsExist : function() {
//定义变量值
var number = this.ownerCt.find('name','RegisterField_contractNum')[0].getValue();//获取textfield的name属性值
/*-------------- 发送请求----------------*/
Ext.Ajax.request({
url : 'ContractAction_checkExist.do',
params : {
contractNum : number
},
method : 'POST',
scope : this.ownerCt,
success : function(response, options) {
var responseArray = Ext.util.JSON
.decode(response.responseText);
if (responseArray.success == true) // 合同号已经被使用
this.bIsExist = false;// 给变量赋值
else
// 合同号可以使用
this.bIsExist = true;// 给变量赋值
}
});
/*-------------/ 发送请求----------------*/
return this.ownerCt.bIsExist;
}
2. Action中:
/**检查合同号是否已经存在
*@author William
*@return String
* */
public String checkExist(){
super.setContentType(super.JSON);
String contractNum = super.getRequest().getParameter("contractNum"); // 获取合同号
//验证用户是否存在
try {
int id = contractService.sContractIdByInfo(contractNum);
if( 0 < id )
super.outPrint("{success:true}");
else
super.outPrint("{success:false}");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "json";
}
3. Service中:
/**根据合同号查询合同是否已经存在
* @author William
* @return id 如果存在则返回合同id, 如果不存在则返回 0
* @param String 合同号
* */
public int sContractIdByInfo(String contractNum ) throws SQLException{
return contractDao.sContractIdByInfo(contractNum);
}
4. Dao中:
/**根据合同号查询合同是否已经存在
* @author William
* @return id 如果存在则返回合同id, 如果不存在则返回 0
* @param String 合同号
* */
public int sContractIdByInfo(String contractNum ) throws SQLException{
int id = 0;
Object oReturn = sqlMapClient.queryForObject("Contract.sContractIdByInfo",contractNum);
if( null != oReturn ){
id = (Integer)oReturn;
}
return id;
}
5. xml中:
<!-- 根据合同号 查询合同是否存在 -->
<select id="sContractIdByInfo" resultClass="int" parameterClass="string" >
<![CDATA[ SELECT id FROM sd_contract WHERE num = #contractNum# ]]>
</select>