2012-10-20 51 views
1

我看到了相關的問題,並嘗試了那些幫助不到的問題。我送POST requst使用jQuery這樣的:嘗試發送數組到春天mvc控制器時出現「Bad Request」請求

var data = {};   
      //this works every time and it's not issue 
      var statusArray = $("#status").val().split(','); 
      var testvalue = $("#test").val(); 

        data.test = testvalue; 
      data.status = statusArray ; 

      $.post("<c:url value="${webappRoot}/save" />", data, function() { 
     }) 

在控制器端我嘗試以下操作:

public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestBody String [] status) { 

     //I never get to this point, but when I set statusArray to required false test variable is being populated correctly 
     } 


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam String [] status) { 

     //I never get to this point, but when I set statusArray to required false test variable is being populated correctly 
     } 



public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam("status") String [] status) { 

     //I never get to this point, but when I set statusArray to required false test variable is being populated correctly 
     } 


public void saveStatus(ModelMap model, Principal principal, HttpSession session, final HttpServletResponse response, @RequestParam String test, @RequestParam(name="status") String [] status) { 

     //I never get to this point, but when I set statusArray to required false test variable is being populated correctly 
     } 

沒有這些工作,我不知道我在做什麼錯了,無論我做什麼我得到Bad request

+0

你可以發佈什麼樣的環節你們已經經歷了? – askmish

回答

1

你的狀態參數應該是@RequestParam(value = "status[]") String[] status(Spring 3.1)。

0

我認爲你的問題可能是發送一個數組,你必須多次發送參數。

在GET操作類似的情況:狀態= FOO &狀態= BAR

我不知道春天會自動將一個逗號分隔字符串轉換爲一個數組爲您服務。然而,您可以添加PropertyEditor(請參閱PropertyEditorSupport)以逗號分隔字符串。

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    binder.registerCustomEditor(String[].class, new PropertyEditorSupport() { 
     @Override 
     public String getAsText() { 
      String value[] = (String[]) getValue(); 
      if (value == null) { 
       return ""; 
      } 
      else { 
       return StringUtils.join(value, ","); 
      } 
     } 

     @Override 
     public void setAsText(String text) throws IllegalArgumentException { 
      if (text == null || text.trim().length() == 0) { 
       setValue(null); 
      } 
      else { 
       setValue(StrTokenizer.getCSVInstance(text).getTokenArray()); 
      } 
     } 

    }); 
} 

注意,我使用的公共琅既加入和分割字符串,但使用任何手段你願意,你可以很容易地就去做你自己。一旦你做到了這一點,任何時候你想要一個參數綁定到一個String []從一個單一的字符串,春天會自動爲你轉換它。

1

我也經歷了同樣的問題壞請求。我通過執行以下代碼來解決它。
您可以通過將數組轉換爲json字符串來向控制器發佈數組,方法是JSON.stringify(array)
我推動多個對象使用push()

var a = []; 
    for(var i = 1; i<10; i++){ 
     var obj = new Object(); 
     obj.name = $("#firstName_"+i).val(); 
     obj.surname = $("#lastName_"+i).val(); 
     a.push(obj); 
    } 

    var myarray = JSON.stringify(a); 
    $.post("/ems-web/saveCust/savecustomers",{myarray : myarray},function(e) { 

    }, "json"); 

控制器:
可以使用傑克遜處理JSON字符串。
Jackson是一個高性能的JSON處理器Java庫。

@RequestMapping(value = "/savecustomers", method = RequestMethod.POST) 
    public ServiceResponse<String> saveCustomers(ModelMap model, @RequestParam String myarray) { 

     try{ 
      ObjectMapper objectMapper = new ObjectMapper().configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); 
      List<DtoToMAP> parsedCustomerList = objectMapper.readValue(myarray, new TypeReference<List<DtoToMAP>>() { }); 
      System.out.println(" parsedCustomerList :: " + parsedCustomerList); 
     }catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

注意:確保您的dto應該包含與使用數組對象發佈相同的變量名稱。
在我的情況下,我的dto包含firstName,lastName作爲發佈數組對象。

傑克遜扶養:

<dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-core-asl</artifactId> 
     <version>1.9.3</version> 
    </dependency> 
    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.3</version> 
    </dependency> 
相關問題