1

我的 Eclipse 动态 Web 项目结构如下:

网页内容:

  • CSS
    • (使用了各种css文件)
  • JavaScript
    • (JS文件)
  • 页面
    • Page1.jsp
    • Page2.jsp
  • 元信息
  • 网络信息
    • web.xml

我使用 jQuery 验证引擎进行前端和 Ajax 验证page1.jsp,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/文件夹中,Ajax 验证调用就不会发生,即使对下一页的操作确实如此.

我认为这是因为 URL 不匹配,当我尝试访问我的 tomcat 服务器中的 struts 类时,因为以下 URL 有效:

http://localhost:8080/AjaxTest/ajaxVal/LnameVal

然而,这并没有:

http://localhost:8080/AjaxTest/Pages/ajaxVal/LnameVal 

我什至将ajaxAction包中的命名空间更改struts.xml/Pages/ajaxVal,但没有骰子。

4

2 回答 2

1

您正在使用相对 URL 进行验证操作:

ajaxFormValidationURL : 'ajaxVal/FormVal'

您应该使用<s:url>标签或将路径设为绝对路径。它一直有效,直到你移动它提供了最大的线索;当您开始四处移动时,相对 URL 本质上是脆弱的。

IMO 手工编码 URL 有点脆弱,这也是原因之一。

不相关,但我可能会将"valaction"包命名空间设置为"/".

于 2013-09-05T13:31:11.477 回答
0

要提交到动作,需要放置动作 URL。使用 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"/>',

请记住始终使用 URL 标签来呈现 URL。

于 2013-09-05T12:42:23.067 回答