vikramjit_S
vikramjit_S

Reputation: 31

jQuery validation engine in Struts 2 not working

My Eclipse Dynamic Web Project structure is as follows:

WebContent:

I'm using the jQuery validation engine for front end and Ajax validation in page1.jsp, the Ajax part calls an action class which then validates the data.

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>

the Ajax call in the class attribute of the "last name" input tag is in jquery.validationEngine-en.js it is:

"ajaxStrutsCall": {
                "url": "ajaxVal/LnameVal",
                "alertTextOk": "Your last name seems ok",
                "alertText": "Bad Last name",
                "alertTextLoad": "Validating, please wait"
            },

my 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>

Now, the thing is that all the code was working perfectly when page1.jsp and page2.jsp were one folder up, i.e. in the WebContent, as soon as I added them to the Pages/ folder, the Ajax validation calls do not happen, even though the action to the next page does.

I figured that this this is because the URL is not matching, when i tried accessing the struts class in my tomcat server as the following URL worked:

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

However, this did not:

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

I even changed the namespace in my ajaxAction package in struts.xml to /Pages/ajaxVal, but no dice.

Upvotes: 0

Views: 1437

Answers (2)

Roman C
Roman C

Reputation: 1

To submit to the action, needs to put the action URL. Using Struts tags requires taglib definition

<%@ taglib prefix="s" uri="/struts-tags" %>

change the form tag to

<form id="subform" action="<s:url action='submitForm'/>" method="POST">

the same you should write to get the url for the validation URL

ajaxFormValidationURL : '<s:url namespace="/ajaxVal" action="FormVal"/>',

remember always use URL tag to render URLs.

Upvotes: 0

Dave Newton
Dave Newton

Reputation: 160321

You're using a relative URL for the validation action:

ajaxFormValidationURL : 'ajaxVal/FormVal'

You should either use the <s:url> tag or make the paths absolute. That it worked until you moved it provides the biggest clue; relative URLs are inherently fragile when you start moving things around.

IMO hand-coding URLs is a bit brittle, and this is one of the reasons.

Unrelated, but I'd probably set the "valaction" package namespace to "/".

Upvotes: 1

Related Questions