formValidator向struts2 ajax传參验证

时间:2023-01-08 05:27:04

话不多说,直接上代码

页面head里面的formvalidator验证配置

  <script type="text/javascript">
$(document).ready(function() {
$.formValidator.initConfig({
formID : "form1",
theme : "ArrowSolidBox",
wideWord : false,
submitOnce : true,
onError : function(msg, obj, errorlist) {
$("#errorlist").empty();
$.map(errorlist, function(msg) {
$("#errorlist").append("<li>" + msg + "</li>")
});
alert(msg);
},
ajaxPrompt : '有数据正在异步验证,请稍等...'
}); $("#name").formValidator({
tipID : "nameTip",
onShow : "请输入角色名",
onCorrect : "正确"
}).inputValidator({
max : 10,
onError : "最多输入10个字符。"
}).regexValidator({
regExp : "notempty",
dataType : "enum",
onError : "请输入角色名"
}).ajaxValidator({
url:"sysRoleAction_isRoleNameAjax.action",
type:"post",
dataType:"html",
data:{roleName:function(){return $("#name").val()},filterName:"${name}"},
success:function(data){
if(data=="true"){
return false;
}else{
return true;
}
},
buttons: $("#button"),
error: function(){alert("server没有返回数据。可能server忙。请重试");},
onError : "该角色名已经存在!",
onWait : "正在对角色名进行合法性校验。请稍候…"
});
$("#description").formValidator({tipID : "descriptionTip",empty:true,onShow:"请输入你的描写叙述",onCorrect:"恭喜你,你输对了"}).inputValidator({max:20,onError:"最多输入20个字符"});
});
</script>

struts2 action中script验证的方法

public void isRoleNameAjax() throws IOException {
/** 文本输出流打印 */
PrintWriter out = response.getWriter();
String roleName = request.getParameter("roleName");
String filterName = request.getParameter("filterName");
boolean findByRoleName = systemRoleService.findByRoleName(roleName,filterName);
out.print(findByRoleName);
out.flush();
out.close();
}