使用带有Spring MVC的jQuery Form Plugin提交表单后的成功消息

问题描述

|| 提交表单后,我试图显示成功消息。我从链接中使用jQuery表单插件 并提交作品很好。就像这样:
$(\'#emailForm\').ajaxForm();
现在,我想在“提交”按钮下添加成功消息,因此我在下面添加一个div:
<input name=\"submit\" type=\"submit\" value=\"#springMessage(\'action.save\')\" />

                <div id=\"emailSuccessMessage\">

                </div>
并对jQuery代码进行一些更改:
        var options = { 
        target:        \'#emailSuccessMessage\',success:       showResponse 

    }; 

$(\'#emailForm\').ajaxForm(options); 

function showResponse(responseText,statusText)  { 
    alert(\'status: \' + statusText + \'\\n\\nresponseText: \\n\' + responseText + 
        \'\\n\\nThe output div should have already been updated with the responseText.\'); 
} 
我的Controller方法是:
@RequestMapping(value = \"/password\",method = RequestMethod.POST)
    public String changePassword(@Valid @modelattribute(\"editPassword\") EditPasswordForm editPasswordForm,BindingResult result) {
        if (result.hasErrors()) {
            return \"editaccount\";
        }

        userService.changePassword(editPasswordForm);

        return \"editaccount\";
    } 
现在,当我尝试提交时,它可以工作,但是没有显示警报(我添加了警报只是为了测试)。 showResponse方法如何工作?我是jQuery的新手,我想知道如何实现我的目标。我应该在哪里设置responseText? 谢谢 达维德     

解决方法

如果您将ajax与Spring MVC一起使用,则需要确保您的响应位于Ordet中的Response Body中,以添加
@ResponseBody
注释 因此,您的代码应如下所示:
@RequestMapping(value = \"/password\",method = RequestMethod.POST)
@ResponseBody
    public String changePassword(@Valid @ModelAttribute(\"editPassword\") EditPasswordForm editPasswordForm,BindingResult result) {
        if (result.hasErrors()) {
            return \"editAccount\";
        }

        userService.changePassword(editPasswordForm);

        return \"editAccount\";
    } 
您可以在Spring MVC参考指南中阅读有关此内容的更多信息: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody