日期:2014-05-16  浏览次数:20640 次

struts-jquery-ajax高效分页(mysql版)
本文是struts2+jquery-ajax实现的在mySQL下的高效分页
1:先看下预览效果:(30多万数据测试)
 

2:这个实现首先把后台数据库返回的RowSet转化成json格式的字符串,再把这个json格式的字符串传给前台,用jquery解析
  RowSet数据到json格式的数据转化函数如下:
 
/*
      * @param count 数据总条目数
      * @param pageCount 每页显示数据数
      * */
     private static String RowSetToJson(RowSet rt,int count,int pageCount){
    	 ResultSetMetaData rm=null;
    	 StringBuilder sb=new StringBuilder();
    	 try {
			 rm=rt.getMetaData();
			 sb.append("{\"pageCount\":\""+count+"\",\"pageData\":[");
			 int columNum=rm.getColumnCount();
			 List<String> list=new ArrayList<String>();
			 for(int i=0;i<columNum;i++){
				 list.add(rm.getColumnName(i+1));
			 }
			 
			 for(int i=0;i<pageCount&&rt.next();i++){
				 sb.append("{");
				 for(int j=0;j<list.size();j++){
					 if(j!=list.size()-1){
						 sb.append("\""+list.get(j)+"\":\""+rt.getString(list.get(j))+"\",");
					 }else{
						 sb.append("\""+list.get(j)+"\":\""+rt.getString(list.get(j))+"\"");
					 }
				 }
				 if(i!=pageCount-1){
					 sb.append("},");
				 }else{
					 sb.append("}");
				 } 
			 }
			 sb.append("]}");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return sb.toString();
     }

  调用转化函数方法:
 
 public static String getJson(String tableName,String select,String where,String orderby,int pageIndex,int pageCount){
         StringBuilder sb=new StringBuilder();
         sb.append("select "+select+" from "+tableName);
         if(!"".equals(where)){
        	 sb.append(" ");
        	 if(where.startsWith("where")){
        		 sb.append(" "+where);
        	 }else{
        		 sb.append(" where "+where);
        	 }
         }
         sb.append(" "+orderby+" ");
         int temp=(pageIndex-1)*pageCount;
         sb.append(" limit "+temp+","+pageCount);
    	 RowSet rt=DBop.search(sb.toString());
    	 //求数据总条目数
         int rowNum=0;
         rowNum=DBop.getNum(tableName,where);
    	 return RowSetToJson(rt,rowNum,pageCount);
     }


3:在action函数里调用方法:
  
import com.jiang.DB.GetData;
import com.opensymphony.xwork2.ActionSupport;

public class GetPageData extends ActionSupport {
	private String p;//当前页
	private String orderby;//排序规则
	private String data;
     public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public String getP() {
		return p;
	}

	public void setP(String p) {
		this.p = p;
	}

	public String getOrderby() {
		return orderby;
	}

	public void setOrderby(String orderby) {
		this.orderby = orderby;
	}

	public String execute()throws Exception{
    	 if("".equals(orderby)){
    		 orderby="order by id_t";
    	 }
    	 if("".equals(p)){
    		 p=String.valueOf(1);
    	 }
    	 if(Integer.parseInt(p)==0){
    		 p=String.valueOf(1);
    	 }
    	 data=GetData.getJson("pagetest", "*", "","order by id_t", Integer.parseInt(p), 10);
//    	 System.out.println(data);
    	 return SUCCESS;
     }
}
]

4:页面ajax请求代码:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>jquery-ajax分页</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description"