论坛首页 Java企业应用论坛

仿je分页标签显示

浏览 1656 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-07   最后修改:2009-07-22
这几天一直在看je的分页显示觉得做的非常不错.很合理人性化.于是乎自己也想搞个.网上找了找.貌似je用的是displaytag组件实现的..自己实在是懒.没去看.从网上找了段代码,然后修修补补改成如下格式

不过没有je做的好.有些地方没多想.请大家完善(这里的css是借用je的..robbin老大多包涵)



然后发算法类


package com.smallq.util;

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

public class RefactorPage {
	public static void main(String[] args) {
		List<String> l = RefactorPage.getPageHref(7, 4, "pageAction.do?page");
		for (String s : l) {
			System.out.println(s);
		}
	}

	/**
	 * 分页链接显示
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @return
	 */
	public static List<String> getPageHref(int totalPage, int page, String url) {
		List<String> list = new ArrayList<String>();
		list.add("<div class='pagination'>");
		// 判断是否首页
		if (page != 1) {
			list.add("<a href=" + url + "=" + (page - 1)
					+ " class='prev_page' rel='prev start'>&laquo; 上一页</a> ");
		} else {
			list.add("<span class='disabled prev_page'>&laquo; 上一页</span> ");
		}
		// 如果总页数小于7,全部输出
		if (totalPage < 7) {
			outAll(totalPage, page, url, list);
		}
		// 如果一共7页
		if (totalPage == 7) {
			// 如果是前两页
			if (page < 3) {
				previousTwoPage(page, url, list);
				addFootPage(totalPage, url, list);
			} else
				// 如果是最后两页
				if (page > totalPage - 2) {
					addHeadPage(url, list);
					lastTwoPage(totalPage, page, url, list);
				} else {
					outAll(totalPage, page, url, list);
				}
		}

		// 如果总页数是8
		if (totalPage == 8) {
			// 如果是前两页
			if (page < 3) {
				previousTwoPage(page, url, list);
				addFootPage(totalPage, url, list);
			} else
				// 如果是最后两页
				if (page > totalPage - 2) {
					addHeadPage(url, list);
					lastTwoPage(totalPage, page, url, list);
					// 第三页
				} else if (page == 3) {
					thirdPage(page, url, list);
					addFootPage(totalPage, url, list);
					// 倒数第三页
				} else if (page == totalPage - 2) {
					countDownThirdPage(totalPage, page, url, list);
				} else {
					{
						outAll(totalPage, page, url, list);
					}
				}
		}

		// 如果总页数是9
		if (totalPage == 9) {
			// 如果是前两页
			if (page < 3) {
				previousTwoPage(page, url, list);
				addFootPage(totalPage, url, list);
			} else
				// 如果是最后两页
				if (page > totalPage - 2) {
					addHeadPage(url, list);
					lastTwoPage(totalPage, page, url, list);
					// 第三页
				} else if (page == 3) {
					thirdPage(page, url, list);
					addFootPage(totalPage, url, list);
					// 倒数第三页
				} else if (page == totalPage - 2) {
					countDownThirdPage(totalPage, page, url, list);
					// 第四页
				} else if (page == 4) {
					forthPage(page, url, list);
					addFootPage(totalPage, url, list);
					// 倒数第四页
				} else if (page == 6) {
					addHeadPage(url, list);
					countDownForthPage(totalPage, page, url, list);
				} else {
					{
						outAll(totalPage, page, url, list);
					}

				}
		}

		// 如果总页数是10
		if (totalPage == 10) {
			// 如果是前两页
			if (page < 3) {
				previousTwoPage(page, url, list);
				addFootPage(totalPage, url, list);
			} else
				// 如果是最后两页
				if (page > totalPage - 2) {
					addHeadPage(url, list);
					lastTwoPage(totalPage, page, url, list);
				} else if (page == 3) {
					thirdPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 2) {
					countDownThirdPage(totalPage, page, url, list);
				} else if (page == 4) {
					forthPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 3) {
					addHeadPage(url, list);
					countDownForthPage(totalPage, page, url, list);
				} else if (page == 5) {
					fifthPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 4) {
					addHeadPage(url, list);
					countDownFifthPage(totalPage, page, url, list);
				} else {
					{
						outAll(totalPage, page, url, list);
					}

				}
		}

		if (totalPage > 10) {

			// 如果是前两页
			if (page < 3) {
				previousTwoPage(page, url, list);
				addFootPage(totalPage, url, list);
			} else
				// 如果是最后两页
				if (page > totalPage - 2) {
					addHeadPage(url, list);
					lastTwoPage(totalPage, page, url, list);
					// 如果是第三页
				} else if (page == 3) {
					thirdPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 2) {
					countDownThirdPage(totalPage, page, url, list);
					// 第4页
				} else if (page == 4) {
					forthPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 3) {
					addHeadPage(url, list);
					countDownForthPage(totalPage, page, url, list);
				} else if (page == 5) {
					fifthPage(page, url, list);
					addFootPage(totalPage, url, list);
				} else if (page == totalPage - 4) {
					addHeadPage(url, list);
					countDownFifthPage(totalPage, page, url, list);
				} else {
					addHeadPage(url, list);
					// 中间
					list.add("<a href=" + url + "=" + (page - 1) + ">" + (page - 1)
							+ "</a>");
					list.add("<span class='current'>" + page + "</span>");
					list.add("<a href=" + url + "=" + (page + 1) + ">" + (page + 1)
							+ "</a>");
					// 后段
					addFootPage(totalPage, url, list);
				}
		}
		// 判断是否尾页
		if (page != totalPage) {
			list.add(" <a href=" + url + "=" + (page + 1)
					+ " class='next_page' rel='next'>下一页 &raquo;</a>");
		} else {
			list.add(" <span class='disabled next_page'>下一页 &raquo;</span>");
		}
		list.add("</div>");
		return list;
	}

	/**
	 * 输出全部
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void outAll(int totalPage, int page, String url,
			List<String> list) {
		for (int i = 0; i < totalPage; i++) {
			if ((i + 1) == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
				if (page == totalPage)
					break;
			}
			list.add("<a href=" + url + "=" + (i + 1) + ">" + (i + 1) + "</a>");
		}
	}

	/**
	 * 前两页
	 * 
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void previousTwoPage(int page, String url, List<String> list) {
		for (int i = 0; i < 3; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
			}
			list.add("<a href=" + url + "=" + (i + 1) + " rel='next'>"
					+ (i + 1) + "</a>");
		}
	}

	/**
	 * 后两页
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void lastTwoPage(int totalPage, int page, String url,
			List<String> list) {
		for (int i = totalPage - 3; i < totalPage; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
				if (page == totalPage)
					break;
			}
			list.add("<a href=" + url + "=" + (i + 1) + ">" + (i + 1) + "</a>");
		}
	}

	/**
	 * 第三页
	 * 
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void thirdPage(int page, String url, List<String> list) {
		for (int i = 0; i < 4; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
			}
			list.add("<a href=" + url + "=" + (i + 1) + " rel='next'>"
					+ (i + 1) + "</a>");
		}
	}

	/**
	 * 倒数第三页
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void countDownThirdPage(int totalPage, int page, String url,
			List<String> list) {
		addHeadPage(url, list);
		for (int i = totalPage - 4; i < totalPage; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
				if (page == totalPage)
					break;
			}
			list.add("<a href=" + url + "=" + (i + 1) + ">" + (i + 1) + "</a>");
		}
	}

	/**
	 * 第四页
	 * 
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void forthPage(int page, String url, List<String> list) {
		for (int i = 0; i < 5; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
			}
			list.add("<a href=" + url + "=" + (i + 1) + " rel='next'>"
					+ (i + 1) + "</a>");
		}
	}

	/**
	 * 倒数第四页
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void countDownForthPage(int totalPage, int page, String url,
			List<String> list) {
		for (int i = totalPage - 5; i < totalPage; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
				if (page == totalPage)
					break;
			}
			list.add("<a href=" + url + "=" + (i + 1) + ">" + (i + 1) + "</a>");
		}
	}

	/**
	 * 倒数第五页
	 * 
	 * @param totalPage
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void countDownFifthPage(int totalPage, int page, String url,
			List<String> list) {
		for (int i = totalPage - 6; i < totalPage; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
				if (page == totalPage)
					break;
			}
			list.add("<a href=" + url + "=" + (i + 1) + ">" + (i + 1) + "</a>");
		}
	}

	/**
	 * 第五页
	 * 
	 * @param page
	 * @param url
	 * @param list
	 */
	private static void fifthPage(int page, String url, List<String> list) {
		for (int i = 0; i < 6; i++) {
			if (i + 1 == page) {
				list.add("<span class='current'>" + page + "</span>");
				i = i + 1;
			}
			list.add("<a href=" + url + "=" + (i + 1) + " rel='next'>"
					+ (i + 1) + "</a>");
		}
	}

	/**
	 * 添加尾部
	 * 
	 * @param totalPage
	 * @param url
	 * @param list
	 */
	private static void addFootPage(int totalPage, String url, List<String> list) {
		list.add(" <span class='gap'>&hellip;</span> ");
		list.add("<a href=" + url + "=" + (totalPage - 1) + ">"
				+ (totalPage - 1) + "</a>");
		list.add("<a href=" + url + "=" + (totalPage) + ">" + (totalPage)
				+ "</a>");
	}

	/**
	 * 添加头
	 * 
	 * @param url
	 * @param list
	 */
	private static void addHeadPage(String url, List<String> list) {
		list.add("<a href=" + url + "=" + (1) + ">" + (1) + "</a>");
		list.add("<a href=" + url + "=" + (2) + ">" + (2) + "</a>");
		list.add(" <span class='gap'>&hellip;</span> ");
	}
}



css


body {
  text-align: left;
  font-family: Arial, sans-serifarial, Verdana, Helvetica;
  font-size: 12px;
  line-height: 1.5;
  color: black;
}
.pagination {
	padding: 5px;
	float: left;
}

.pagination a,.pagination a:link,.pagination a:visited {
	padding: 2px 5px 2px 5px;
	margin: 2px;
	border: 1px solid #aaaadd;
	text-decoration: none;
	color: #006699;
}

.pagination a:hover,.pagination a:active {
	border: 1px solid #006699;
	color: #000;
	text-decoration: none;
}

.pagination span.current {
	padding: 2px 5px 2px 5px;
	margin: 2px;
	border: 1px solid #006699;
	font-weight: bold;
	background-color: #006699;
	color: #FFF;
}

.pagination span.disabled {
	padding: 2px 5px 2px 5px;
	margin: 2px;
	border: 1px solid #eee;
	color: #ddd;
}



html代码


<%
			PageOP p = new PageOP();
				List<String> pages = p.getPageHref(pageobj.getPageCount(), pageobj
						.getNowPage(),"index.jsp?page");
				for(String s:pages){
					out.print(s);
				}
		%>




算法一点都不简洁.请大家帮忙完善,谢谢了
上例子程序

我就不传包了.太慢,一共需要4个包

dbcp的三个.一个数据库连接.一个spring核心包,大家自己添加好了..spring的是2.5.6的





  • 大小: 6.1 KB
  • 大小: 6.6 KB
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics