由于缺少“root”元素,我无法填充我的dataTable。
请参阅以下代码并帮助我解决问题。
第1部分:
$('#example').DataTable({
//"ajax": "density.txt",
"ajax" : "getProductPropData",
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"columns": [{
"data": "densityId"
}, {
"data": "densityDescription"
}, {
data: null,
className: "center",
defaultContent: '<a href="#" id="edit" class="edit" data-toggle="modal" data-target="#myModal">Edit</a> / <a href="#" id="delete">Delete</a>'
//defaultContent: '<button type="button" class="btn-default" data-toggle="modal" data-target="#myModal">Open Modal</button>'
}]
});
第2部分:我在Struts 2动作类中正在做什么。
public String execute() throws Exception {
SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
ProductPropertyDAO pdao = new ProductPropertyDAOImpl(sf);
List<DensityGroup> dg = pdao.getProductPropListData("Density");
List<DensityGroup> list = new ArrayList<DensityGroup>();
System.out.println("SIZE is: " + dg.size());
for (int i = 0; i < dg.size(); i++) {
DensityGroup denpojo = new DensityGroup();
denpojo.setDensityId(dg.get(i).getDensityId());
denpojo.setDensityDescription(dg.get(i).getDensityDescription());
list.add(denpojo);
}
data = list;
System.out.println("JSON is :\n" + data);
return "success";
}
来自Chrome网络标签的我的JSON数据是:
[{"densityDescription":"16 KG","densityId":"21"},
{"densityDescription":"Chitti","densityId":"22"}]
正确的数据应该是:
{"data" : [{"densityDescription":"16 KG","densityId":"21"},
{"densityDescription":"Chitti","densityId":"22"}]}
修改数据变量:
private List<DensityGroup> data;
public List<DensityGroup> getData() {
return data;
}
public void setData(List<DensityGroup> data) {
this.data = data;
}
DensityGroup.java
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name="DENGROUP")
public class DensityGroup{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column (name = "DEN_ID")
@SequenceGenerator(name="dengroupSeq",sequenceName="density_group_seq")
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="dengroupSeq")
private String densityId;
public String getDensityId() {
return densityId;
}
public void setDensityId(String densityId) {
this.densityId = densityId;
}
public String getDensityDescription() {
return densityDescription;
}
public void setDensityDescription(String densityDescription) {
this.densityDescription = densityDescription;
}
@Column (name = "DEN_DESC")
private String densityDescription;
}
答案 0 :(得分:2)
使用ajax.dataSrc
选项设置为空字符串。
例如:
"ajax" : {
"url": "getProductPropData",
"dataSrc": "",
"dataType": 'json',
"contentType": "application/json; charset=utf-8"
}
请参阅this example以获取代码和演示。