Search code examples
struts2datatables

how to send or capture dataTable 1.10 parameters in struts2 action class


I have a web application in which I am using DataTable version 1.10 to display the records and for pagination. I am new to DataTable. I am finding it difficult in capturing the parameters like search string in the search box of data table, Sort action,iTotalRecords,iTotalDisplayRecords,iDisplayLength etc etc.,

I am using struts2 for my web application. I have enabled the bServerSide=true; and calling "sAjaxSource" : "PaginationAction". the records private List<Income> aaData; are getting populated in jsp page. But I am finding it difficult to capture the other parameters as specified above. to which action class's variable does this entered string goes to.? I have a variable private String search; in my action class to which I am expecting the search box string should be assigned.

Please help me which this. Do I need to add any jar files of datatable1.10 version to my project( I am not sure If we have any jar).

Here is my JQuery Code:

 var oTable=$(".IncomeTable").dataTable({
    "paging":true,
    "searching": true,
    "bProcessing" : true,
    "bServerSide" : true,
    "bJQueryUI" : true,
    "info":true,
    "bAutoWidth": false,
    "iDisplayLength": 10,
    "aLengthMenu": [[10, 15], [10, 15]],
    "sPaginationType" : "full_numbers",
    "ajax" : "PaginationAction",
    "aoColumns": [
         {"mData":"description","bSearchable": true,"bSortable": true},
         {"mData":"catergory.userCodeName","bSearchable": false,"bSortable": false},
         {"mData":"payee.payeeName","bSearchable": false,"bSortable": false},
         {"mData":"transactionDate","bSearchable": false,"bSortable": false},
         {"mData":"amount","sWidth":"30px","bSearchable": false,"bSortable": false}]
});

Action Class variables:

private int iTotalRecords;
private int iTotalDisplayRecords;
private int iDisplayLength;
private String sEcho;
private String sColumns;
private String sSearch;
private String search;
private String sKeyword;
private List<Income> aaData;

Note: all the integer values are coming as '0' and string variables as null


Solution

  • After several days of Google search, My version of conclusion for the above post is as below.

    Conclusion: Strut2 will not automatically populate the values of datatable properties to action class's property. But the dataTable properties that are sent from the jsp page can be captured using the HttpServletRequest object. See the below code

    HttpServletRequest request=ServletActionContext.getRequest();
    String searchFilterString=request.getParameter("search[value]");
    // here String 'search[value]' is the parameter name  sent by datatable
    

    Here is the link to the list of parameters sent to server from datatables and parameters sent from server to datatable

    Below is the piece of java Code which I have used to trim the List to support my datatable records.

      int fromIndex=0,toIndex=0;    
      int length=Integer.parseInt(request.getParameter("length"); 
      int start=Integer.parseInt(request.getParameter("start");
      int iDisplayLength=Integer.parseInt(request.getParameter("iDisplayLenght");
      if(length>0)
       {
       fromIndex=(int)(Math.ceil((start+1)/lenght)*length);
       toIndex=fromIndex+length;
       }
      else
      {
       fromIndex=(int)(Math.ceil((start+1)/iDisplayLength)*iDisplayLength);
       toIndex=fromIndex+iDisplayLength;
       }
      if(toIndex>getAaData().size())
      {
      toIndex=getAaData().size();
       }
     setAaData(getAaData().subList(fromIndex,toIndex));
    

    Let me know if any one have any doubts regarding the above post and my version of answer. I haven't done any change to the above mentioned JQuery code.

    Any corrections and suggestion for the above post are greatly appreciated

    Note: Different parameters will be passes to server if you are usingsAjaxSource in stead of ajax in the jquery Code. You can get to know about the parameters of datatable in request object.