Spring控制器如何获取并将JQuery dataTable aoData转换为Java对象

时间:2014-10-15 18:10:41

标签: spring-mvc jquery-datatables

我有这个js代码:

                table = $('#searchResultTable').dataTable(
                    {
                        "destroy" : true,
                        "bProcessing" : true,
                        "bServerSide" : true,
                        "sDom" : 'trtip',
                        "sPaginationType" : "full_numbers",
                        "sServerMethod" : "POST",
                        "sEmptyTable" : "No Entity Profile found",
                        "iDisplayLength" : searchBO.pagination.pageSize,
                        "sAjaxSource" : "search.jax",

                        "fnServerParams" : function(aoData) {
                            aoData.push({
                                "name" : "searchBO",
                                "value" : searchBO
                            });
                        },
                        "fnServerData" : function(sSource, aoData,
                                fnCallback, oSettings) {
                            oSettings.jqXHR = $.ajax({
                                "contentType" : "application/json",
                                "dataType" : "json",
                                "url" : sSource,
                                "type" : "post",
                                "data" : aoData,
                                "success" : fnCallback,
                                "error" : function(a, b, c) {
                                    alert("error!");
                                }
                            });
                        },
                        "fnRowCallback" : function(nRow, aData,
                                iDisplayIndex) {
                            $('td:eq(1)', nRow).html(
                                    '<a href="viewProfile-'
                                            + aData.encEntityId + '.htm">'
                                            + aData.entityId + '</a>');
                            return nRow;
                        }
                    });

searchBO将是这样的:

var searchBO = {
    "pagination" : pagination,
    "individual" : individual,
    "organization" : organization,
    "enrollmentId" : enrollmentId,
    "medicareId" : medicareId,
    "npi" : npi,
    "contractId" : contractId,
    "firstName" : firstName,
    "lastName" : lastName,
    "ssn" : ssn,
    "dob" : dob,
    "licenseNumber" : licenseNumber,
    "licenseState" : licenseState,
    "specialtyType" : specialtyType,
    "licenseExpirationDate" : licenseExpirationDate,
    "businessName" : businessName,
    "ein" : ein,
    "organizationType" : organizationType,
    "city" : city,
    "state" : state,
    "zipCode" : zipCode,
    "startDate" : startDate,
    "endDate" : endDate
}

在Chrome调试模式下,我可以看到我传递给服务器的数据是:

 Request Payload
 sEcho=1&iColumns=2&sColumns=%2C&iDisplayStart=0&iDisplayLength=1&mDataProp_0=0&sSearch_0=&bRegex_0=false&bSearchable_0=true&bSortable_0=true&mDataProp_1=1&sSearch_1=&bRegex_1=false&bSearchable_1=true&bSortable_1=true&sSearch=&bRegex=false&iSortCol_0=0&sSortDir_0=asc&iSortingCols=1&searchBO=%5Bobject+Object%5D

现在我对Spring控制器将这些数据解析为Java对象的最佳方式感到有点困惑,这可能吗?我希望我的控制器是这样的:

    @RequestMapping(value = "/search.jax", method = RequestMethod.POST)
@ResponseBody
public Object doSearch(@RequestBody SearchRequestJSON searchRequestJSON,
        HttpServletRequest request, HttpServletResponse response)
        throws APSException {
    SearchResponseJSON json = new SearchResponseJSON();
    return json;
}

我发现SearchResponseJSON.java看起来像是:

import java.util.List;
public class SearchResponseJSON {
private int sEcho;
private int iTotalRecords;
private int iTotalDisplayRecords;
private List<EntityProfileVO> aaData;



public int getiTotalDisplayRecords() {
    return iTotalDisplayRecords;
}

public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
    this.iTotalDisplayRecords = iTotalDisplayRecords;
}


public int getiTotalRecords() {
    return iTotalRecords;
}

public void setiTotalRecords(int iTotalRecords) {
    this.iTotalRecords = iTotalRecords;
}

public int getsEcho() {
    return sEcho;
}

public void setsEcho(int sEcho) {
    this.sEcho = sEcho;
}

public List<EntityProfileVO> getAaData() {
    return aaData;
}

public void setAaData(List<EntityProfileVO> aaData) {
    this.aaData = aaData;
}
}

我非常不确定SearchRequestJSON.java需要是什么样的。我希望控制器@RequestBody可以将aoData从前端转换为searchRequestJSON对象。 谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

使用@ModelAttribute而不是@RequestBody