分页每个项目里面差不多都会用到
我以前耶找了很多个 但最近掌握了一个很好用的分页
先是一个page的bean:
Java代码
1.package com.leatherstore.other;
2.
3.public class Page {
4.
5./** 是否有上一页 */
6.private boolean hasPrePage;
7.
8./** 是否有下一页 */
9.private boolean hasNextPage;
10.
11./** 每页的数量 */
12.private int everyPage;
13.
14./** 总页数 */
15.private int totalPage;
16.
17./** 当前页*/
18.private int currentPage;
19.
20./** 起始点 */
21.private int beginIndex;
22.
23./** 总记录数*/
24.private int totalCount;
25.
26./**
27.* @return totalCount
28.*/
29.public int getTotalCount() {
30.return totalCount;
31.}
32.
33./**
34.* @param totalCount 要设置的 totalCount
35.*/
36.public void setTotalCount(int totalCount) {
37.this.totalCount = totalCount;
38.}
39.
40./** The default constructor */
41.public Page(){
42.
43.}
44.
45./** construct the page by everyPage
46.* @param everyPage
47.* */
48.public Page(int everyPage){
49.this.everyPage = everyPage;
50.}
51.
52./** The whole constructor */
53.public Page(boolean hasPrePage, boolean hasNextPage,
54.int everyPage, int totalPage,
55.int currentPage, int beginIndex,int totalCount) {
56.this.hasPrePage = hasPrePage;
57.this.hasNextPage = hasNextPage;
58.this.everyPage = everyPage;
59.this.totalPage = totalPage;
60.this.currentPage = currentPage;
61.this.beginIndex = beginIndex;
62.this.totalCount = totalCount;
63.}
64.
65./**
66.* @return
67.* Returns the beginIndex.
68.*/
69.public int getBeginIndex() {
70.return beginIndex;
71.}
72.
73./**
74.* @param beginIndex
75.* The beginIndex to set.
76.*/
77.public void setBeginIndex(int beginIndex) {
78.this.beginIndex = beginIndex;
79.}
80.
81./**
82.* @return
83.* Returns the currentPage.
84.*/
85.public int getCurrentPage() {
86.return currentPage;
87.}
88.
89./**
90.* @param currentPage
91.* The currentPage to set.
92.*/
93.public void setCurrentPage(int currentPage) {
94.this.currentPage = currentPage;
95.}
96.
97./**
98.* @return
99.* Returns the everyPage.
100.*/
101.public int getEveryPage() {
102.return everyPage;
103.}
104.
105./**
106.* @param everyPage
107.* The everyPage to set.
108.*/
109.public void setEveryPage(int everyPage) {
110.this.everyPage = everyPage;
111.}
112.
113./**
114.* @return
115.* Returns the hasNextPage.
116.*/
117.public boolean getHasNextPage() {
118.return hasNextPage;
119.}
120.
121./**
122.* @param hasNextPage
123.* The hasNextPage to set.
124.*/
125.public void setHasNextPage(boolean hasNextPage) {
126.this.hasNextPage = hasNextPage;
127.}
128.
129./**
130.* @return
131.* Returns the hasPrePage.
132.*/
133.public boolean getHasPrePage() {
134.return hasPrePage;
135.}
136.
137./**
138.* @param hasPrePage
139.* The hasPrePage to set.
140.*/
141.public void setHasPrePage(boolean hasPrePage) {
142.this.hasPrePage = hasPrePage;
143.}
144.
145./**
146.* @return Returns the totalPage.
147.*
148.*/
149.public int getTotalPage() {
150.return totalPage;
151.}
152.
153./**
154.* @param totalPage
155.* The totalPage to set.
156.*/
157.public void setTotalPage(int totalPage) {
158.this.totalPage = totalPage;
159.}
160.}
package com.leatherstore.other;
public class Page {
/** 是否有上一页 */
private boolean hasPrePage;
/** 是否有下一页 */
private boolean hasNextPage;
/** 每页的数量 */
private int everyPage;
/** 总页数 */
private int totalPage;
/** 当前页*/
private int currentPage;
/** 起始点 */
private int beginIndex;
/** 总记录数*/
private int totalCount;
/**
* @return totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount 要设置的 totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex,int totalCount) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
} 然后构建一个page的工厂PageUtil:
Java代码
1.package com.leatherstore.other;
2.
3.public class PageUtil {
4./**
5.* Use the origin page to create a new page
6.*
7.* @param page
8.* @param totalRecords
9.* @return
10.*/
11.public static Page createPage(Page page, int totalRecords) {
12.return createPage(page.getEveryPage(), page.getCurrentPage(),
13.totalRecords);
14.}
15.
16./**
17.* the basic page utils not including exception handler
18.*
19.* @param everyPage
20.* @param currentPage
21.* @param totalRecords
22.* @return page
23.*/
24.public static Page createPage(int everyPage, int currentPage,
25.int totalRecords) {
26.everyPage = getEveryPage(everyPage);
27.currentPage = getCurrentPage(currentPage);
28.int beginIndex = getBeginIndex(everyPage, currentPage);
29.int totalPage = getTotalPage(everyPage, totalRecords);
30.boolean hasNextPage = hasNextPage(currentPage, totalPage);
31.boolean hasPrePage = hasPrePage(currentPage);
32.
33.return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
34.currentPage, beginIndex, totalRecords);
35.}
36.
37.private static int getEveryPage(int everyPage) {
38.return everyPage == 0 ? 10 : everyPage;
39.}
40.
41.private static int getCurrentPage(int currentPage) {
42.return currentPage == 0 ? 1 : currentPage;
43.}
44.
45.private static int getBeginIndex(int everyPage, int currentPage) {
46.return (currentPage - 1) * everyPage;
47.}
48.
49.private static int getTotalPage(int everyPage, int totalRecords) {
50.int totalPage = 0;
51.
52.if (totalRecords % everyPage == 0)
53.totalPage = totalRecords / everyPage;
54.else
55.totalPage = totalRecords / everyPage + 1;
56.
57.return totalPage;
58.}
59.
60.private static boolean hasPrePage(int currentPage) {
61.return currentPage == 1 ? false : true;
62.}
63.
64.private static boolean hasNextPage(int currentPage, int totalPage) {
65.return currentPage == totalPage || totalPage == 0 ? false : true;
66.}
67.
68.}
package com.leatherstore.other;
public class PageUtil {
/**
* Use the origin page to create a new page
*
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}
/**
* the basic page utils not including exception handler
*
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage,
int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, totalRecords);
}
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
} 然后建一个专用的bean:
Java代码
1.package com.leatherstore.hibernate.domain;
2.
3.import java.util.List;
4.
5.import com.leatherstore.other.Page;
6.
7.public class Result {
8.private Page page; //分页信息
9.private List content; //每页显示的集合
10./**
11.* The default constructor
12.*/
13.public Result() {
14.super();
15.}
16.
17./**
18.* The constructor using fields
19.*
20.* @param page
21.* @param content
22.*/
23.public Result(Page page, List content) {
24.this.page = page;
25.this.content = content;
26.}
27.
28./**
29.* @return Returns the content.
30.*/
31.public List getContent() {
32.return content;
33.}
34.
35./**
36.* @return Returns the page.
37.*/
38.public Page getPage() {
39.return page;
40.}
41.
42./**
43.* @param content
44.* The content to set.
45.*/
46.public void setContent(List content) {
47.this.content = content;
48.}
49.
50./**
51.* @param page
52.* The page to set.
53.*/
54.public void setPage(Page page) {
55.this.page = page;
56.}
57.}
package com.leatherstore.hibernate.domain;
import java.util.List;
import com.leatherstore.other.Page;
public class Result {
private Page page; //分页信息
private List content; //每页显示的集合
/**
* The default constructor
*/
public Result() {
super();
}
/**
* The constructor using fields
*
* @param page
* @param content
*/
public Result(Page page, List content) {
this.page = page;
this.content = content;
}
/**
* @return Returns the content.
*/
public List getContent() {
return content;
}
/**
* @return Returns the page.
*/
public Page getPage() {
return page;
}
/**
* @param content
* The content to set.
*/
public void setContent(List content) {
this.content = content;
}
/**
* @param page
* The page to set.
*/
public void setPage(Page page) {
this.page = page;
}
}
然后在数据访问层写两个方法:
Java代码
1.ProductDAO:
2.public List getProductByPage(Page page);
3.public int getProductCount(); //返回数据的总数
4.ProductDAO的接口实现ProductDAOImpl:
5.//为了在spring里统一编程风格:我用的回调的方法
6.public List getProductByPage(final Page page) {
7.return this.getHibernateTemplate().executeFind(new HibernateCallback(){
8.public Object doInHibernate(Session session) throws HibernateException, SQLException {
9.Query query=session.createQuery("from Productinfo");
10.query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
11.query.setMaxResults(page.getEveryPage());
12.return query.list();
13.}
14.});
15.}
16.
17.public int getProductCount() {
18.List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
19.return ((Integer)list.iterator().next()).intValue();
20.}
ProductDAO:
public List getProductByPage(Page page);
public int getProductCount(); //返回数据的总数
ProductDAO的接口实现ProductDAOImpl:
//为了在spring里统一编程风格:我用的回调的方法
public List getProductByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery("from Productinfo");
query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
query.setMaxResults(page.getEveryPage());
return query.list();
}
});
}
public int getProductCount() {
List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
return ((Integer)list.iterator().next()).intValue();
} 然后是业务层:
Java代码
1.IProduct:
2.public Result listProduct(Page page);
IProduct:
public Result listProduct(Page page); 然后IProduct接口的实现:IProductImpl:
Java代码
1.private ProductDAO productDAO;
2.public void setProductDAO(ProductDAO productDAO){
3.this.productDAO=productDAO;
4.}
5.
6.public Result listProduct(Page page) {
7.int totalRecords = this.productDAO.getProductCount();
8.page = PageUtil.createPage(page, totalRecords);
9.List products = this.productDAO.getProductByPage(page);
10.return new Result(page, products);
11.}
private ProductDAO productDAO;
public void setProductDAO(ProductDAO productDAO){
this.productDAO=productDAO;
}
public Result listProduct(Page page) {
int totalRecords = this.productDAO.getProductCount();
page = PageUtil.createPage(page, totalRecords);
List products = this.productDAO.getProductByPage(page);
return new Result(page, products);
} 然后再代理层:
Java代码
1.ProductProxy:
2.IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
3.
4.public Result productlist(Page page){
5.try{
6.return pro.listProduct(page);
7.}catch(DataAccessException ex){
8.ex.printStackTrace();
9.return null;
10.}
11.}
ProductProxy:
IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
public Result productlist(Page page){
try{
return pro.listProduct(page);
}catch(DataAccessException ex){
ex.printStackTrace();
return null;
}
} 呵呵 终于到productAction啦
显示前方法的代码
Java代码
1.Page page = new Page(); //实例化一个page对象
2.page.setEveryPage(10); //设置每页显示的条数
3.page.setCurrentPage(1); //为第一页
4.Result result = pdp.productlist(page);
5.request.setAttribute("page", pageinfo);
6.request.setAttribute("productlist", list);
7.return mapping.findForward("showProduct");
Page page = new Page(); //实例化一个page对象
page.setEveryPage(10); //设置每页显示的条数
page.setCurrentPage(1); //为第一页
Result result = pdp.productlist(page);
request.setAttribute("page", pageinfo);
request.setAttribute("productlist", list);
return mapping.findForward("showProduct"); 接着就是jsp页面了
Html代码
1.<logic:iterate id="product" name="productlist">
2.//中间迭代所要显示的数据
3.</logic:iterate>
4.<tr>
5.<td width="80" height="30"> </td>
6.<logic:equal value="true" name="page" property="hasPrePage">
7.<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
8.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
9.</logic:equal>
10.<logic:notEqual value="true" name="page" property="hasPrePage">
11.<td width="150" height="30"><div align="right">首页</div></td>
12.<td width="80" height="30"><div align="center">上一页</div></td>
13.</logic:notEqual>
14.<logic:equal value="true" name="page" property="hasNextPage">
15.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
16.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
17.</logic:equal>
18.<logic:notEqual value="true" name="page" property="hasNextPage">
19.<td width="80" height="30"><div align="center">下一页</div></td>
20.<td width="80" height="30"><div align="center">尾页</div></td>
21.</logic:notEqual>
22.<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
23.</tr>
<logic:iterate id="product" name="productlist">
//中间迭代所要显示的数据
</logic:iterate>
<tr>
<td width="80" height="30"> </td>
<logic:equal value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right">首页</div></td>
<td width="80" height="30"><div align="center">上一页</div></td>
</logic:notEqual>
<logic:equal value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center">下一页</div></td>
<td width="80" height="30"><div align="center">尾页</div></td>
</logic:notEqual>
<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
</tr> 可以显示相应的页面信息
然后productAction里面的showProductByTag代码如下:
Java代码
1.Page page = new Page();
2.page.setEveryPage(10);
3.String pagemark = request.getParameter("goto");
4.if (pagemark == null) {
5.String state = request.getParameter("index");
6.String pageno = request.getParameter("pageno");
7.System.out.println("pageno=" + pageno);
8.if ("first".equals(state)) {
9.page.setCurrentPage(1);
10.Result result = pdp.productlist(page);
11.request.setAttribute("page", result.getPage());
12.request.setAttribute("productlist", result.getContent());
13.} else if ("prew".equals(state)) {
14.page.setCurrentPage(Integer.parseInt(pageno));
15.Result result = pdp.productlist(page);
16.request.setAttribute("page", result.getPage());
17.request.setAttribute("productlist", result.getContent());
18.} else if ("next".equals(state)) {
19.page.setCurrentPage(Integer.parseInt(pageno));
20.Result result = pdp.productlist(page);
21.request.setAttribute("page", result.getPage());
22.request.setAttribute("productlist", result.getContent());
23.} else if ("end".equals(state)) {
24.page.setCurrentPage(Integer.parseInt(pageno));
25.Result result = pdp.productlist(page);
26.request.setAttribute("page", result.getPage());
27.request.setAttribute("productlist", result.getContent());
28.}
29.} else {
30.page.setCurrentPage(Integer.parseInt(pagemark));
31.Result result = pdp.productlist(page);
32.request.setAttribute("page", result.getPage());
33.request.setAttribute("productlist", result.getContent());
34.}
35.return mapping.findForward("showProduct");
我以前耶找了很多个 但最近掌握了一个很好用的分页
先是一个page的bean:
Java代码
1.package com.leatherstore.other;
2.
3.public class Page {
4.
5./** 是否有上一页 */
6.private boolean hasPrePage;
7.
8./** 是否有下一页 */
9.private boolean hasNextPage;
10.
11./** 每页的数量 */
12.private int everyPage;
13.
14./** 总页数 */
15.private int totalPage;
16.
17./** 当前页*/
18.private int currentPage;
19.
20./** 起始点 */
21.private int beginIndex;
22.
23./** 总记录数*/
24.private int totalCount;
25.
26./**
27.* @return totalCount
28.*/
29.public int getTotalCount() {
30.return totalCount;
31.}
32.
33./**
34.* @param totalCount 要设置的 totalCount
35.*/
36.public void setTotalCount(int totalCount) {
37.this.totalCount = totalCount;
38.}
39.
40./** The default constructor */
41.public Page(){
42.
43.}
44.
45./** construct the page by everyPage
46.* @param everyPage
47.* */
48.public Page(int everyPage){
49.this.everyPage = everyPage;
50.}
51.
52./** The whole constructor */
53.public Page(boolean hasPrePage, boolean hasNextPage,
54.int everyPage, int totalPage,
55.int currentPage, int beginIndex,int totalCount) {
56.this.hasPrePage = hasPrePage;
57.this.hasNextPage = hasNextPage;
58.this.everyPage = everyPage;
59.this.totalPage = totalPage;
60.this.currentPage = currentPage;
61.this.beginIndex = beginIndex;
62.this.totalCount = totalCount;
63.}
64.
65./**
66.* @return
67.* Returns the beginIndex.
68.*/
69.public int getBeginIndex() {
70.return beginIndex;
71.}
72.
73./**
74.* @param beginIndex
75.* The beginIndex to set.
76.*/
77.public void setBeginIndex(int beginIndex) {
78.this.beginIndex = beginIndex;
79.}
80.
81./**
82.* @return
83.* Returns the currentPage.
84.*/
85.public int getCurrentPage() {
86.return currentPage;
87.}
88.
89./**
90.* @param currentPage
91.* The currentPage to set.
92.*/
93.public void setCurrentPage(int currentPage) {
94.this.currentPage = currentPage;
95.}
96.
97./**
98.* @return
99.* Returns the everyPage.
100.*/
101.public int getEveryPage() {
102.return everyPage;
103.}
104.
105./**
106.* @param everyPage
107.* The everyPage to set.
108.*/
109.public void setEveryPage(int everyPage) {
110.this.everyPage = everyPage;
111.}
112.
113./**
114.* @return
115.* Returns the hasNextPage.
116.*/
117.public boolean getHasNextPage() {
118.return hasNextPage;
119.}
120.
121./**
122.* @param hasNextPage
123.* The hasNextPage to set.
124.*/
125.public void setHasNextPage(boolean hasNextPage) {
126.this.hasNextPage = hasNextPage;
127.}
128.
129./**
130.* @return
131.* Returns the hasPrePage.
132.*/
133.public boolean getHasPrePage() {
134.return hasPrePage;
135.}
136.
137./**
138.* @param hasPrePage
139.* The hasPrePage to set.
140.*/
141.public void setHasPrePage(boolean hasPrePage) {
142.this.hasPrePage = hasPrePage;
143.}
144.
145./**
146.* @return Returns the totalPage.
147.*
148.*/
149.public int getTotalPage() {
150.return totalPage;
151.}
152.
153./**
154.* @param totalPage
155.* The totalPage to set.
156.*/
157.public void setTotalPage(int totalPage) {
158.this.totalPage = totalPage;
159.}
160.}
package com.leatherstore.other;
public class Page {
/** 是否有上一页 */
private boolean hasPrePage;
/** 是否有下一页 */
private boolean hasNextPage;
/** 每页的数量 */
private int everyPage;
/** 总页数 */
private int totalPage;
/** 当前页*/
private int currentPage;
/** 起始点 */
private int beginIndex;
/** 总记录数*/
private int totalCount;
/**
* @return totalCount
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount 要设置的 totalCount
*/
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex,int totalCount) {
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.totalCount = totalCount;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex() {
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex) {
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage() {
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage() {
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage) {
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage() {
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage) {
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage() {
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage) {
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage() {
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
} 然后构建一个page的工厂PageUtil:
Java代码
1.package com.leatherstore.other;
2.
3.public class PageUtil {
4./**
5.* Use the origin page to create a new page
6.*
7.* @param page
8.* @param totalRecords
9.* @return
10.*/
11.public static Page createPage(Page page, int totalRecords) {
12.return createPage(page.getEveryPage(), page.getCurrentPage(),
13.totalRecords);
14.}
15.
16./**
17.* the basic page utils not including exception handler
18.*
19.* @param everyPage
20.* @param currentPage
21.* @param totalRecords
22.* @return page
23.*/
24.public static Page createPage(int everyPage, int currentPage,
25.int totalRecords) {
26.everyPage = getEveryPage(everyPage);
27.currentPage = getCurrentPage(currentPage);
28.int beginIndex = getBeginIndex(everyPage, currentPage);
29.int totalPage = getTotalPage(everyPage, totalRecords);
30.boolean hasNextPage = hasNextPage(currentPage, totalPage);
31.boolean hasPrePage = hasPrePage(currentPage);
32.
33.return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
34.currentPage, beginIndex, totalRecords);
35.}
36.
37.private static int getEveryPage(int everyPage) {
38.return everyPage == 0 ? 10 : everyPage;
39.}
40.
41.private static int getCurrentPage(int currentPage) {
42.return currentPage == 0 ? 1 : currentPage;
43.}
44.
45.private static int getBeginIndex(int everyPage, int currentPage) {
46.return (currentPage - 1) * everyPage;
47.}
48.
49.private static int getTotalPage(int everyPage, int totalRecords) {
50.int totalPage = 0;
51.
52.if (totalRecords % everyPage == 0)
53.totalPage = totalRecords / everyPage;
54.else
55.totalPage = totalRecords / everyPage + 1;
56.
57.return totalPage;
58.}
59.
60.private static boolean hasPrePage(int currentPage) {
61.return currentPage == 1 ? false : true;
62.}
63.
64.private static boolean hasNextPage(int currentPage, int totalPage) {
65.return currentPage == totalPage || totalPage == 0 ? false : true;
66.}
67.
68.}
package com.leatherstore.other;
public class PageUtil {
/**
* Use the origin page to create a new page
*
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords) {
return createPage(page.getEveryPage(), page.getCurrentPage(),
totalRecords);
}
/**
* the basic page utils not including exception handler
*
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage,
int totalRecords) {
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage, everyPage, totalPage,
currentPage, beginIndex, totalRecords);
}
private static int getEveryPage(int everyPage) {
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage) {
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage) {
return (currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords) {
int totalPage = 0;
if (totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1;
return totalPage;
}
private static boolean hasPrePage(int currentPage) {
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage) {
return currentPage == totalPage || totalPage == 0 ? false : true;
}
} 然后建一个专用的bean:
Java代码
1.package com.leatherstore.hibernate.domain;
2.
3.import java.util.List;
4.
5.import com.leatherstore.other.Page;
6.
7.public class Result {
8.private Page page; //分页信息
9.private List content; //每页显示的集合
10./**
11.* The default constructor
12.*/
13.public Result() {
14.super();
15.}
16.
17./**
18.* The constructor using fields
19.*
20.* @param page
21.* @param content
22.*/
23.public Result(Page page, List content) {
24.this.page = page;
25.this.content = content;
26.}
27.
28./**
29.* @return Returns the content.
30.*/
31.public List getContent() {
32.return content;
33.}
34.
35./**
36.* @return Returns the page.
37.*/
38.public Page getPage() {
39.return page;
40.}
41.
42./**
43.* @param content
44.* The content to set.
45.*/
46.public void setContent(List content) {
47.this.content = content;
48.}
49.
50./**
51.* @param page
52.* The page to set.
53.*/
54.public void setPage(Page page) {
55.this.page = page;
56.}
57.}
package com.leatherstore.hibernate.domain;
import java.util.List;
import com.leatherstore.other.Page;
public class Result {
private Page page; //分页信息
private List content; //每页显示的集合
/**
* The default constructor
*/
public Result() {
super();
}
/**
* The constructor using fields
*
* @param page
* @param content
*/
public Result(Page page, List content) {
this.page = page;
this.content = content;
}
/**
* @return Returns the content.
*/
public List getContent() {
return content;
}
/**
* @return Returns the page.
*/
public Page getPage() {
return page;
}
/**
* @param content
* The content to set.
*/
public void setContent(List content) {
this.content = content;
}
/**
* @param page
* The page to set.
*/
public void setPage(Page page) {
this.page = page;
}
}
然后在数据访问层写两个方法:
Java代码
1.ProductDAO:
2.public List getProductByPage(Page page);
3.public int getProductCount(); //返回数据的总数
4.ProductDAO的接口实现ProductDAOImpl:
5.//为了在spring里统一编程风格:我用的回调的方法
6.public List getProductByPage(final Page page) {
7.return this.getHibernateTemplate().executeFind(new HibernateCallback(){
8.public Object doInHibernate(Session session) throws HibernateException, SQLException {
9.Query query=session.createQuery("from Productinfo");
10.query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
11.query.setMaxResults(page.getEveryPage());
12.return query.list();
13.}
14.});
15.}
16.
17.public int getProductCount() {
18.List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
19.return ((Integer)list.iterator().next()).intValue();
20.}
ProductDAO:
public List getProductByPage(Page page);
public int getProductCount(); //返回数据的总数
ProductDAO的接口实现ProductDAOImpl:
//为了在spring里统一编程风格:我用的回调的方法
public List getProductByPage(final Page page) {
return this.getHibernateTemplate().executeFind(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query query=session.createQuery("from Productinfo");
query.setFirstResult(page.getBeginIndex()); //hibernate分页的精髓 呵呵
query.setMaxResults(page.getEveryPage());
return query.list();
}
});
}
public int getProductCount() {
List list=this.getHibernateTemplate().find("select count(*) from Productinfo");
return ((Integer)list.iterator().next()).intValue();
} 然后是业务层:
Java代码
1.IProduct:
2.public Result listProduct(Page page);
IProduct:
public Result listProduct(Page page); 然后IProduct接口的实现:IProductImpl:
Java代码
1.private ProductDAO productDAO;
2.public void setProductDAO(ProductDAO productDAO){
3.this.productDAO=productDAO;
4.}
5.
6.public Result listProduct(Page page) {
7.int totalRecords = this.productDAO.getProductCount();
8.page = PageUtil.createPage(page, totalRecords);
9.List products = this.productDAO.getProductByPage(page);
10.return new Result(page, products);
11.}
private ProductDAO productDAO;
public void setProductDAO(ProductDAO productDAO){
this.productDAO=productDAO;
}
public Result listProduct(Page page) {
int totalRecords = this.productDAO.getProductCount();
page = PageUtil.createPage(page, totalRecords);
List products = this.productDAO.getProductByPage(page);
return new Result(page, products);
} 然后再代理层:
Java代码
1.ProductProxy:
2.IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
3.
4.public Result productlist(Page page){
5.try{
6.return pro.listProduct(page);
7.}catch(DataAccessException ex){
8.ex.printStackTrace();
9.return null;
10.}
11.}
ProductProxy:
IProduct pro=(IProduct)AppContext.getInstance().getappcontext().getBean("productServicewithTran");
public Result productlist(Page page){
try{
return pro.listProduct(page);
}catch(DataAccessException ex){
ex.printStackTrace();
return null;
}
} 呵呵 终于到productAction啦
显示前方法的代码
Java代码
1.Page page = new Page(); //实例化一个page对象
2.page.setEveryPage(10); //设置每页显示的条数
3.page.setCurrentPage(1); //为第一页
4.Result result = pdp.productlist(page);
5.request.setAttribute("page", pageinfo);
6.request.setAttribute("productlist", list);
7.return mapping.findForward("showProduct");
Page page = new Page(); //实例化一个page对象
page.setEveryPage(10); //设置每页显示的条数
page.setCurrentPage(1); //为第一页
Result result = pdp.productlist(page);
request.setAttribute("page", pageinfo);
request.setAttribute("productlist", list);
return mapping.findForward("showProduct"); 接着就是jsp页面了
Html代码
1.<logic:iterate id="product" name="productlist">
2.//中间迭代所要显示的数据
3.</logic:iterate>
4.<tr>
5.<td width="80" height="30"> </td>
6.<logic:equal value="true" name="page" property="hasPrePage">
7.<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
8.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
9.</logic:equal>
10.<logic:notEqual value="true" name="page" property="hasPrePage">
11.<td width="150" height="30"><div align="right">首页</div></td>
12.<td width="80" height="30"><div align="center">上一页</div></td>
13.</logic:notEqual>
14.<logic:equal value="true" name="page" property="hasNextPage">
15.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
16.<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
17.</logic:equal>
18.<logic:notEqual value="true" name="page" property="hasNextPage">
19.<td width="80" height="30"><div align="center">下一页</div></td>
20.<td width="80" height="30"><div align="center">尾页</div></td>
21.</logic:notEqual>
22.<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
23.</tr>
<logic:iterate id="product" name="productlist">
//中间迭代所要显示的数据
</logic:iterate>
<tr>
<td width="80" height="30"> </td>
<logic:equal value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right"><a href="../product.do?method=showProductByTag&index=first&msg=${msg }">首页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=prew&pageno=${page.currentPage -1}&msg=${msg }">上一页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasPrePage">
<td width="150" height="30"><div align="right">首页</div></td>
<td width="80" height="30"><div align="center">上一页</div></td>
</logic:notEqual>
<logic:equal value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=next&pageno=${page.currentPage +1 }&msg=${msg }">下一页</a></div></td>
<td width="80" height="30"><div align="center"><a href="../product.do?method=showProductByTag&index=end&pageno=${page.totalPage }&msg=${msg }">尾页</a></div></td>
</logic:equal>
<logic:notEqual value="true" name="page" property="hasNextPage">
<td width="80" height="30"><div align="center">下一页</div></td>
<td width="80" height="30"><div align="center">尾页</div></td>
</logic:notEqual>
<td height="30" colspan="3"><div align="center">页次${page.currentPage }/${page.totalPage } 共${page.totalCount }条记录</div> <div align="center"></div></td>
</tr> 可以显示相应的页面信息
然后productAction里面的showProductByTag代码如下:
Java代码
1.Page page = new Page();
2.page.setEveryPage(10);
3.String pagemark = request.getParameter("goto");
4.if (pagemark == null) {
5.String state = request.getParameter("index");
6.String pageno = request.getParameter("pageno");
7.System.out.println("pageno=" + pageno);
8.if ("first".equals(state)) {
9.page.setCurrentPage(1);
10.Result result = pdp.productlist(page);
11.request.setAttribute("page", result.getPage());
12.request.setAttribute("productlist", result.getContent());
13.} else if ("prew".equals(state)) {
14.page.setCurrentPage(Integer.parseInt(pageno));
15.Result result = pdp.productlist(page);
16.request.setAttribute("page", result.getPage());
17.request.setAttribute("productlist", result.getContent());
18.} else if ("next".equals(state)) {
19.page.setCurrentPage(Integer.parseInt(pageno));
20.Result result = pdp.productlist(page);
21.request.setAttribute("page", result.getPage());
22.request.setAttribute("productlist", result.getContent());
23.} else if ("end".equals(state)) {
24.page.setCurrentPage(Integer.parseInt(pageno));
25.Result result = pdp.productlist(page);
26.request.setAttribute("page", result.getPage());
27.request.setAttribute("productlist", result.getContent());
28.}
29.} else {
30.page.setCurrentPage(Integer.parseInt(pagemark));
31.Result result = pdp.productlist(page);
32.request.setAttribute("page", result.getPage());
33.request.setAttribute("productlist", result.getContent());
34.}
35.return mapping.findForward("showProduct");