Question

using the jQuery validation engines built in Ajax functionality, i send a request from my last name field to my struts action class.
My HTML code is :

<script>
    $(document).ready (function ()
    {           
        $("#subform").validationEngine('attach', {
            autoHidePrompt : true , 
            autoHideDelay : 5000                
        }); 
    });
</script>

<body>

<div class = "container">                           
    <form id = "subform" action = "next" 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[ajaxStrutsCall] refers to jquery.validationEngine-en.js for the url of the struts call.
jquery.validationEngine-en.js is :

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

the url is mapped in my struts.xml file, which is :

<package name = "ajaxAction" namespace = "/ajaxVal" extends = "json-default">
    <action name = "LnameVal" class = "validation.struts.AjaxStrutsAction" method = "execute">
        <result name = "success" type = "json" />
    </action>
</package>

and finally my struts action class is :

public class AjaxStrutsAction extends ActionSupport implements ServletResponseAware, ServletRequestAware {

private HttpServletRequest request;
private HttpServletResponse response;

@Override
public void setServletRequest(HttpServletRequest request) {
    this.request = request;
}
@Override
public void setServletResponse(HttpServletResponse response) {
    this.response = response;
}

@Override
public String execute() throws Exception {

    Gson gson = new Gson ();
    PrintWriter out = null;
    String data = request.getParameter("txtlname");
    System.out.println ("in struts class, got data from ajax query, lname is: " + data);

    try {
        Object [] ret = new Object [3];
        ret[0] = "txtlname";
        ret[1] = true;

        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        out = response.getWriter();
        out.write(gson.toJson(ret));

    }catch (Exception e) {
        System.out.println ("error " + e.getStackTrace() );
    }finally {
        out.flush();
        out.close();
    }

    return "success";
}

}

now, the response from the struts action class is proper, theres no issues there, but the data is not getting sent to the class, ie, request.getParameter ("txtlname") returns null, the name is not read by the class/sent by the request.

Was it helpful?

Solution

ok, i figured this out, it seems that for individual field validations, the jquery validation engine sends the data with the name of the input field in fieldId and the actual value stored by it in fieldValue
so if txtlname is the name of the field, it is stored in fieldId and the value in fieldValue, so changing

String data = request.getParameter("txtlname");

to

String data = request.getParameter("fieldValue");  

solved the problem

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top