【问题标题】:Success message after submitting form using jQuery Form Plugin with Spring MVC使用带有 Spring MVC 的 jQuery 表单插件提交表单后的成功消息
【发布时间】:2011-09-01 06:31:15
【问题描述】:

我试图在提交表单后显示成功消息。我使用来自link的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.'); 
} 

我的控制器方法是:

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

谢谢

大卫

【问题讨论】:

  • 看起来从来没有调用成功的函数 showResponse.. 为什么?

标签: jquery ajax forms spring-mvc


【解决方案1】:

如果您在 Spring MVC 中使用 ajax,您需要确保您的响应将在响应正文中为了做到这一点,请添加 @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

【讨论】:

  • 看起来这可能是我正在寻找的。我稍后会试一试!!
  • 非常感谢!这正是我需要的!
猜你喜欢
  • 2017-11-17
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 2013-01-10
  • 2013-05-03
  • 2016-03-19
  • 1970-01-01
  • 2016-05-29
相关资源
最近更新 更多