在后台Action中:
public class ValidateAction extends BaseActionSupport {
@Resource
private ValidateService validateService;
private String pojoName;
private String columeName;
private String contents;
private String conditon;
private String result;
/**
* result
* 返回0表示不存在了
* 返回1表示存在
* 返回2表示不能为空
* 返回3表示字段不全
*/
@JSONResult(values = {"result"}) //设置JSON
public void check(){
try {
contents = java.net.URLDecoder.decode(contents,"utf-8");//设置字符格式
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
boolean flag = validateService.isExist(pojoName, columeName, contents, conditon);
if(flag)
result = "1";
else
result = "0";
return ;
}
}
在ServiceImpl中:
继承extends PersistSupport(通用持久化操作DAO接口实现类。不公开)
public boolean isExist(String pojoName,String columeName, String contents, String conditon) {
if(contents == null || contents.trim().equals(""))
return true;
if(pojoName == null || pojoName.trim().equals(""))
return true;
if(columeName == null || columeName.trim().equals(""))
return true;
String sql = "select count(dep) from "+pojoName+" dep where dep."+columeName+" = '"+StringUtil.checkSingleQuotationMarks(contents)+"'";
if(conditon != null && !conditon.trim().equals("")){
sql = sql + conditon;
}
System.out.println("ValidateServiceImpl sql:"+sql);
Query query = em.createQuery(sql);
if(Integer.parseInt(query.getSingleResult().toString()) > 0 && !"".equals((contents.trim())))
return true;
return false;
}
}
在js代码:
/**
* 验证数据库某个pojo的属性值是否存在
* @param pojoName
* @param columeName
* @param contents
* @param oldeValue
* @param id
* @param msg
* @return
*/
function check_Data(pojoName,columeName,obj,oldeValue,id,msg){
var lastNode = obj.parentNode.childNodes[obj.parentNode.childNodes.length - 1];
var lastNode2 = obj.parentNode.childNodes[obj.parentNode.childNodes.length - 2];
if (lastNode.id == "__checkDataMessagePanel" || lastNode.id == "__ErrorMessagePanel") {
obj.parentNode.removeChild(lastNode);
}
if (lastNode2.id == "__checkDataMessagePanel"|| lastNode2.id == "__ErrorMessagePanel") {
obj.parentNode.removeChild(lastNode2);
}
var contents = obj.value;
if(contents == ""){
var e = document.createElement("span");
e.style.color = "red";
e.innerHTML="请输入"+msg+"!";
e.id="__checkDataMessagePanel";
obj.parentNode.appendChild(e);
document.getElementById(id).value="";
return ;
}
if(oldeValue != '' && oldeValue==contents){
document.getElementById(id).value="1";
return ;
}
var urlWork = "<c:url value='/validate/check.json' />";
urlWork += "?pojoName="+pojoName+"&columeName="+columeName+"&contents="+encodeURI(encodeURI(contents));
$.ajaxSettings.async = false;
var result = "";
$.getJSON(urlWork,function(data){
result = data.result;
});
if(result == 0){
var e = document.createElement("span");
e.style.color = "green";
e.id="__checkDataMessagePanel";
e.innerHTML = "此值可以使用!";
document.getElementById(id).parentNode.appendChild(e);
document.getElementById(id).value="1";
}
if(result == 1){
var e = document.createElement("span");
e.style.color = "red";
e.id="__checkDataMessagePanel";
e.innerHTML = msg+"已经存在!";
//document.getElementById(id).parentNode.appendChild(e);
obj.parentNode.appendChild(e);
document.getElementById(id).value="";
}
}
前端使用格式:
<input type="text" name="hys.mc" id="mc" value="${hys.mc}" class="input" onblur="check_Data('XtbgHysxx','mc',this,'${hys.mc}','validatemc','会议室名称');"></input>
<input type="hidden" id="validatemc" dataType="Require" msg="请输入适合的名称!" value="${hys.mc}" ></input>