数据库分页:
mysql:select pname from product limit 0,2;第一个参数是指要开始的地方,第二个参数是指每页显示多少条数据;注意:第一页用0表示。
oracle:rownumber
SqlServer:top
一、服务器端
①新建包com.paging.action中创建CityAction.java类
package com.paging.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONSerializer;
/**
* Servlet implementation class CityAction
*/
@WebServlet("/CityAction")
public class CityAction extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* Default constructor.
*/
public CityAction() {
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
this.doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
request.setCharacterEncoding("utf-8");
PrintWriter writer=response.getWriter();
//String type=request.getParameter("type");
List<String> list=CityDataSource.getCitysList();//生成json对象,json数据一定要带<K,V>
String pageNo=request.getParameter("pageNo");//服务器端接收客户端的一个页码
int currentPage=1;//当前页,当前页是第一页,打开手机进来的时候是第一页
if(pageNo!=null){
currentPage=Integer.parseInt(pageNo);//如果接收到一个值(这个值是传递过来的),则转化成一个整型数据,原因是这个页码是要变化的,而且还要累加
}
DividePage pUtil=new DividePage(25,list.size(), currentPage);//
int start=pUtil.getFromIndex();//从哪开始
int end=pUtil.getToIndex();//到哪结束
List<String> subList=list.subList(start, end);//在总集合中截取集合,模拟分页
Map<String, List<String>>map=new HashMap<String, List<String>>();
map.put("citys", subList);
String jsonString=JSONSerializer.toJSON(map).toString();
writer.println(jsonString);//注意生成json字符串时,这个地方一定要是独立干净的
writer.flush();
writer.close();
}
}
②创建数据源CityDataSource.java
package com.paging.action;
import java.util.ArrayList;
import java.util.List;
public class CityDataSource {
public CityDataSource() {
// TODO Auto-generated constructor stub
}
/**
* 提供数据源
* @return
*/
public static List<String> getCitysList(){
List<String> list=new ArrayList<String>();
for(int i=0;i<300;i++){
list.add("上海"+i);
}//为了增强滑动效果,用for增加数据集合
return list;
}
}
③需要把数据拆分,第一页显示几条,第二页显示几条,所以,页码是变化的,变化由客户端提交变化(手机进来是第一页,再滑动则是第二页,再滑第三页,页码是由客户端提交上来的),创建分页工具类DividePage.java
package com.paging.action;
import java.util.*;
public class DividePage {
private int pageSize;// 每页显示的条数
private int recordCount; // 记录的总条数
private int currentPage;// 当前页
private int pageCount;// 总页数
public DividePage(int pageSize, int recordCount, int currentPage) {
this.pageSize = pageSize;
this.recordCount = recordCount;
this.setCurrentPage(currentPage);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public int getCurrentPage() {
return currentPage;
}
/**
* 设置定位在当前页
*
* @param currentPage
*/
public void setCurrentPage(int currentPage) {
int activePage = currentPage <= 0 ? 1 : currentPage;
activePage = activePage > getPageCount() ? getPageCount()
: activePage;
this.currentPage = activePage;
}
/**
* 获得总页数
*
* @return
*/
public int getPageCount() {
pageCount = recordCount / pageSize;
int mod = recordCount % pageSize;
if (mod != 0) {
pageCount++;
}
return recordCount == 0 ? 1 : pageCount;
}
public int getFromIndex() {
return (currentPage - 1) * pageSize;
}
public int getToIndex() {
return Math.min(recordCount, currentPage * pageSize);
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
}
在地址栏输入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=1显示如下
在地址栏输入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=2显示如下
在地址栏输入http://192.168.1.102:8080/Web_ListView_Paging/CityAction?pageNo=3显示如下