OK, last completed the client's pagination, this time we on the last demo on the modification, to achieve the service end of the page ~
JS Code:
<script type= "Text/javascript" >
$ (document). Ready (function () {
$ (' #table_id_example '). DataTable ({
"bprocessing": false,//whether to display load
"Sajaxsource": '/datatabledemo/user/json ',//Request resource path
) ServerSide ": true,//open server processing mode/
*
use AJAX to process data ssource on the service side
: That is," Sajaxsource "
aodata: Parameters to be passed to the service side
Fncallback: callback function to process returned data
/
"Fnserverdata": Function (ssource, Aodata, fncallback) {
$.ajax ({
' type ': ' Post ', '
url ': ssource,
' dataType ': ' json ',
' data ': {' aodata ': Json.stringify (Aodata)} ,
"Success": function (resp) {
fncallback (resp);
}
);
},
"columns": [
{data:] ID "},
{data:" Name "},
{data:" Age "},
]
}
) </script>
Turn on the server processing mode, at which time we each operation of the table, the DataTable will help us to initiate requests to the server for data, the default is to pass the parameters in get way, so we add the following "Fnserverdata", so that you can modify the way the parameters passed by post, So what is the "aodata" sent to the service end is what ghost, do not worry, we take out directly from the servlet to see the
this time our servlet also make a little change:
public class Action extends HttpServlet {
/**
* * */
private static final long Serialversionuid = 595731549 6919679612L;
@Override
protected Void Service (HttpServletRequest request, httpservletresponse response)
throws Servletexception, IOException {
//encoding set
request.setcharacterencoding ("UTF-8");
Response.setcontenttype ("text/html; Charset=utf-8 ");
Response.setcharacterencoding ("UTF-8");
Turn the transmitted JSON string into a JSON array
jsonarray ja = jsonarray.fromobject (request.getparameter ("Aodata"));
System.out.println (JA);
Get data from database
list<user> listuser = Service.getinstance (). GetAll ();
The data is encapsulated and returned to the client
datatablejsonresponse Dtjs = new Datatablejsonresponse (listuser);
Jsonobject jsonobject = Jsonobject.fromobject (Dtjs);
Response.getwriter (). Print (jsonobject.tostring ());
}
At this point we run, OK, and find the so-called parameters that look like
[{"name": "Secho", "Value": 1}, {"Name": "Icolumns", "Value": 3}, {"Name": "Scolumns", " Value ":",, "}, {" Name ":" Idisplaystart "," value ": 0}, {" Name ":" Idisplaylength "," Value ": ten}, {" Name ":" Mdataprop_0 "," Value ": ID"}, {"Name": "Ssearch_0", "Value": "}, {" Name ":" Bregex_0 "," value ": false}, {" Name ":" Bsearchable_0 "," value " : true}, {"Name": "Bsortable_0", "Value": true}, {"Name": "Mdataprop_1", "value": "Name"}, {"Name": "Ssearch_1", "Value": ""
}, {"Name": "Bregex_1", "value": false}, {"Name": "Bsearchable_1", "Value": true}, {"Name": "Bsortable_1", "Value": true}, {' name ': ' mdataprop_2 ', ' value ': ' Age '}, {' name ': ' ssearch_2 ', ' value ': ' '}, {' name ': ' Bregex_2 ', ' value ': false}, {' Name ' : "Bsearchable_2", "Value": true}, {"Name": "Bsortable_2", "Value": true}, {"Name": "Ssearch", "Value": "}, {" Name ":" Bregex "," value ": false}, {" Name ":" Isortcol_0 "," value ": 0}, {" Name ":" Ssortdir_0 "," Value ":" ASC "}, {" Name ":" Isortingcols ', ' Value ': 1}]
is not feeling a little obscure, so this dish online check, reluctantly is to understand this ghost things, and then explain a few of the parameters we need:
Secho: The client sends a request to send over an identity, although there are some eggs do not know, but we have to return the data in the server must carry this logo, oh, the official website of the document said that the server to take out the logo the best turn into an int to prevent XXS attacks (Meng, for the moment, All we know is we need this.
Icolumns: Number of columns
Idisplaystart: Starting Index
Idisplaylength: Number of rows displayed
{' name ': ' Mdataprop_0 ', ' value ': ' ID '},
{' name ': ' Ssearch_0 ', ' Value ': '},
{' name ': ' Bregex_0 ', ' value ': false},
{' name ': ' Bsearchable_0 ', ' Value ': true},
{' name ': ' Bsortable_0 ', ' Value ': true}
Parameter settings for each column, _0 is the first column, which we do not, to the following is to extract the name of the column
Ssearch: Global Search field
Isortcol_0: Sorted Columns
Ssortdir_0: How to sort
Completion of the parameters of interpretation, then the next to extract our parameters ~ ~ Start Adding code
public class Action extends HttpServlet {/** * * */private static final long Serialversionuid = 5957315496919679612
L @Override protected void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOE
Xception {//Encoding set request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("text/html;
Charset=utf-8 ");
Response.setcharacterencoding ("UTF-8");
Turn the transmitted JSON string into a JSON array jsonarray ja = jsonarray.fromobject (request.getparameter ("Aodata"));
System.out.println (JA);
Gets the required parameter value String Secho = null;
Integer icolumns = null;
Integer idisplaystart = null;
Integer idisplaylength = null; list<string> Mdataprop = new arraylist<string> ();
Holds column name String ssearch = null;
Integer isortcol_0 = null;
String isortcol = null;
String ssortdir_0 = null; for (int i = 0; i < ja.size (); i++) {if (Ja.getjsonobject (i). getString ("name"). Equals ("Secho")) Secho = Ja.getjson
Object (i). GetString ("value"); else if (Ja.getjsonoBject (i). getString ("name"). Equals ("Icolumns")) Icolumns = integer.valueof (Ja.getjsonobject (i). GetString ("value")
; else if (Ja.getjsonobject (i). getString ("name"). Equals ("Idisplaystart")) Idisplaystart = Integer.valueof (
Ja.getjsonobject (i). GetString ("value")); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Idisplaylength")) Idisplaylength = Integer.valueof (
Ja.getjsonobject (i). GetString ("value"));
else if (Ja.getjsonobject (i). getString ("name"). Equals ("Ssearch")) Ssearch = Ja.getjsonobject (i). GetString ("value"); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Isortcol_0")) Isortcol_0 = Integer.valueof (Ja.getjsonobject (
i). GetString ("value")); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Ssortdir_0")) Ssortdir_0 = Ja.getjsonobject (i). GetString ("
Value "); else if (icolumns!= null) {for (int j = 0; J < Icolumns; J +) if (Ja.getjsonobject (i). getString ("name"). Equals (" Mdataprop_ "+ j)" Mdataprop.add (Ja.getjsonobject (i). getString ("ValuE "));
} Isortcol = Mdataprop.get (ISORTCOL_0);
System.out.println (Secho);
System.out.println (Icolumns);
System.out.println (Idisplaystart);
System.out.println (idisplaylength);
System.out.println (Ssearch);
System.out.println (Isortcol);
System.out.println (SSORTDIR_0);
Get data from database list<user> Listuser = Service.getinstance (). GetAll ();
The data is encapsulated and returned to the client datatablejsonresponse Dtjs = new Datatablejsonresponse (listuser);
Jsonobject jsonobject = Jsonobject.fromobject (Dtjs);
Response.getwriter (). Print (jsonobject.tostring ());
}
OK, finished processing, then we run a run to see if we want to play the parameters, here I do not post it out, you ran to see that the good found the parameters we want, then we can splice our SQL statements, At the same time we have to make a small change to our datatablejsonresponse, because here for the returned data is also a parameter requirements, then we put the required parameters, the code is as follows:
public class Datatablejsonresponse {Object secho; Object itotalrecords; Number of records in Query Object itotaldisplayrecords;
The filtered record number Object aadata;
Public Datatablejsonresponse () {super ();
Public Datatablejsonresponse (Object aadata) {super ();
This.aadata = Aadata; } public Datatablejsonresponse (object Secho, Object Itotalrecords, Object Itotaldisplayrecords, Object Aadata) {super (
);
This.secho = Secho;
This.itotalrecords = Itotalrecords;
This.itotaldisplayrecords = Itotaldisplayrecords;
This.aadata = Aadata;
Public Object Getaadata () {return aadata;
The public void Setaadata (Object aadata) {this.aadata = Aadata;
Public Object Getsecho () {return secho;
The public void Setsecho (Object secho) {this.secho = Secho;
Public Object Getitotalrecords () {return itotalrecords;
The public void Setitotalrecords (Object itotalrecords) {this.itotalrecords = Itotalrecords;
Public Object Getitotaldisplayrecords () {return itotaldisplayrecords; } public void Setitotaldisplayrecords (Object itotaldisplayrecords) {this.itotaldisplayrecords = Itotaldisplayrecords;
}
}
Full servlet:
public class Action extends HttpServlet {/** * * */private static final long Serialversionuid = 5957315496919679612
L @Override protected void Service (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOE
Xception {//Encoding set request.setcharacterencoding ("UTF-8"); Response.setcontenttype ("text/html;
Charset=utf-8 ");
Response.setcharacterencoding ("UTF-8");
Turn the transmitted JSON string into a JSON array jsonarray ja = jsonarray.fromobject (request.getparameter ("Aodata"));
Gets the required parameter value String Secho = null;
Integer icolumns = null;
Integer idisplaystart = null;
Integer idisplaylength = null; list<string> Mdataprop = new arraylist<string> ();
Holds column name String ssearch = null;
Integer isortcol_0 = null;
String isortcol = null;
String ssortdir_0 = null; for (int i = 0; i < ja.size (); i++) {if (Ja.getjsonobject (i). getString ("name"). Equals ("Secho")) Secho = Ja.getjson
Object (i). GetString ("value"); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Icolumns")) Icolumns = integer.valueof (Ja.getjsonobject (i) getString ("value")); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Idisplaystart")) Idisplaystart = Integer.valueof (
Ja.getjsonobject (i). GetString ("value")); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Idisplaylength")) Idisplaylength = Integer.valueof (
Ja.getjsonobject (i). GetString ("value"));
else if (Ja.getjsonobject (i). getString ("name"). Equals ("Ssearch")) Ssearch = Ja.getjsonobject (i). GetString ("value"); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Isortcol_0")) Isortcol_0 = Integer.valueof (Ja.getjsonobject (
i). GetString ("value")); else if (Ja.getjsonobject (i). getString ("name"). Equals ("Ssortdir_0")) Ssortdir_0 = Ja.getjsonobject (i). GetString ("
Value "); else if (icolumns!= null) {for (int j = 0; J < Icolumns; J +) if (Ja.getjsonobject (i). getString ("name"). Equals ("
Mdataprop_ "+ j)" Mdataprop.add (Ja.getjsonobject (i). GetString ("value")); }} Isortcol =Mdataprop.get (ISORTCOL_0);
String sql = null; if (Ssearch.equals ("")) sql = "SELECT *" (select Id,name,age,rownum rn from dtdemo_xxx) "+" where RN between "+ Idis
Playstart + "and" + (Idisplaystart+idisplaylength) + "ORDER BY" + Isortcol + "" + ssortdir_0; else sql = "SELECT * from" (select Id,name,to_char (age,999) age,rownum RN to dtdemo_xxx where ID like '% + Ssearch + ' % ' or name like '% ' + ssearch + '% ' or ' + '% ' + ssearch + '% ') ' + ' where rn between ' + Idisplaystart + ' and ' + (Idis
Playstart+idisplaylength) + "ORDER BY" + Isortcol + "" + ssortdir_0;
SYSTEM.OUT.PRINTLN (SQL);
Get data from database list<user> Listuser = Service.getinstance (). GETALL (SQL);
Gets the number of records int size = Service.getinstance (). GetAll (). Size ();
The data is encapsulated and returned to the client datatablejsonresponse Dtjs = new Datatablejsonresponse (secho,size,size,listuser);
Jsonobject jsonobject = Jsonobject.fromobject (Dtjs);
Response.getwriter (). Print (jsonobject.tostring ());
}
}
At this point, the service end page completion ~
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.