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

标签 java jquery spring-mvc datatables

我有一个使用 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/

相关文章:

jQuery - 捕获选项卡选择事件

spring - 使用 hibernate 空间配置 Spring Boot

javascript - 用 jQuery 隐藏的 HTML 元素在链接单击时不会重新出现

java - 如何从整个月的日历中跳过周末(周六和周日)

java - 格式化 TreeItem 文本颜色?

java - 在 post 请求中作为 JSON 字符串传递的 FormDataParam 对象未正确反序列化

jquery 找到精确的字符串,不多也不少

java - 方法参数级别的@ModelAttribute 注释是什么意思?

javascript - 如何在 javascript 中获取 spring mvc Controller 模型键值?

java - 使用 JNDI 从 LDAP 服务器搜索(选择)任何数据