Struts 2中的jQuery验证引擎无法正常工作

时间:2013-09-05 12:08:43

标签: java ajax jsp struts2 jquery-validation-engine

我的eclipse Dynamic Web Project结构如下:

WebContent:

  • CSS
    • (使用各种css文件)
  • 的JavaScript
    • (JS档案)

    • Page1.jsp
    • Page2.jsp
  • META-INF
  • WEB-INF
    • lib
    • 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.jsppage2.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,但没有骰子。

2 个答案:

答案 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"/>',

请记住始终使用网址标记来呈现网址。