gpt4 book ai didi

java - Spring MVC + jQuery 数据表 + JSON 响应

转载 作者:行者123 更新时间:2023-11-29 04:51:21 24 4
gpt4 key购买 nike

我有一个使用 jQuery 数据表呈现数据的 Spring MVC 应用程序。这是我的类和 javascript 代码

员工.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 格式的响应并为每一列应用/使用渲染方法以将数据渲染到表中?

[注意:看起来我不能将 aoColumns' 与每一列的 mData 一起使用,因为我的 json 响应是员工对象列表]

更新 - 1:下面的示例 json

{"empList":[{"empId":3,"firstName":"Kiran","lastName":"Kumar"},{"empId":1,"firstName":"John","lastName":"Smith"},{"empId":0,"firstName":"Sachin","lastName":"Kumar"}]}

最佳答案

我将您的示例 ajax 数据保存在 github 并将其作为 ajax 源提供。您将那部分保留在返回 json 响应的 method 中,而不是 empList 作为 json 响应中的基本根将其替换为 data ,就像我在上面的 github json 文件中所做的那样。现在,除了 ajaxSource 之外,您还需要将 columns 映射到您的 dataTables,如下所示:

"columns": [
{ "data": "empId"},//should match column value in json file
{ "data": "firstName"},//should match column value in json file
{ "data": "lastName"}//should match column value in json file
]

A Demo here

关于java - Spring MVC + jQuery 数据表 + JSON 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283357/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com