easy ui 中数据表格的分页其实是很简单的,分页是在数据表格可以正常显示数据的基础上进行的,在这里给出servlet的代码,其中selectAll()方法是从数据库中提取所有数据,
分页的一种思路是:从数据表中取出所有数据,然后根据分页的要求取出对应的数据。
package thejavabean; import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONArray; public class Table_info extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { try {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
String page = request.getParameter("page"); // 当前页数
String rows = request.getParameter("rows"); // 每页显示行数 List<Student> allList=selectAll(); //获取所有数据
int p=Integer.parseInt(page);
int r=Integer.parseInt(rows);
int begin=(p-1)*r; //当前页开始项
int num=begin;
int count=r;
List<Student> list=new ArrayList();
System.out.println();
while(count>0&&num<allList.size()){
list.add(allList.get(num));
num++;
count--;
}
int total=selectAll().size();
String json = "{\"total\":"+total+" , \"rows\":"+JSONArray.fromObject(list).toString()+"}"; response.getWriter().write(json.toString());
out.flush();
out.close();
} catch (SQLException e) {
e.printStackTrace();
} }
public List<Student> selectAll() throws SQLException{
StudentManage sm=new StudentManage();
return sm.selectAll(""); }
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response); } }