2011-05-24 86 views
2

我试图在提交表单后显示成功消息。我使用jQuery表格插件link使用Spring MVC的jQuery Form插件提交表单后的成功消息

并提交工作正常。就这样:

$('#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?

感谢

的Dawid

+0

它看起来像一个函数从成功showResponse永远不会被称为..为什么? – Dawid 2011-05-24 21:51:34

回答

2

如果您正在使用AJAX与Spring MVC,你需要响应体中确保你的反应会在ordet做补充@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

+0

它看起来像这可能是我正在寻找。我会稍后再试! – Dawid 2011-05-25 07:52:56

+0

非常感谢!这正是我需要的! – Dawid 2011-05-25 21:14:58