jquery验证引擎ajax调用struts2动作不发送数据

时间:2013-09-18 11:12:22

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

使用内置Ajax功能的jQuery验证引擎,我将我姓氏字段的请求发送到我的struts动作类。
我的HTML代码是:

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

ajax [ajaxStrutsCall]引用struts调用url的jquery.validationEngine-en.js。
jquery.validationEngine-en.js是:

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

url映射到我的struts.xml文件中,即:

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

最后我的struts动作类是:

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";
}

}

现在,struts动作类的响应是正确的,那里没有问题,但数据没有被发送到类,即request.getParameter ("txtlname")返回null,类不读取名称/请求发送。

1 个答案:

答案 0 :(得分:0)

好吧,我想出来了,似乎对于单个字段验证,jquery验证引擎发送的数据包含fieldId中输入字段的名称以及fieldValue中存储的实际值}
因此,如果txtlname是字段的名称,则它存储在fieldId中,而值存储在fieldValue中,因此更改

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

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

解决了问题