<%@ page language="java" import="java.util.*" pageEncoding="gbk"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>jquery-ajax分页
id | name | remark | age | hobby | 操作 |
---|
package com.jiang.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{ System.out.println("p:"+p); System.out.println("orderby:"+orderby); 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), 3); System.out.println("data:"+data);// System.out.println(data); return SUCCESS; }}
打印结果:data:{"pageCount":"4","pageData":[{"id":"1","id_t":"1","name":"1","remark":"1","age":"1","hobby":"1"},{"id":"2","id_t":"2","name":"b","remark":"b","age":"b","hobby":"b"},{"id":"3","id_t":"3","name":"啊","remark":"啊","age":"啊","hobby":"啊"},{"id":"4","id_t":"4","name":"啊","remark":"啊","age":"啊","hobby":"啊"},]}
package com.jiang.DB;import java.sql.ResultSetMetaData;import java.sql.SQLException;import java.util.ArrayList;import java.util.List;import javax.sql.RowSet;public class GetData { 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); } /* * @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(); Listlist=new ArrayList (); for(int i=0;i
源:http://a881127b.iteye.com/blog/972277