界面(jsp):
$(function() {
//失去焦点后跟数据库查询
$("#t_id").blur(function() { //失去焦点
$.ajax({
type: "post",
url: "${pageContext.request.contextPath}/taskmanage!query.action",
data: {'t_id' : $('#t_id').val()},
dataType: "json",
success: function(msg) {
alert(msg.tid);
$('#t_id_msg').html(msg.tid);
}
});
});
});
<input type="text" name="t_id" id="t_id" />
<span id="t_id_msg"></span>
action中:
//新增任务时查询任务id是否存在
private Map<String,String> info = new HashMap<String,String>();
public void query() {
try {
info = (Map<String, String>) this.daoHelper.getSqlMapClientTemplate().queryForObject("taskmanageMap.info", this);
if (null != info) {
JSONObject json = new JSONObject();
json.put("tid", "此id已存在");
response.getWriter().print(json);
} else {
JSONObject json = new JSONObject();
json.put("tid", "可以用此id");
response.getWriter().print(json);
}
} catch (Exception e) {
e.printStackTrace();
}
}
数据库中:
select T_ID as "t_id"
from TASK where t_id=#t_id#