2012-02-29 73 views
1
$.ajax({ 
    cache:false, 
    type: 'POST', 
    url: "${saveDTO}", 
    data: mySubmitData, //a stringified json object, a form converted using toObject plugin 
    contentType: "application/json", 
    success: function(data) {    
    savedDialog.html(JSON.stringify(data) + "<br><br>"+mySubmitData); 
    } 
}); 

和控制器是:轉換的json使用jquery POJO /春/傑克遜

public @ResponseBody MyDTO saveDTO(@Valid final MyDTO myDTO,BindingResult result, Model model){ 
System.out.println(myDTO.getMyField + " " + myDTO.getSecondField; 
return new MyDTO(); 
} 

輸出我在對話框中顯示得到這樣的:

{"myField":null,"secondField":null} //new empty dto converted and returned spring 

{"myField":"RU","secondField":"13-02-12"} //submitted data, a form converted to json 

雖然我SYS出去控制檯輸出顯示這兩個字段都是空的,JSON沒有約束!爲什麼? DTO本身只有兩個私人領域與適當的獲取/設置器

+1

添加「MyDTO」的代碼 – beerbajay 2012-02-29 12:00:33

回答

0
//js 
var mySubmitData ='{"myField":"myFieldValue", "secondField":"secondFieldValue"}'; 
//dto 
public class MyDTO implements Serializable { 

    private String myField; 

    private String secondField; 

    MyDTO() {} //important 

    public void setMyField() { ...} 

    public void setSecondField() { ... } 
} 
//web resource 
    public @ResponseBody MyDTO saveDTO(@Valid final MyDTO myDTO,BindingResult result, Model model){ 
    System.out.println(myDTO.getMyField + " " + myDTO.getSecondField; 
     return myDTO; 
     //return new MyDTO(); //it's empy object if you don't have default values 
    } 
+0

好的嘗試,你有點錯過了關鍵點。 myDTO字段爲空,則sys out print命令將刪除null。我正在重新調整一個新對象來檢查正確的映射。我認爲問題出在我的串行器/解串器中 – NimChimpsky 2012-02-29 14:39:17