首页 > 代码库 > EasyUI 页面分页

EasyUI 页面分页

DAO

package com.hanqi.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;

import com.hanqi.entity.Region;
import com.hanqi.entity.Student;

public class StudentDAO {


	Configuration  cfg = null;
	ServiceRegistry  sr = null;
	 SessionFactory sf = null;
	 Session  se =null;
	Transaction tr = null;
	
	public StudentDAO()//注册服务
	{
		//1.加载配置文件
				cfg = new Configuration().configure();
				//2.注册服务
				 sr = new  StandardServiceRegistryBuilder()
						.applySettings(cfg.getProperties()).build();
	}
	//初始化
	private void init()
	{
		sf= cfg.buildSessionFactory(sr);
		se = sf.openSession();
		tr = se.beginTransaction();
	}
	//提交和释放
	private void destroy()
	{
		tr.commit();//提交事务
		se.close();
		sf.close();
	}
	
	//获取分页数据集合列表
	public List<Student> getPageList(int page , int rows)
	{
		init();
		 List<Student> rtn = new ArrayList<>(); 
		 rtn = se.createQuery("from Student").setMaxResults(rows)//每页行数
				 .setFirstResult((page-1)*rows).list();//其实页码

		 destroy();
		return rtn;
	}
	
	//获取数据条数
	public int getTotal()
	{
		int rtn= 0;
		init();
		//获取Query对对象,定义集合并实例化
		List<Object> lo = se.createQuery("select count(1) from Student").list();
		
		if(lo != null && lo.size() > 0)
		{
			rtn = Integer.parseInt(lo.get(0).toString());//转换成int并赋值
		}
		
		 destroy();
		
		return rtn;
	}
}

  service

package com.hanqi.service;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.hanqi.dao.StudentDAO;
import com.hanqi.entity.Student;

public class StudentService {
	
	PageJSON<Student> lls = new PageJSON<Student>();
	//查询分页数据
	//返回JSON
	public String getPageJSON(int page, int rows)
	{
		
		
		String rtn = "{total:0,rows:[]}";//空的JSON对象
		
		int total = new StudentDAO().getTotal();
		if(total>0)
		{
			List<Student> ls = new StudentDAO().getPageList(page, rows);
			
			String ls_json = JSONArray.toJSONString(ls);//转成JSON格式
			
			//转义字符,转成JSON读取的格式
			
			rtn =  "{\"total\":"+total+",\"rows\":"+ls_json+"}" ;

			
		}
		
		return rtn;
	}

}

  Servlet

package com.hanqi.web;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hanqi.service.StudentService;

/**
 * Servlet implementation class StudentServlet
 */
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//转码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html");
		
		//接受参数
		String spage = request.getParameter("page");//页码
		String srows = request.getParameter("rows");//每页行数
		
		if(spage!= null && srows != null)
		{
			//转型
			int page = Integer.parseInt(spage);
			int rows = Integer.parseInt(srows);
		
			String json = new StudentService().getPageJSON(page, rows);
			response.getWriter().println(json);
		}
		else
		{
			response.getWriter().println("{total:0,rows:[]}");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  页面

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<!-- 顺序不可以乱 -->
<!-- 1.jQuery的js包 -->
<script type="text/javascript" src="http://www.mamicode.com/jquery-easyui-1.4.4/jquery.min.js"></script>
<!-- 2.css资源 -->
<link  rel="stylesheet" type="text/css" href="http://www.mamicode.com/jquery-easyui-1.4.4/themes/default/easyui.css">
<!-- 3. 图标资源 -->
<link  rel="stylesheet" type="text/css" href="http://www.mamicode.com/jquery-easyui-1.4.4/themes/icon.css">
<!-- 4.easyui的js包 -->
<script type="text/javascript" src="http://www.mamicode.com/jquery-easyui-1.4.4/jquery.easyui.min.js"></script>
<!-- 5.本地语言 -->
<script type="text/javascript" src="http://www.mamicode.com/jquery-easyui-1.4.4/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
	
	$("#hh").datagrid({
		
		url:‘StudentServlet‘,
	//冻结列
	frozenColumns:[[
	                {field:‘id‘,checkbox:true},//复选框
	                {field:‘sno‘,title:‘学号‘,width:100}  
	                
	                
	                ]],
	              //定义列
	        	    columns:[[    

	        	        {field:‘sname‘,title:‘姓名‘,width:200,align:‘center‘},    
	        	        {field:‘ssex‘,title:‘性别‘,width:200,align:‘center‘,
	        	        	formatter: function(value,row,index){
	        	        		if(value =http://www.mamicode.com/= ‘f‘)"hh"></table>
</body>
</html>

  技术分享

技术分享

 

EasyUI 页面分页