我有java对象作为对象A包含对象B而对象B包含对象C,这是我的对象如下
public class objectA{
private String name;
private String college;
private objectB obj;
}
public class objectB{
private String address;
private String dept;
private objectC obj;
}
public class objectC{
private String bus;
private String bike;
}
我必须将objectA从 jsp 发送到控制器如何在jquery中创建对象objectA,这是我的ajax代码
$("#SaveBtnId").click(function() {
**HOW TO MAKE OBJECT A**
$.ajax({
url : "../seller/addSeller",
type : "post",
data : objectA,
contentType : "application/json",
dataType : "json",
success : function(data) {
alert("Saved Successfully");
},
error : function(error) {
alert(error);
alert("Details failed to save");
}
});
});
答案 0 :(得分:0)
以下代码为您创建了一个可以传递给控制器的模型。 如果您遇到任何问题,请尝试告诉我。
$("#SaveBtnId").click(function() {
ObjectC:{
bus:$('#txtBus').val(), //Considering as getting from textBox
dept:$('#txtBike').val(),
};
ObjectB:{
address:$('#txtAdd').val(), //Considering as getting from textBox
dept:$('#dept').val(),
obj:ObjectC
};
ObjectA:{
name: $('#txtName').val(),
college: $('#txtCollege').val(),
obj:ObjectB
};
$.ajax({
url : "../seller/addSeller",
type : "post",
data : JSON.stringify(objectA),
contentType : "application/json",
dataType : "json",
success : function(data) {
alert("Saved Successfully");
},
error : function(error) {
alert(error);
alert("Details failed to save");
}
});
});
答案 1 :(得分:0)
你可以这样做:
$("#SaveBtnId").click(function() {
var a = {};
var b = {};
var c = {};
c['bus'] = 'bus';
c['bike'] = 'bike';
b['address'] = 'address';
b['dept'] = 'dept'
b['objectC'] = c;
a['name'] = 'name';
a['college'] = 'college';
a['objectB'] = b;
$.ajax({
url : "../seller/addSeller",
type : "post",
data : JSON.strigify(a),
contentType : "application/json",
dataType : "json",
success : function(data) {
alert("Saved Successfully");
},
error : function(error) {
alert(error);
alert("Details failed to save");
}
});
});
这可能会有所帮助。 请注意,所有三个类都必须具有setter和getter。