`
谷超
  • 浏览: 163397 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JQuery paganition

 
阅读更多

前阶段使用了JQuery paganition进行分页,写一个简单的例子与大家分享一下!

实体类User

 

package com.guchao.pagination.entity;

import java.util.Date;

public class User {

	private int id;
	
	private String username;
	
	private int age;
	
	private Date birthday;

	public User(){}
	public User(int id, String username, int age, Date birthday) {
		this.id = id;
		this.username = username;
		this.age = age;
		this.birthday = birthday;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	
}

 

 

请求的Servlet

 

package com.guchao.pagination.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.LinkedList;
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.JSONObject;
import net.sf.json.JsonConfig;

import com.guchao.pagination.entity.ResultJSonObject;
import com.guchao.pagination.entity.User;
import com.guchao.pagination.util.JsonDateValueProcessor;

public class TestPaginationServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		int pageNo = Integer.parseInt(req.getParameter("pageNo"));
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		
		System.out.println(pageNo+","+pageSize);
		int startRow = pageNo * pageSize;
		Connection con = null;
		Statement stmt = null;
		ResultSet rs = null;
		int count = 0;
		List data = new LinkedList();
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			con = DriverManager.getConnection("jdbc:mysql://localhost:3306/myhibernate", "root", "mysql");
			stmt = con.createStatement();
			String countSql = "select count(*) from t_user";
			rs = stmt.executeQuery(countSql);
			if(rs.next()){
				count = rs.getInt(1);
			}
			String sql = "select id,username,age,birthday from t_user limit "+startRow+","+pageSize;
			rs = stmt.executeQuery(sql);
			while(rs.next()){
				User u = new User(rs.getInt("id"),rs.getString("username"),rs.getInt("age"),rs.getDate("birthday"));
				data.add(u);
				System.out.println(rs.getInt("id")+","+rs.getString("username")+","+rs.getInt("age")+","+rs.getDate("birthday"));
			}
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} finally{
			try {
				rs.close();
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		ResultJSonObject returnObject = new ResultJSonObject();
		returnObject.setTotal(count);
		returnObject.setResult(data);
		//因为返回数据中带有日期类型,必须要使用这个处理类进行处理
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class, new JsonDateValueProcessor("yyyy-MM-dd"));
		JSONObject jsonObject = JSONObject.fromObject(returnObject, config);
		System.out.println(jsonObject.toString());
		resp.getWriter().write(jsonObject.toString());
	}
}

 

 

返回JSON对象类

 

package com.guchao.pagination.entity;

import java.util.List;

public class ResultJSonObject {

	public int total;
	
	public List<Object> result;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List<Object> getResult() {
		return result;
	}

	public void setResult(List<Object> result) {
		this.result = result;
	}
}

 

 

JSON日期类型的处理类

 

package com.guchao.pagination.util;

import java.text.SimpleDateFormat;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

public class JsonDateValueProcessor implements JsonValueProcessor {

	public static final String Default_DATE_PATTERN = "yyyy-MM-dd";  
	private SimpleDateFormat dateFormat;
	
	public JsonDateValueProcessor(String datePattern){
		try {  
	         dateFormat = new SimpleDateFormat(datePattern);  
	     } catch (Exception e) {
	         dateFormat = new SimpleDateFormat(Default_DATE_PATTERN);   
	     }
	}
     
	public Object processArrayValue(Object value, JsonConfig jsonConfig) {
		return process(value);
	}

	public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {
		return process(value);
	}
	
	private Object process(Object value) {  
        if (value == null) {  
            return "";  
        } else {  
            return dateFormat.format(value);  
        }  
    } 
}

 

 

web.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	
	<servlet>
		<servlet-name>TestPagination</servlet-name>
		<servlet-class>com.guchao.pagination.servlet.TestPaginationServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>TestPagination</servlet-name>
		<url-pattern>/testPagination.do</url-pattern>
	</servlet-mapping>
	
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

 

JSP页面

 

<%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%
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>My JSP 'index.jsp' starting page</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" content="This is my page">
	<script type="text/javascript" src="jquery-1.7.1.js"></script>
	<script type="text/javascript" src="jquery.pagination.js"></script>
	<link rel="styleSheet" type="text/css" href="pagination.css">
	<script type="text/javascript">
	
		var items_per_page = 3;//每页显示记录数  
		var page_index = 0;//起始页
		
		function getDataList(index){
			$.ajax({
				type:"POST",
				url:"testPagination.do",//请求servlet
				data:"pageNo="+index+"&pageSize="+items_per_page,//请求参数,页码号和每页显示的记录数
				dataType:"json",//返回值类型为JSON
				contentType:"application/x-www-form-urlencoded",
				success:function(msg){
					var total = msg.total;//记录数
					var html="<table><tr><th>id</th><th>name</th><th>age</th><th>birthday</th></tr>";
					
					$.each(msg.result,function(i,n){
						html+= "<tr><td>"+n.id+"</td><td>"+n.username+"</td><td>"+n.age+"</td><td>"+n.birthday+"</td></tr>";
					});
					html += "</table>";
					$('#Searchresult').html(html);//数据显示
					//分页组件显示
					if($("#Pagination").html().length == ''){
						$("#Pagination").pagination(total, {
							'items_per_page'      : items_per_page, //每页显示记录数 
			                'num_display_entries' : 10,  //可见的页码数
			                'num_edge_entries'    : 2, //显示边缘页码的数目 
			                'prev_text'           : "上一页",  
			                'next_text'           : "下一页",
			                'callback'            : pageselectCallback  
			             });
			        }
				}
			});
		}
		
		//page_index 页码号
		function pageselectCallback(page_index, jq){
			 getDataList(page_index);
		}
		
		$(document).ready(function(){
		    getDataList(page_index);  
		}); 
	</script>
  </head>
  
  <body>
		<dl id="Searchresult">
			<dt>
				Search Results will be inserted here ...
			</dt>
		</dl>
		<br style="clear: both;" />
		<div id="Pagination" class="pagination"></div>
		<br style="clear: both;" />
	</body>
</html>

 

项目结构和页面效果图请看附件图片!

 

 

  • 大小: 8.7 KB
  • 大小: 26.2 KB
分享到:
评论

相关推荐

    jQuery源码 jQuery源码 jQuery源码

    jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码jQuery源码...

    jquery-3.7.0.min.js(jQuery下载)

    jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)jquery-3.7.0.min.js(jQuery下载)...

    jquery-3.3.1.js和jquery-3.3.1.min.js

    jquery-3.3.1.js和jquery-3.3.1.min.js免费下载哈。jquery-3.3.1.js和jquery-3.3.1.min.js免费下载哈。jquery-3.3.1.js和jquery-3.3.1.min.js免费下载哈。jquery-3.3.1.js和jquery-3.3.1.min.js免费下载哈。jquery-...

    jQuery、jQueryUI及jQueryMobile技巧与示例

    资源名称:jQuery、jQuery UI及jQuery Mobile技巧与示例内容简介:《jQuery、jQuery UI及jQuery Mobile技巧与示例》包括jQuery、jQuery UI、jQuery Mobile以及jQuery插件四部分内容。第一部分介绍jQuery核心库,从...

    jQuery入门jQuery入门

    jQuery入门,jQuery入门,jQuery入门,jQuery入门

    jquery1.7中文手册CHM文档(附jquery1.82chm手册)

    资源名称:jquery1.7 中文手册 CHM文档(附jquery1.82 chm手册)内容简介:因国内jquery中文手册更新太慢了,等了一段时间实在等不下去了,干脆自己动手做一个丰衣足食,时刻更新. 最后感谢Shawphy提供1.4.1版,jehn提供...

    jquery插件库(jquery.treeview插件库)

    jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery.treeview插件库)jquery插件库(jquery....

    开发工具 jquery-1.11.3.min

    开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-1.11.3.min开发工具 jquery-...

    jquery精简版jquery-small.js

    jquery 精简版 jquery 精简版 jquery 精简版jquery 精简版 jquery 精简版 jquery 精简版 jquery 精简版

    前端+jQuery+实现烟花特效

    前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+jQuery+实现烟花特效前端+...

    开发工具 jquery.dataTables.min

    开发工具 jquery.dataTables.min开发工具 jquery.dataTables.min开发工具 jquery.dataTables.min开发工具 jquery.dataTables.min开发工具 jquery.dataTables.min开发工具 jquery.dataTables.min开发工具 jquery....

    jquery1.2.3到3.3.1版本都有

    jquery1.2.3到3.3.1版本都有: jquery-1.10.2.min.js jquery-1.11.1.min.js jquery-1.11.3.min.js jquery-1.2.3.min.js jquery-1.3.2.min.js jquery-1.4.2.min.js jquery-1.4.4.min.js jquery-1.5.2.min.js jquery-...

    jquery文档jquery文档jquery文档

    jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档jquery文档

    jQuery1.12.4+jQuery中文手册.rar

    本压缩包内包含以下文件: jquery-1.12.4.js jquery-1.12.4.min.js jQuery1.11.0_20140330.chm jQueryAPI_CHM.CHM

    JQuery实现异步刷新效果

    jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,jquery,...

    jquery 插件jquery 插件jquery 插件jquery 插件

    jquery 插件jquery 插件jquery 插件jquery 插件

    Jquery智能提示完整全部版本vsdoc.js

    jquery-1.3.2-vsdoc.js jquery-1.8.3.min.js jquery-1.3.2.min.js jquery-1.4.1-vsdoc.js jquery-1.4.1.min.js jquery-1.4.2-vsdoc.js jquery-1.4.2.min.js jquery-1.4.3-vsdoc.js jquery-1.4.3.min.js ...

    jQuery API 3.3.1 中文手册

    jQuery API 3.3.1 中文手册,jQuery是一个JavaScript框架,自面世以来,以其快速、简洁,能够很轻易地处理HTML文档、控制事件、给页面添加动画和Ajax效果等功能使多很多WEB编程者对其非常热爱,本手册旨在帮助广大...

    Ext + Jquery Ext + Jquery Ext + Jquery

    Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery Ext + Jquery

    jQuery全能权威指南:jQuery Core+jQuery Plugin+jQuery UI+jQuery Mobile 源码

    、CSS和JavaScript知识的开发者,内容覆盖了jQuery知识体系的全部内容,包括jQuery Core、jQuery Plugin 、jQuery UI、jQuery Mobile以及大量第三方的插件库和2800多个应用jQuery技术的网页参考。

Global site tag (gtag.js) - Google Analytics