html
<form:input id="name" path="name" htmlEscape="false" maxlength="100" class="form-control required " readonly="" />
javascript
$(document).ready(function() {
$("#inputForm").validate({
rules:{
name:{
remote:{
type : "POST",
url : "${ctx}/*****/*****/*****",
data : {name : function() {return $('#name').val();}}
},
},
},
messages:{
name:{
remote:"已经存在",
}
},
submitHandler: function(form){
loading('正在提交,请稍等...');
form.submit();
},
errorContainer: "#messageBox",
errorPlacement: function(error, element) {
$("#messageBox").text("输入有误,请先更正。");
if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-group")){
error.appendTo(element.parent().parent());
} else {
error.insertAfter(element);
}
}
});
});
controller
@RequestMapping(value = "*****")
public String name(User user,HttpServletResponse response) {
List<User> list = userService.findNameRepeat(User);
if(list.size()>0){
log.info("已经存在!");
return renderString(response, "false", "");
}else{
log.info("可以使用!");
return renderString(response, "true", "");
}
}
protected String renderString(HttpServletResponse response, String string, String type) {
try {
response.reset();
response.setContentType(type);
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
return null;
} catch (IOException e) {
return null;
}
}