【发布时间】:2016-02-09 03:23:06
【问题描述】:
我有一个使用 jQuery Datatables 呈现数据的 Spring MVC 应用程序。 这是我的课程和 javascript 代码
Employee.java
public class Employee {
private int empId ;
private String firstName;
private String lastName ;
// getters and setters
}
EmployeeResponse.java
public class EmployeeResponse {
private List<Employee> empList ;
// getters and setters
}
EmployeeController.java
@Controller
@RequestMapping("/admin")
public class EmployeeController {
@RequestMapping(value = "/get-all-employee", method = RequestMethod.POST, consumes = { MediaType.APPLICATION_JSON_VALUE })
@ResponseBody
public EmployeeResponse getAllEmployee(HttpServletRequest request) {
EmployeeResponse response = new EmployeeResponse();
try {
response = getAllEmployeeList(); // returns response object
} catch (Exception e) {
logger.error("DCConsoleController::createNewStream exception: " + com.priceline.utils.StringUtils.getStackTrace(e));
return null;
}
return response ;
}
}
emp.jsp
<div class="row">
<table id="ldapStreamTable" class="table">
<thead>
<tr>
<th>Emp Id</th>
<th>First Name</th>
<th>Last Name</th>
</tr>
</thead>
</table>
</div>
emp.js
$(document).ready(function() {
var table = $('#appTable').DataTable( {
"sDom" : domSetting,
"oLanguage" : {
"sLengthMenu" : "_MENU_ records per page",
"sSearch" : "<span class='add-on'><i class='icon-search'></i></span>Search Application:",
"sZeroRecords" : "No matching records found",
},
"iDisplayLength" : 10,
"aLengthMenu" : [
[ 5, 10, 20, 25, 50, -1 ],
[ 5, 10, 20, 25, 50, "All" ] ],
"aaSorting" : [ [ 0, 'asc' ] ],
'sAjaxSource': '{context}/admin/get-all-employee',
[Pending here]
});
});
我应该怎么做才能以 json 格式获取响应并为每一列应用/使用渲染方法以将数据渲染到表中?
[注意:看起来我不能对每列使用带有 mData 的 aoColumns',因为我的 json 响应是员工对象列表]
更新 - 1:下面的示例 json
{"empList":[{"empId":3,"firstName":"Kiran","lastName":"Kumar"},{"empId":1,"firstName":"John","lastName":"Smith"},{"empId":0,"firstName":"Sachin","lastName":"Kumar"}]}
【问题讨论】:
-
您是否考虑过使用 Gson 或类似的库?
-
我来自控制器的响应已经是使用 Jackson API 的 json 格式。我无法弄清楚的是如何将作为 List
对象的 json 响应绑定到 UI 上的列中 -
你能发布你的
json回复吗? -
@GuruprasadRao - 使用示例 json 响应更新了问题。
标签: java jquery spring-mvc datatables