我的eclipse Dynamic Web Project结构如下:
WebContent:
我在page1.jsp
中使用jQuery验证引擎进行前端和Ajax验证,Ajax部分调用一个动作类,然后验证数据。
page1.jsp
<script>
$(document).ready (function ()
{
$("#subform").validationEngine('attach', {
autoHidePrompt : true ,
autoHideDelay : 5000,
ajaxFormValidation : true,
ajaxFormValidationURL : 'ajaxVal/FormVal',
ajaxFormValidationMethod : 'post',
onBeforeAjaxFormValidation : function (form, options) {
console.log ("before ajax validation in function");
form.validationEngine ('hideAll');
$('#txtlname').validationEngine ('showPrompt', 'Validating your name please wait', 'load', true);
return true;
},
onAjaxFormComplete : function (status, form, json, option) {
console.log ("ajax validation done");
console.log ("status: " + status);
console.log ("data returned " + json);
console.log ("checking status now");
if (status) {
console.log ("status good, detaching now");
form.validationEngine ('hideAll');
form.validationEngine ('detach');
form.submit ();
}
}
});
});
</script>
<title>Form Validation test</title>
</head>
<body>
<div>
<form id = "subform" action = "submitForm" method = "post"> <br><br><br>
First Name: <input id = "txtfname" name = "txtfname" type = "text" class = "validate[required]"
placeholder = "enter emp name" />
Last name: <input id = "txtlname" name = "txtlname" type = "text" placeholder = "enter emp lname"
class = "validate[required, ajax[ajaxStrutsCall]]" />
Age: <input id = "txtage" name = "txtage" type = "text" placeholder = "enter age" />
<input id ="cmdsubmit" type = "submit" name = "cmdsubmit" value = "click here" />
</form>
</div>
</body>
"last name"
输入标记的class属性中的Ajax调用位于jquery.validationEngine-en.js
中:
"ajaxStrutsCall": {
"url": "ajaxVal/LnameVal",
"alertTextOk": "Your last name seems ok",
"alertText": "Bad Last name",
"alertTextLoad": "Validating, please wait"
},
我的struts.xml
:
<struts>
<constant name = "struts.devMode" value = "true" />
<package name = "strutsaction" extends = "struts-default">
<action name = "submitForm" class = "validation.action.JqueryAction" method = "execute">
<result name = "success">/Pages/page2.jsp</result>
</action>
</package>
<package name = "ajaxAction" namespace = "/ajaxVal" extends = "json-default">
<action name = "LnameVal" class = "validation.struts.AjaxStrutsAction" method = "execute">
<result name = "success" type = "json" />
</action>
<action name = "FormVal" class = "ajax.form.validator.FormValidation" method = "execute">
<result name = "success" type = "json" />
</action>
</package>
现在,当page1.jsp
和page2.jsp
是一个文件夹时,即WebContent
,只要我将它们添加到Pages/
,所有代码都能正常运行{1}}文件夹,即使执行到下一页的操作,也不会发生Ajax验证调用。
我认为这是因为当我尝试访问我的tomcat服务器中的struts类时,URL不匹配,因为以下URL工作:
http://localhost:8080/AjaxTest/ajaxVal/LnameVal
但是,这不是:
http://localhost:8080/AjaxTest/Pages/ajaxVal/LnameVal
我甚至将ajaxAction
中的struts.xml
包中的名称空间更改为/Pages/ajaxVal
,但没有骰子。
答案 0 :(得分:1)
您正在使用相对网址进行验证操作:
ajaxFormValidationURL : 'ajaxVal/FormVal'
您应该使用<s:url>
标记或将路径设为绝对路径。它一直有效直到你移动它提供了最大的线索;当你开始移动东西时,相对URL本质上是脆弱的。
IMO手工编码网址有点脆弱,这也是原因之一。
不相关,但我可能会将"valaction"
包名称空间设置为"/"
。
答案 1 :(得分:0)
要提交操作,需要输入操作网址。使用Struts标签需要taglib定义
<%@ taglib prefix="s" uri="/struts-tags" %>
将表单标记更改为
<form id="subform" action="<s:url action='submitForm'/>" method="POST">
您应该编写以获取验证URL的URL
ajaxFormValidationURL : '<s:url namespace="/ajaxVal" action="FormVal"/>',
请记住始终使用网址标记来呈现网址。