求一段JAVA代码要求如下 获取输入的字段和数据库中的比较如果没有就把该字段存入数据库 数据库 STUDENT
1个回答
展开全部
这个是重复的验证,你可以在jsp页面通过Ajax,将输入的值传到后台Action,在Action里把该值放入查询语句到数据库进行查询,进行重复判断。
js代码如下:
var f = document.forms[0];
$("#role_name").blur(function(){
if(null == f.role_name.value){
return ;
}
$.ajax({
type: "POST",
url: "RoleInfo.do",
data: "method=validateRolename&id=${af.map.id}&role_name=" + f.role_name.value,
dataType: "json",
error: function(request, settings) {},
success: function(oper) {
if(oper.result){
$("#role_name").val("");
alert("系统已存在此角色");
f.role_name.focus();
}else {
return;
}
}
});
Action代码:
public ActionForward validateRolename(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaBean dynaBean = (DynaBean) form;
StringBuffer oper = new StringBuffer("{result:");
String role_name = (String) dynaBean.get("role_name");
//String id = (String) dynaBean.get("id");
if (!StringUtils.isBlank(role_name)) {
RoleInfo ri = new RoleInfo();
ri.setRole_name(role_name);
//if(null != id){
ri.getMap().put("id_not_in",(String) dynaBean.get("id"));
//}
ri = super.getFacade().getRoleInfoService().getRoleInfo(ri);
if (null == ri) {
oper.append(false);
} else {
oper.append(true);
}
} else {
oper.append("error");
}
oper.append("}");
super.render(response, oper.toString(), "text/x-json;charset=UTF-8");
return null;
}
js代码如下:
var f = document.forms[0];
$("#role_name").blur(function(){
if(null == f.role_name.value){
return ;
}
$.ajax({
type: "POST",
url: "RoleInfo.do",
data: "method=validateRolename&id=${af.map.id}&role_name=" + f.role_name.value,
dataType: "json",
error: function(request, settings) {},
success: function(oper) {
if(oper.result){
$("#role_name").val("");
alert("系统已存在此角色");
f.role_name.focus();
}else {
return;
}
}
});
Action代码:
public ActionForward validateRolename(ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {
DynaBean dynaBean = (DynaBean) form;
StringBuffer oper = new StringBuffer("{result:");
String role_name = (String) dynaBean.get("role_name");
//String id = (String) dynaBean.get("id");
if (!StringUtils.isBlank(role_name)) {
RoleInfo ri = new RoleInfo();
ri.setRole_name(role_name);
//if(null != id){
ri.getMap().put("id_not_in",(String) dynaBean.get("id"));
//}
ri = super.getFacade().getRoleInfoService().getRoleInfo(ri);
if (null == ri) {
oper.append(false);
} else {
oper.append(true);
}
} else {
oper.append("error");
}
oper.append("}");
super.render(response, oper.toString(), "text/x-json;charset=UTF-8");
return null;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询