Client-side vs Server side Ajax Spring MVC 3

Client-side vs Server side Ajax Spring MVC 3

cycledualcycledual Posts: 1Questions: 1Answers: 0

Hi, I originally was loading 18000 records into my DataTable and found the performance unacceptable for my page. Once loaded though, the DataTable's functionality is stellar. I have since been working on a server-side approach implemented in a Spring 3.0, MVC framework, but I have a few questions regarding the implementation because it is not yet working the way it was when client-side.

Do I need to implement the logic for paging and searching as explained here? http://www.codeproject.com/Articles/359750/jQuery-DataTables-in-Java-Web-Applications?
It is not working the same way it did as client-side.

Also, how can I maintain the selections?

In this forum I have found examples for Spring MVC and the controller is very simple in that it only prepares and returns a properly formatted JSON object as a string. But the paging and filtering don't work.

http://www.datatables.net/forums/discussion/2456/datatables-spring-mvc-support
http://datatables.net/forums/discussion/7949/getting-cannot-read-property-length-of-undefined-in-chrome-error

Thank you for your help.

// My table

<table id="clients" class="display">
<thead><tr><th>LOBs</th><th>Line Of Business</th><th>BE Number</th><th>BE Client Name</th><th>Acronym</th><th>AE Number</th><th>AE Client Name</th><th>AE RPI<br>Internal</th><th>AE RPI<br>Master</th><th>AE RPI<br>Client</th><th>Term Date</th></tr>
</thead>
<tfoot><tr><th>LOBs</th><th>Line Of Business</th><th>BE Number</th><th>BE Client Name</th><th>Acronym</th><th>AE Number</th><th>AE Client Name</th><th>AE RPI<br>Internal</th><th>AE RPI<br>Master</th><th>AE RPI<br>Client</th><th>Term Date</th></tr>
</tfoot>
</table>
// My initialization:
$(document).ready(function() {
    clientTable = $('#clients').dataTable({
        "bServerSide": true,
        "bProcessing": true,
        "deferRender": true,
        "sAjaxSource": "ClientGsonObjects.html",
        "sAjaxDataProp": "aoData",
        "fnServerData": function (sSource, aoData, fnCallback) {
            $.ajax({
                "dataType": 'json',
                "type": "GET",
                "url": sSource,
                "data": aoData,
                "success":fnCallback
            });
        },
        "aoColumns": [
                      {"mData": "lob", "defaultContent": ""},
                      {"mData": "clientLob", "defaultContent": ""},
                      {"mData": "beNumber", "defaultContent": ""},
                      {"mData": "beClientName", "defaultContent": ""},
                      {"mData": "acronym", "defaultContent": ""},
                      {"mData": "aeNumber", "defaultContent": ""},
                      {"mData": "aeClientName", "defaultContent": ""},
                      {"mData": "aeRpiInternal", "defaultContent": ""},
                      {"mData": "aeRpiMaster", "defaultContent": ""},
                      {"mData": "aeRpiClient", "defaultContent": ""},
                      {"mData": "termDate", "defaultContent": ""}
                  ],
        "columnDefs": [{"targets": [0], "visible": false, "searchable": false}],
        "sDom": 'W<"clear">lfrtip',
        "oColumnFilterWidgets": {
            "aiExclude": [0,2,3,4,5,6,7,8,9,10]
        }
    });
});
// My Controller:
@Controller
public class MainController  {

    List<Client> clients = null;
    List<Client> newClients = null;
    
    private int iTotalRecords;

    @RequestMapping(value = "/secured/ClientGsonObjects.html", method = RequestMethod.GET)
    public @ResponseBody String doGet(
            @RequestParam int iDisplayStart,
                        @RequestParam int iDisplayLength,
                        @RequestParam int iColumns,
                        @RequestParam String sSearch,
                        @RequestParam int iSortingCols,
                        @RequestParam int iSortCol_0,
                        @RequestParam String sSortDir_0,
                        @RequestParam String sEcho) throws Exception {
        
        // Some displays
        System.out.println("Here in the servlet");
        System.out.println("iDisplayStart: " + iDisplayStart + ", iDisplayLength: " + iDisplayLength);
        System.out.println("sEcho: " + sEcho + ", sSearch: " + sSearch);

        //DataTableParamModel param = DataTablesParamUtility.getParam(request);
        //String sEcho = param.sEcho;
        int iTotalDisplayRecords; //value will be set when code filters companies by keyword
         
        newClients = new LinkedList<Client>();
        
        try {
            for(Client client : clients) {
                 if(client.getClientLob().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getBeNumber().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getBeClientName().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAcronym().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAeNumber().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAeClientName().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAeRpiInternal().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAeRpiMaster().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getAeRpiClient().toLowerCase().contains(sSearch.toLowerCase()) ||
                    client.getTermDate().toLowerCase().contains(sSearch.toLowerCase())){
                     newClients.add(client);
                 }
            }
        }
        catch (Exception e) {
            // A display
            System.out.println("Here was the sSearch");
            e.printStackTrace();            
        }
        
        iTotalDisplayRecords = newClients.size();

        final int sortColumnIndex = iSortCol_0; //param.iSortColumnIndex;
        final int sortDirection = sSortDir_0.equals("asc") ? -1 : 1;

        // A display
        System.out.println("newClients search size: " + newClients.size());
         
        Collections.sort(newClients, new Comparator<Client>(){
             
            @Override
            public int compare(Client c1, Client c2) {

                switch(sortColumnIndex){
                    case 0:
                        return c1.getClientLob().compareTo(c2.getClientLob()) * sortDirection;
                    case 1:
                        return c1.getBeNumber().compareTo(c2.getBeNumber()) * sortDirection;
                    case 2:
                        return c1.getBeClientName().compareTo(c2.getBeClientName()) * sortDirection;
                    case 3:
                        return c1.getAcronym().compareTo(c2.getAcronym()) * sortDirection;
                    case 4:
                        return c1.getAeNumber().compareTo(c2.getAeNumber()) * sortDirection;
                    case 5:
                        return c1.getAeClientName().compareTo(c2.getAeClientName()) * sortDirection;
                    case 6:
                        return c1.getAeRpiInternal().compareTo(c2.getAeRpiInternal()) * sortDirection;
                    case 7:
                        return c1.getAeRpiMaster().compareTo(c2.getAeRpiMaster()) * sortDirection;
                    case 8:
                        return c1.getAeRpiClient().compareTo(c2.getAeRpiClient()) * sortDirection;
                    case 9:
                        return c1.getTermDate().compareTo(c2.getTermDate()) * sortDirection;
                }
                return 0;
            }
        });
         
        // A display
        System.out.println("newClients compare size: " + newClients.size());

        if(newClients.size() < iDisplayStart + iDisplayLength)
            newClients = newClients.subList(iDisplayStart, newClients.size());
        else
            newClients = newClients.subList(iDisplayStart, iDisplayLength);

        // A display
        System.out.println("newClients subList size: " + newClients.size());

        Gson gson = new Gson();
        JsonObject jsonResponse = new JsonObject();
        
        try {

            jsonResponse.addProperty("sEcho", sEcho);
            jsonResponse.addProperty("iTotalRecords", iTotalRecords);
            jsonResponse.addProperty("iTotalDisplayRecords", iTotalDisplayRecords);
            jsonResponse.add("aoData", gson.toJsonTree(clients));

        }
        catch (JsonIOException e) {
            // A display
            System.out.println("Here in the json call");
            e.printStackTrace();
        }

        // A display
        System.out.println("Size of the json: " + jsonResponse.toString().substring(0, 400));
        return jsonResponse.toString();
    }    
This discussion has been closed.