Struts2 + Bootstrap datatable: how to paginate wit

2019-09-09 15:34发布

问题:

I'm working in some app that uses Struts2 framework and in the action i load all the data that we need in some List<Object> and then in the JSP file this list is iterated and displayed in a datatable using bootstrap framework.

This works fine, but i would know how to do this without load the full data in the resulting HTML, like if i have 1.000.000 (one million) of records.

I don't want that all the records are in the resulting HTML output from the JSP, only the 10 first, and with the pagination load (with jQuery?) the rest of the dataset if is needed.

回答1:

To add to the Enrique San Martín's comment: http://legacy.datatables.net/usage/server-side

Any good example should use iDisplayStart and iDisplayLength parameters, and of course "bServerSide": true property of datatable().

Here the link to the blog-post: Using jQuery Datatable with Struts2 action class using Ajax.



回答2:

Well, this is some good aproach, but i have to convert the request.getParameter to struts variable (declared as privated and then generate getters y setters):

FROM: https://datatables.net/development/server-side/jsp

edit: This link have some request.gP with old values from the datatables, instead we have to use the params from:

https://datatables.net/manual/server-side#Sent-parameters

Like in an action:

private String draw;

private String length;

private String order;

private String search;

private String start;

getters&setters{} //or make it public

Then in the actionMethod that is called by the ajax make the query to the db and return the dataset.

I will edit this answer when i have the full pagination working with struts2+datatables,

In the datatable there is some configurations that are necesary (see the docs from datatables.net):

<script>
$(document).ready( function() {
    $('#datatable').dataTable( {
      "iDisplayLength": 5,
      "processing": true,
      "serverSide": true,
      "ajax": "paginateTableAction"
    } );
  } );
</script>


回答3:

The best way is to create a method in your DAO that have as additional parameters start & end for example :

getCustomers( int start,int end){
// get all customers
// sublist the result and get the data from start index to end index
}

in your controller you'll do almost the same and you will give start and end parameters through POST or GET request

@RequestMapping(value = "/sublistedCustomers", method = RequestMethod.GET)
public String getSublistedCustomers(@RequestParam("start") final int start,@RequestParam("end") final int end){
 model.addAttribute("cutomersResult",yourDAO.getCustomers(start,end));
}

now you have a part of customers record on your JSP file and it s up to you to decide how to display this sublisted result.