2011-05-24 87 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