- 浏览: 88813 次
- 性别:
- 来自: 武汉
文章分类
- 全部博客 (97)
- java (23)
- 自定义标签 (3)
- struts (7)
- SSI (0)
- SSH (6)
- IBATIS (14)
- hibernate (2)
- spring (2)
- ssl认证 (1)
- jstl (2)
- jstl中list嵌套map (0)
- ajax (2)
- lucene (2)
- js (5)
- servlet (1)
- jquery (0)
- javascript (7)
- struts1 (1)
- sql (7)
- 公司面试心得 (3)
- jdk (1)
- ERP行业必备的工具 (1)
- 数据库连接池 (1)
- 日志配置问题 (1)
- 呵呵 (0)
- DOJO (1)
- WS (0)
- 工作流JBPM (1)
- 開發工具準備 (1)
- 注解 (0)
- fdsfsdf (0)
- 分页 (0)
- oracle (1)
最新评论
-
kongchuijian:
System.out.println(Arrays.toStr ...
java中数组的问题 -
wyr123789:
你上面已经定义成字节型的为什么还要强转啊?还有你那个'z'-i ...
新手入门 -
ipanel420:
居然是茁壮的同事
SSH2整合+分页+ajax+js校验用户名 -
yemengfan:
楼主能不能把你这个项目发给我啊。我想看看! yemengfa ...
SSH2整合+分页+ajax+js校验用户名
分页每个项目里面差不多都会用到
我以前耶找了很多个 但最近掌握了一个很好用的分页
先是一个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");
发表评论
-
SSH2整合+分页+ajax+js校验用户名
2011-04-18 18:31 17931 action: FriendAction package ... -
shh的例子
2011-03-24 01:21 910action: EmployeeAction.java @Co ... -
struts+hibernate+spring标准配置文件
2010-10-25 11:01 1150web.xml: <?xml version=& ... -
struts-config.xml文件的配置
2010-10-14 08:37 789<?xml version="1.0" ... -
ssh整合中web教你怎么去写配置文件
2010-10-13 17:21 820web.xml配置 <?xml version=&quo ...
相关推荐
在这个“jsp最简单的分页例子”中,我们将探讨如何使用JavaServer Pages(JSP)技术实现基础的分页功能。 JSP是一种动态网页开发技术,它结合了HTML、CSS和Java代码,使得开发者可以在服务器端生成HTML响应。在分页...
这个"ASP.NET Repeater 简单增删改查分页例子"是一个专门为初学者设计的示例项目,它演示了如何利用Repeater控件以及三层架构来实现一个完整的数据操作和分页功能。 首先,我们来看Repeater控件。Repeater是最基本...
这个“php 分页例子”可能是为了教你如何在PHP中实现简单的分页功能,这对于初学者来说是一项必备技能。下面将详细解释PHP分页的基本原理和实现方法。 1. **分页概念** 分页是一种将大量数据分成若干小块(页)...
本教程以“北大青鸟-JSP分页例子”为主题,旨在帮助开发者理解并掌握如何在JSP中实现分页功能。分页是网页设计中常见的一种技术,它能有效地组织大量数据,提高用户体验,避免一次性加载过多内容导致的页面卡顿。 ...
在这个“EXTJS简单分页例子”中,我们将深入探讨EXTJS如何实现分页功能,以及如何与后台进行交互。 分页是Web应用中常见的数据展示方式,尤其在处理大量数据时,它可以提高页面加载速度并提供良好的用户体验。EXTJS...
本示例主要关注jQuery结合Ajax实现的分页功能,这种技术允许用户在不刷新整个页面的情况下加载新的内容,提高了用户体验。我们将深入探讨jQuery分页的核心概念、实现方法以及如何使用Ajax进行数据获取。 **一、...
下面,我们将详细解析给定的“asp数字分页例子”,并深入理解其工作原理、关键代码段以及如何调用上一篇的asp数字分页代码。 ### 1. ASP数字分页概述 ASP数字分页主要涉及以下几个关键步骤: - **数据库查询**:从...
这个“asp超简单分页例子”很可能是为了展示如何在ASP中进行基础的分页功能。 首先,我们需要了解分页的基本原理。分页主要是通过计算每一页显示的数据量,然后根据用户请求的页码来决定从数据库中取出哪一部分数据...
这个"功能齐全的DataGrid分页例子"着重展示了如何有效地实现DataGrid的分页功能,以提高用户体验并优化服务器性能。 首先,DataGrid的分页涉及到数据绑定和用户交互两部分。在ASP.NET中,DataGrid可以通过两种方式...
这个"bootstrap table分页例子.zip"文件包含了一个展示如何在Bootstrap Table中实现分页功能的示例。Bootstrap Table提供了两种主要的分页方式:简单分页和智能分页。 **简单分页**: 1. **配置项**: 在初始化...
#### 标题:ExtJS .Net分页例子 根据提供的标题“ExtJS .Net分页例子”,本文将详细介绍如何在.NET平台下使用ExtJS实现数据分页功能。ExtJS是一款强大的JavaScript库,用于构建复杂的前端应用,而.NET则是微软提供...
在网页开发中,数据量较大的页面往往需要分页...通过学习和理解这个jQuery分页例子,开发者不仅可以掌握如何在项目中实现分页功能,还能深入理解前端与后端的交互方式,以及如何通过JavaScript和CSS来优化用户体验。
在这个分页例子中,我们将深入探讨如何在Struts MVC中实现高效、灵活的分页功能。 首先,我们从“Model”部分开始。在Struts MVC中,模型通常由业务逻辑对象(Business Logic Objects, BLOs)或持久层对象...
在这个"jqgrid加载本地数据并且分页例子"中,我们将深入探讨如何利用jqGrid实现从本地数据源加载数据并进行分页显示。 首先,要理解jqGrid的工作原理。它基于jQuery库,通过HTML表格元素来渲染数据,并通过AJAX从...
在这个例子中,我们将探讨如何实现基于Hibernate的分页功能,以及如何在DAO层和业务逻辑层进行交互。 首先,我们需要在DAO层设计接口。在`MemberDao`接口中,定义了两个核心方法,用于处理分页查询和获取总记录数。...
在这个“jsp连接sql2000添加删除分页例子”中,我们将探讨如何使用JSP与Microsoft SQL Server 2000数据库进行交互,实现数据的添加、删除功能,并结合分页技术来提高用户体验。 首先,连接JSP与SQL Server 2000需要...
一个功能齐全的DataGrid分页例子一个功能齐全的DataGrid分页例子
标题中的“一个VB列表分页例子.rar”表明这是一个关于Visual Basic(VB)编程的实例,主要涉及的是在VB中实现列表数据的分页显示。在Web开发或桌面应用中,当数据量较大时,分页是一种常见的优化用户体验的方法,它...
"分页例子 xiangxi"这个标题可能指的是一个具体的分页实现案例,下面将深入探讨分页的基本原理、实现方式以及相关的技术细节。 1. 分页基本原理: 分页的核心思想是将大数据集划分为多个小部分,每次只加载一部分...
基于这个需求,"basedao分页例子补充"提供了一种通用的解决方案,它结合了分页功能和自定义标签(taglib)的使用,使得在Java Web项目中处理大量数据变得更加便捷和规范。下面我们将详细探讨这两个核心知识点。 ...