我有一个应该加载表数据的jquery数据。我正在使用struts2 Hibernate。
JSP页面:
<script>
$(document).ready(function() {
$(".jqueryDataTable").dataTable({
"sPaginationType" : "full_numbers",
"bProcessing" : false,
"bServerSide" : false,
"sAjaxSource" : "getProductPropData",
"bJQueryUI" : true,
"aoColumns" : [
{ "mData": "densityId" },
{ "mData": "densityDescription" }
]
} );
} );
</script>
struts.xml中
<package name="json" namespace="/" extends="json-default">
<action name="getProductPropData" class="com.test.action.PropertyListDataAction">
<result type="json">
<param name="excludeNullProperties">true</param>
<param name="noCache">true</param>
</result>
</action>
</package>
行动类:
public String execute() throws Exception {
// TODO Auto-generated method stub
SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
ProductPropertyDAO pdao = new ProductPropertyDAOImpl(sf);
List<DensityGroup> dg = pdao.getProductPropListData("Density");
Gson gson = new Gson();
jsonData = gson.toJson(dg);
System.out.println("dg:"+dg);
return "success";
}
我以json格式从数据库获得响应,但我的数据表没有被填充。
] 2
JSON输出:
{"jsonData":"[{\"densityId\":15,\"validationAware\":{}},{\"densityId\":11,\"densityDescription\":\"Mcvr\",\"validationAware\":{}},{\"densityId\":14,\"densityDescription\":\"test\",\"validationAware\":{}},{\"densityId\":16,\"densityDescription\":\"Chitti\",\"validationAware\":{}},{\"densityId\":12,\"densityDescription\":\"MCVR\",\"validationAware\":{}},{\"densityId\":13,\"densityDescription\":\"4\\\" DIA\",\"validationAware\":{}},{\"densityId\":14,\"densityDescription\":\"test\",\"validationAware\":{}},{\"densityId\":15,\"validationAware\":{}},{\"densityId\":21,\"densityDescription\":\"1 1\/4\\\" DIA\",\"validationAware\":{}},{\"densityId\":22,\"densityDescription\":\"3\/4\\\" DIA\",\"validationAware\":{}},{\"densityId\":37,\"densityDescription\":\"25\\\"DIA\",\"validationAware\":{}},{\"densityId\":24,\"densityDescription\":\"8\\\" DIA\",\"validationAware\":{}},{\"densityId\":25,\"densityDescription\":\"1 1\/8\\\" DIA\",\"validationAware\":{}},{\"densityId\":28,\"densityDescription\":\"6\\\"\",\"validationAware\":{}},{\"densityId\":29,\"densityDescription\":\"1\/2\\\" DIA\",\"validationAware\":{}},{\"densityId\":30,\"densityDescription\":\"EXHAUST STEAM\",\"validationAware\":{}},{\"densityId\":19,\"densityDescription\":\"1 1\/2\\\" DIA\",\"validationAware\":{}},{\"densityId\":44,\"densityDescription\":\"EXHAUSTCLADDING\",\"validationAware\":{}},{\"densityId\":1,\"densityDescription\":\"16 KG\",\"validationAware\":{}},{\"densityId\":11,\"densityDescription\":\"Mcvr\",\"validationAware\":{}},{\"densityId\":2,\"densityDescription\":\"20 KG\",\"validationAware\":{}},{\"densityId\":3,\"densityDescription\":\"24 KG\",\"validationAware\":{}},{\"densityId\":5,\"densityDescription\":\"48 KG\",\"validationAware\":{}},{\"densityId\":6,\"densityDescription\":\"64 KG\",\"validationAware\":{}},{\"densityId\":7,\"densityDescription\":\"96 KG\",\"validationAware\":{}},{\"densityId\":8,\"densityDescription\":\"100 KG\",\"validationAware\":{}},{\"densityId\":9,\"densityDescription\":\"128 KG \",\"validationAware\":{}},{\"densityId\":10,\"densityDescription\":\"120 KG \",\"validationAware\":{}},{\"densityId\":18,\"densityDescription\":\"144KG\",\"validationAware\":{}},{\"densityId\":23,\"densityDescription\":\"5\\\" DIA\",\"validationAware\":{}},{\"densityId\":26,\"densityDescription\":\"4\\\"\",\"validationAware\":{}},{\"densityId\":27,\"densityDescription\":\"125 KG\",\"validationAware\":{}},{\"densityId\":34,\"densityDescription\":\"10\\\" DIA\",\"validationAware\":{}},{\"densityId\":32,\"densityDescription\":\"18\\\" \",\"validationAware\":{}},{\"densityId\":33,\"densityDescription\":\"12\\\" DIA\",\"validationAware\":{}},{\"densityId\":35,\"densityDescription\":\"14\\\" DIA\",\"validationAware\":{}},{\"densityId\":36,\"densityDescription\":\"5\/8\\\" DIA\",\"validationAware\":{}},{\"densityId\":38,\"densityDescription\":\"(1\/2 x 24 SWG)\",\"validationAware\":{}},{\"densityId\":39,\"densityDescription\":\"8\\\" FLANGE\",\"validationAware\":{}},{\"densityId\":40,\"densityDescription\":\"6\\\" FLANGE\",\"validationAware\":{}},{\"densityId\":41,\"densityDescription\":\"3\\\" FLANGE\",\"validationAware\":{}},{\"densityId\":42,\"densityDescription\":\"25 MM DIA\",\"validationAware\":{}},{\"densityId\":43,\"densityDescription\":\"150 KG\",\"validationAware\":{}},{\"densityId\":46,\"densityDescription\":\"INSULATION WORK\",\"validationAware\":{}},{\"densityId\":47,\"densityDescription\":\"18\\\" DIA\",\"validationAware\":{}},{\"densityId\":16,\"densityDescription\":\"Chitti\",\"validationAware\":{}},{\"densityId\":45,\"densityDescription\":\"1 3\/8 DIA\",\"validationAware\":{}},{\"densityId\":56,\"densityDescription\":\"18 KG\",\"validationAware\":{}},{\"densityId\":55,\"densityDescription\":\"40 KG\",\"validationAware\":{}},{\"densityId\":48,\"densityDescription\":\"85 KG\",\"validationAware\":{}},{\"densityId\":49,\"densityDescription\":\"2\\\"\",\"validationAware\":{}},{\"densityId\":50,\"densityDescription\":\"12 KG\",\"validationAware\":{}},{\"densityId\":53,\"densityDescription\":\"FLANGES\",\"validationAware\":{}},{\"densityId\":51,\"densityDescription\":\"2 1\/2\\\" DIA\",\"validationAware\":{}},{\"densityId\":52,\"densityDescription\":\"INSULATION \",\"validationAware\":{}},{\"densityId\":54,\"densityDescription\":\"VALVES\",\"validationAware\":{}},{\"densityId\":57,\"densityDescription\":\"CLASS O\",\"validationAware\":{}},{\"densityId\":58,\"densityDescription\":\"140 KG\",\"validationAware\":{}},{\"densityId\":59,\"densityDescription\":\"30 KG\",\"validationAware\":{}}]"}
答案 0 :(得分:0)
默认情况下,datatables会在json中查找“data”元素。 它无法找到它,因此它是“未定义的”。 DataTables提供了一种指定要使用的元素的方法。将您的json与下面的文档链接进行比较,您应该能够在数据对象中指定ajax.dataSrc =“jsonData”或将“jsonData”更改为“data”。 如果可能,我建议切换到数据表选项的新命名约定,它更清晰,文档是针对它定制的。如果你不能使用新的命名方案,改变你的json对象可能是最好的选择。
文档: https://datatables.net/examples/ajax/custom_data_property.html