2014-12-24 30 views
0

我有一個HTML表單,其中有一個「嵌套」對象。 如果我發送一個只有純屬性的enitty,沒有其他實體在裏面,這是可以的,但是使用ajax發送表單到一個REST控制器,拋出一個異常而不是事件到達控制器,因爲這個'嵌套'對象沒有被重新分配爲發送的主要實體的屬性,即Product將嵌套對象提交給REST控制器

我能搞到兩個differentes jquery方式AJAX運行數據:

$.param($('form').serializeArray()) 
"id=&type.id=1&name=One" 

JSON.stringify($('form').serializeArray()) 
"[{"name":"id","value":""},{"name":"type.id","value":"1"},{"name":"name","value":"One"}]" 

而在後一種情況下,我當然可以創建一個$.eachjquery功能完全轉換目的。 但有沒有什麼辦法可以很容易地使用jquery轉換它ou配置良好的傑克遜映射器對象?

這裏是表單,ajax調用,java實體和控制器。

形式:

<form> 
    <input type="hidden" name="id"/> 
    <select id="type.id"> 
    <option value='1'>One</option> 
    <!-- ... --> 
    </select> 
    <input type="text" id="name"/> 
</form> 

實體:

@Entity 
@Table 
public class Product { 
    @Id 
    @GeneratedValue 
    private Integer id; 
    @ManyToOne 
    private Type type; 
    @Column(nullable = false, length = 250) 
    private String name; 

    /* getters and setters */ 
} 

@Table 
@Entity 
public class Type { 
    @Id 
    @GeneratedValue 
    @Column(length = 5) 
    private Integer id; 
    @Column(length = 50, nullable = false) 
    private String name; 

    /* getters and setters */ 
} 

阿賈克斯一些按鈕提交:

$.ajax({ 
     type : "POST", 
     url : "create", 
     contentType : "application/json; charset=utf-8", 
     dataType : "json", 
     data : /* 
       here stands the doubt. How serialize my form? 
       */, 
     success : function(data) { 
      if (data === true) { 
       alert('Success!'); 
      } else { 
       console.log('Some error'); 
      } 
     } 
    }); 

控制器:

@RequestMapping(value = "/create", method = RequestMethod.POST) 
@ResponseBody 
public Boolean create(@RequestBody Domain model) throws Exception { 
    try { 
     getService().create(model); 
     return true; 
    } catch (Exception e) { 
     return false; 
    } 
} 

回答

-1

你可以試試這個:

控制器:在一些按鈕提交

@RequestMapping(value = "/create", method = RequestMethod.POST , headers = "Accept=application/json") 
@ResponseBody 
public Map<String, String> create(@RequestBody Domain model) throws Exception { 
    HashMap<String, String> result = new HashMap<String, String>(); 
    try { 
     getService().create(model); 
     result.put("status",true); 
    } catch (Exception e) { 
     result.put("status",false); 
    } 
    return result; 
} 

AJAX:

$.ajax({ 
     type : "POST", 
     url : "create", 
     contentType : "application/json; charset=utf-8", 
     dataType : "json", 
     data : /* 
       here stands the doubt. How serialize my form? 
       */, 
     success : function(data) { 
      if (data.status === true) { 
       alert('Success!'); 
      } else { 
       console.log('Some error'); 
      } 
     } 
    }); 
+0

謝謝嘗試幫助我,史蒂文。但主要問題仍然是:如何序列化形式? – Moesio

1

對不起,我只是誤解了你的問題。 這是另一種解決方案,它提供了表單選擇器並返回json對象。

function serializeForm(formSelector){ 
    var params = $.param($(formSelector).serializeArray()); 
    var jsonStr = '{"'+params.replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g,'":"')+'"}'; 
    var jsonObj = JSON.parse(jsonStr); 
    return jsonObj; 
} 

var formJsonData = serializeForm('form'); 
+0

但是,如果主對象具有另一個對象作爲主對象的字段,如我的問題示例'type.id'。對象映射器無法將'type.id'作爲Product字段找到並引發異常。這是主要問題。 – Moesio

0

這是另一個對象序列化器。

$.fn.serializeObject = function() 
{ 
    var o = {}; 
    var a = this.serializeArray(); 
    $.each(a, function() { 
     if (o[this.name] !== undefined) { 
      if (!o[this.name].push) { 
       o[this.name] = [o[this.name]]; 
      } 
      o[this.name].push(this.value || ''); 
     } else { 
      o[this.name] = this.value || ''; 
     } 
    }); 
    return o; 
}; 
+0

我只是嘗試過,@Ghokun,但正如我在另一個答案中的評論中所說:如果主對象具有另一個對象作爲主對象的字段,如我的問題示例'type.id'。對象映射器無法將'type.id'作爲Product字段找到並引發異常。這是主要問題。 – Moesio