jquery datatable服务端分页(2)

好的,加工完毕,这时我们跑一跑看看是不是打出我们要的参数,这里我就不贴出来了,你们自己跑了看,好的发现拿到了我们要的参数,那么接下来可以拼接我们的sql语句了,同时我们要对我们的DataTableJSONResponse进行一下小小的修改,因为这里对于返回的数据也是有参数要求的,那我们就把需要的参数放进去,代码如下:

public class DataTableJSONResponse { Object sEcho; Object iTotalRecords; //查询的记录数 Object iTotalDisplayRecords; //过滤后的记录数 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; } public void setAaData(Object aaData) { this.aaData = aaData; } public Object getsEcho() { return sEcho; } public void setsEcho(Object sEcho) { this.sEcho = sEcho; } public Object getiTotalRecords() { return iTotalRecords; } public void setiTotalRecords(Object iTotalRecords) { this.iTotalRecords = iTotalRecords; } public Object getiTotalDisplayRecords() { return iTotalDisplayRecords; } public void setiTotalDisplayRecords(Object iTotalDisplayRecords) { this.iTotalDisplayRecords = iTotalDisplayRecords; } }

完整的Servlet:

public class Action extends HttpServlet { /** * */ private static final long serialVersionUID = 5957315496919679612L; @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 编码设置 request.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=UTF-8"); response.setCharacterEncoding("UTF-8"); // 把传送过来的JSON字符串转成JSON数组 JSONArray ja = JSONArray.fromObject(request.getParameter("aodata")); // 获取需要的参数值 String sEcho = null; Integer iColumns = null; Integer iDisplayStart = null; Integer iDisplayLength = null; List<String> mDataProp = new ArrayList<String>(); //存放列名 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.getJSONObject(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 * from(select id,name,age,rownum rn from dtdemo_xxx)" +"where rn between " + iDisplayStart + " and " + (iDisplayStart+iDisplayLength) +" order by " + iSortCol + " " + sSortDir_0; else sql = "select * from(select id,name,to_char(age,999) age,rownum rn from dtdemo_xxx where id like '%" + sSearch +"%' or name like '%"+ sSearch +"%' or age like '%"+ sSearch +"%')" +"where rn between " + iDisplayStart + " and " + (iDisplayStart+iDisplayLength) +" order by " + iSortCol + " " + sSortDir_0; System.out.println(sql); // 从数据库获取数据 List<User> listUser = Service.getInstance().getAll(sql); //获取记录数 int size = Service.getInstance().getAll().size(); // 数据封装并返回给客户端 DataTableJSONResponse dtjs = new DataTableJSONResponse(sEcho,size,size,listUser); JSONObject jsonObject = JSONObject.fromObject(dtjs); response.getWriter().print(jsonObject.toString()); } }

至此,服务端分页完成~

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wzfdsd.html