1. Cases prepared by the General class
The generic idea of a javabean entity, which is written in generics, is to reach the table in which you want to make the table pagination in which database. All of the process and judgment of paging are encapsulated in this class, and the paging class is reached by the action or servlet directly calling the paging classes.
Article from: Click to open the link
The calling code in the action takes only one line, and of course encapsulates the properties of the class entity and generates the appropriate set and get methods:
Pagination =new pagination<admin> (Admin.class,pagination.getnowpage ());
Package www.csdn.utils;
Importjava.util.ArrayList;
Importjava.util.List;
/***
*
* @author Yang
*
* Universal Paging Tool class
*/
public class pagination<t> Extendsbasehibernatedao {
Number of records displayed per page
privatestatic Final int PAGESIZE = 5;
Total pages
Privateinteger Countpage;
Current page
Privateinteger Nowpage;
Total number of records
Privateinteger Countrecond;
Current page data
Privatelist<t> entities;
/**
* Default constructor is used to instantiate the privatization variable of the class, there must be a constructor, do not write an error
*/
Publicpagination () {
}
/**
* The argument Builder is used to provide a page-wrapping method for the paging class package externally
*/
Publicpagination (class<t> className, int nowpage) {
this.countrecond= Getcountrecord (className);
this.countpage= This.countrecond% PAGESIZE = 0? This.countrecond
/pagesize:this.countrecond/pagesize + 1;
Handling of the current page
if (nowpage <= 1) {
This.nowpage= 1;
}else {
if (nowpage >= this.countpage) {
This.nowpage= This.countpage;
}else {
This.nowpage= Nowpage;
}
}
The value of the past must be the global nowpage can not be a parameter nowpage, otherwise the interpretation of the next page will not work
This.entities= getnowpageinfor (This.nowpage, className);
}
Publicvoid setcountpage (Integer countpage) {
This.countpage= Countpage;
}
Publicvoid setnowpage (Integer nowpage) {
This.nowpage= Nowpage;
}
Publicvoid Setcountrecond (Integer countrecond) {
This.countrecond= Countrecond;
}
Publicvoid Setentities (list<t> entities) {
this.entities= entities;
}
Publicinteger Getcountpage () {
Returncountpage;
}
Publicinteger Getnowpage () {
Returnnowpage;
}
Publicinteger Getcountrecond () {
Returncountrecond;
}
Publiclist<t> getentities () {
Returnentities;
}
/**
* How to obtain the total number of records from the database
*
* @param className
* @return Method Description: Not directly return the value, by first declaring a variable, the test error will not appear null pointer exception;
* Query's Uniqueresult () method returns a Long value, so it's strongly converted to an integer;
* This ingenious use of the ToString () method to complete the request for strong transfer, this method is only suitable for the case of less records;
* When the number of records is large, you can directly declare the variables used in the above page as long
*/
Publicinteger Getcountrecord (class<t> className) {
inti = 0;
try{
Be sure to remember the writing of the aggregate function in quotation marks, there are spaces before Var from followed by, no error; here var is a random variable temporary name
I= Integer.parseint (This
. GetSession ()
. CreateQuery (
"SelectCount (Var) from" + Classname.getname ()
+ "var"). Uniqueresult (). toString ());
I= Integer.parseint (This
. GetSession ()
. CreateQuery (
"SelectCount (Var) from" + Classname.getname ()
+ "var"). Uniqueresult (). toString ());
}catch (Exception e) {
Thrownew RuntimeException (e);
}finally {
Hibernatesessionfactory.closesession ();
}
Returni;
}
/**
*
* Pagination Method
*
* @param nowpage
* @param className
* @return
*/
@SuppressWarnings ("Unchecked")
Publiclist<t> getnowpageinfor (Integer nowpage, class<t> className) {
list<t> entities = newarraylist<t> ();
try {
Entities= this.getsession (). Createcriteria (ClassName)
. Setfirstresult ((nowPage-1) * PAGESIZE)
. Setmaxresults (PAGESIZE). List ();
}catch (Exception e) {
Thrownew RuntimeException (e);
}finally {
Hibernatesessionfactory.closesession ();
}
Returnentities;
}
}
Pagination implementation interface written by 2.STRUTS2 tags
<% @page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
<% @taglib uri= "/struts-tags" prefix= "s"%>
<! doctypehtml public "-//w3c//dtd HTML 4.01 transitional//en" >
<title>index</title>
<body>
<div align= "center" >
<table border= "1px" cellpadding= "0" cellspacing= "0" >
<thead>
<tralign= "center" bgcolor= "#FAFAF1" height= ">"
<th width= "7%" > Selection </th>
<th width= "7%" >ID</th>
<th width= "20%" > Username </th>
<th width= "20%" > Password </th>
<th width= "40%" > Operations </th>
</tr>
</thead>
<tbody>
<s:iterator var= "entity" value= "pagination.entities" >
<tr>