2013-07-13 54 views
1

在我的春天應用程序,我正在傳遞兩個請求參數到我的業務邏輯..如何傳遞多個請求參數春天?

$.ajax({ 
     url : "classes/addResult", 
     type:'POST', 
     data : {"names":names,"globalClassId":globalClassId} 

    }); 

而且我的業務邏輯

@RequestMapping(value = "addResult", method = RequestMethod.POST) 
public String addResult(ResultForm form, 
     BindingResult result, Model model, 
     @RequestParam("names") String[] names, 
     @RequestParam("globalClassId") String globalClassId) 
     throws Exception { 
     ------------- 
     ------------ 
    return ""; 
} 

但不控制器調用此方法。爲什麼是它們是任何錯我的代碼..

+0

如果您收到'names'爲'String'和它自己的解碼,它的工作原理? –

回答

2
<script type="text/javascript"> 
    var names = new Array(); 
    $.ajax({ 
     url : "Result", 
     type : 'POST', 
     data : { 
      "names" : JSON.stringify(names),//or names.join() 
      "globalClassId" : globalClassId 
     }}); 
</script> 
1

如果您收到錯誤400(錯誤請求)????????????當您將數組數據({names:names})傳遞給$ .ajax()方法時,它會在數據庫名稱後面追加squre括號[](表示數據庫名稱將是[] //不是名稱)

爲此,你需要在你的代碼的一些變化

@RequestMapping(value = "addResult", method = RequestMethod.POST) 
public String addResult(ResultForm form, 
     BindingResult result, Model model, 
     @RequestParam("names[]") String[] names, //replace names with names[] 
     @RequestParam("globalClassId") String globalClassId) 
     throws Exception { 
     ------------- 
     ------------ 
    return ""; 
} 

,或者您可以使用

@RequestMapping(value = "addResult", method = RequestMethod.POST) 
    public String addResult(ResultForm form, 
      BindingResult result, Model model, 
      @RequestParam("globalClassId") String globalClassId) 
      throws Exception { 
     String[] names = request.getParameterValues("names[]");//getting names array here 
      ------------- 
      ------------ 
     return ""; 
    }