JSP Struts 2中的jQuery验证引擎不工作

webghufk  于 2023-10-14  发布在  jQuery
关注(0)|答案(2)|浏览(130)

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

网站内容:

  • CSS
  • (使用各种css文件)
  • JavaScript
  • (JS文件)
  • Page1.jsp
  • Page2.jsp
  • META-INF
  • WEB-INF
  • lib
  • web.xml

我在page1.jsp中使用jQuery验证引擎进行前端和Ajax验证,Ajax部分调用一个action类,然后验证数据。

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>

jquery.validationEngine-en.js中,"last name"输入标记的class属性中的Ajax调用是:

"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

我甚至将struts.xml中的ajaxAction包的名称空间更改为/Pages/ajaxVal,但没有成功。

rvpgvaaj

rvpgvaaj1#

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

ajaxFormValidationURL : 'ajaxVal/FormVal'

您应该使用<s:url>标记或使路径成为绝对路径。它工作,直到你移动它提供了最大的线索;当你开始移动东西的时候,相对URL本身就很脆弱。
IMO手工编码的URL有点脆弱,这是原因之一。
不相关,但我可能会将"valaction"包命名空间设置为"/"

3htmauhk

3htmauhk2#

要提交操作,需要放置操作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。

相关问题