我的Eclipse动态Web项目结构如下:
网页内容:
- CSS
- (使用的各种CSS文件)
- JavaScript
- (JS 文件)
- 页面
- Page1.jsp
- Page2.jsp
- 元信息
- WEB-INF
- 库
- web.xml
我在 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/
文件夹中,Ajax 验证调用不会发生,即使下一页的操作会发生。
当我尝试访问 Tomcat 服务器中的 struts 类时,我认为这是因为 URL 不匹配,如下 URL 有效:
http://localhost:8080/AjaxTest/ajaxVal/LnameVal
但是,这并没有:
http://localhost:8080/AjaxTest/Pages/ajaxVal/LnameVal
我什至更改了我的 ajaxAction
中的命名空间封装在 struts.xml
至/Pages/ajaxVal
,但没有骰子。
最佳答案
您正在使用相对 URL 进行验证操作:
ajaxFormValidationURL : 'ajaxVal/FormVal'
您应该使用 <s:url>
标记或使路径成为绝对路径。在你移动它之前它一直有效,这提供了最大的线索;当您开始移动内容时,相对 URL 本质上是脆弱的。
IMO 手动编码 URL 有点脆弱,这是原因之一。
不相关,但我可能会设置 "valaction"
将命名空间封装为"/"
.
关于java - Struts 2 中的 jQuery 验证引擎无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18635934/