Java实现标签分页
最近为了开发一个网站,里面要用分页功能,但是之前很少自己写分页标签,又不想用现成框架。所以自己参考了些资料,写了个分页例子测试了一下。
代码主要分为三个类:
- PageTag 分页标签类
- Page 分页bean
- Constant 设置常量
Page代码:
- /**
- *
- * @author byyang
- *
- */
- public class Page
- {
- private int current = 0; //当前页,默认为第一页
- private int size; //记录总数
- private int length; //每页的长度
- private String url; //访问路径
- public Page(int offset, int size, int length) {
- super();
- this.current = offset;
- this.size = size;
- this.length = length;
- }
- /**
- * 获取总页数
- * @return
- */
- public int pageCount(){
- int pagecount = 0;
- if(this.size % this.length == 0){
- pagecount = this.size / this.length;
- }else{
- pagecount = this.size / this.length + 1;
- }
- return pagecount;
- }
- //最后一页开始条数
- public int lastPageOffset(){
- return this.size - lastPageSize();
- }
- //最后一页页大小
- public int lastPageSize(){
- int lastpagesize = 0;
- if(this.size % this.length == 0){
- lastpagesize = this.length;
- }else{
- lastpagesize = this.size % this.length;
- }
- return lastpagesize;
- }
- //获取起始页
- public int getOffset() {
- return current;
- }
- //总记录数
- public int getSize() {
- return size;
- }
- //每页大小
- public int getLength() {
- return length;
- }
- //获取访问路径
- public String getUrl() {
- return url;
- }
- //上一页
- public int getLastOffset(){
- int offset = this.getOffset() - this.getLength();
- if(offset > 0){
- return offset;
- }else{
- return 0;
- }
- }
- //下一页
- public int getNextOffset(){
- int offset = this.getOffset() + this.getLength();
- if(offset > this.getSize()){
- return getLastOffset();
- }else{
- return offset;
- }
- }
- public String getPageNavigation(){
- String pageNavigation = "";
- return pageNavigation;
- }
- public void setOffset(int offset) {
- this.current = offset;
- }
- public void setSize(int size) {
- this.size = size;
- }
- public void setLength(int length) {
- this.length = length;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- }
现在我们要实现PageTag标签类,自定义标签有两种实现TagSupport和SimpleTagSupport,两个都能实现这个功能,这个看个人选择了。
- import java.io.IOException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.jsp.JspException;
- import javax.servlet.jsp.JspWriter;
- import javax.servlet.jsp.tagext.TagSupport;
- import com.byyang.util.Constants;
- /**
- * doStartTag()和doEndTag()返回值处理:
- * SKIP_BODY (0) :跳过了开始和结束标签之间的代码。
- * EVAL_BODY_INCLUDE(1):将body的内容输出到存在的输出流中
- * SKIP_PAGE(5): 忽略剩下的页面。
- * EVAL_PAGE隐含(6):继续执行下面的页
- * @author yangby
- *
- */
- public class PageTag extends TagSupport{
- private String url;
- public String getUrl(){
- return this.url;
- }
- public void setUrl(String url) {
- this.url = url;
- }
- public int doEndTag()throws JspException{
- return EVAL_PAGE;
- }
- public int doStartTag() throws JspException{
- HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
- //获取分页bean
- Page page = (Page)request.getAttribute("pager");
- JspWriter out = this.pageContext.getOut();
- //拼接分页信息
- StringBuilder s = new StringBuilder();
- s.append("共 " + ((page.getSize() - 1) / page.getLength() + 1) + " 页 ");
- //设置当前页第一条记录索引
- String current = request.getParameter("current");
- if(current != null && !"".equals(current)){
- page.setOffset(Integer.parseInt(current));
- }
- if (page.getSize() > page.getLength()){
- //获取连接符pref
- String pref;
- if (this.url.indexOf("?") > -1){
- pref = "&";
- }else {
- pref = "?";
- }
- this.doStart(s, page, pref);
- this.doBody(s, page, pref);
- this.doEnd(s,page,pref);
- }else{
- s.append(" ");
- }
- s.append(" 共 " + page.getSize() + " 条记录");
- try{
- out.println(s.toString());
- }catch (IOException e){
- e.printStackTrace();
- }
- return SKIP_BODY;
- }
- /**
- * //拼接"首页"和"<"
- * @param s
- * @param page
- * @param pref
- */
- public void doStart(StringBuilder s,Page page,String pref){
- if (page.getOffset() > 0){
- s.append("<a href=‘" + this.url + pref +
- "current=0‘>[首页]</a>\n" + "<a href=‘" + this.url + pref +
- "current=" + page.getLastOffset() + "‘>[<]</a>\n");
- }else{
- s.append("<a href=‘" + this.url + pref + "current=0‘>[首页]</a>\n" );
- }
- }
- /**
- * 拼接"尾页"和">"
- * @param s
- * @param page
- * @param pref
- */
- public void doEnd(StringBuilder s,Page page,String pref){
- if (page.getOffset() < page.getSize() - page.getLength()) {
- s.append("<a href=‘" + this.url + pref + "current=" + page.getNextOffset() + "‘>[>]</a>\n" + "<a href=‘" +
- this.url + pref + "current=" + page.lastPageOffset() +
- "‘>[尾页]</a>\n");
- }else{
- s.append("\n"+ "<a href=‘" + this.url + pref + "current=" + page.lastPageOffset() + "‘>[尾页]</a>\n");
- }
- }
- /**
- * 数字连接
- * @param s
- * @param page
- * @param pref
- */
- public void doBody(StringBuilder s,Page page,String pref){
- //显示半径
- int radius = Constants.MAX_PAGE_INDEX / 2 * page.getLength();
- //显示数字的连接从第几条开始
- int startOffset;
- if ((page.getOffset() < radius) || (page.pageCount() <= Constants.MAX_PAGE_INDEX)){
- startOffset = 0;
- }else{
- if (page.getOffset() < page.getSize() - radius){
- startOffset = page.getOffset() - radius;
- }else{
- startOffset = (page.getSize() / page.getLength() - Constants.MAX_PAGE_INDEX) *
- page.getLength();
- }
- }
- for (int i = startOffset; (i < page.getSize()) && (i < startOffset + Constants.MAX_PAGE_INDEX * page.getLength()); ){
- if (i == page.getOffset()){
- s.append("<b>" + (i / page.getLength() + 1) + "</b>\n");
- }else{
- s.append("<a href=‘" + this.url + pref + "current=" + i +
- "‘>" + (i / page.getLength() + 1) + "</a>\n");
- }
- i += page.getLength();
- }
- }
- }
静态参数类:
- public class Constants {
- public static int PAGE_SIZE = 5; //每页大小
- public static int MAX_PAGE_INDEX = 5;//最多显示5个数字连接
- }
编写tld文件mypage.tld:
- <?xml version="1.0" encoding="utf-8" ?>
- <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
- <taglib>
- <tlib-version>2.5</tlib-version>
- <jsp-version>1.2</jsp-version>
- <short-name>mypage</short-name>
- <uri>\tag-page</uri>
- <tag>
- <name>page</name>
- <tag-class>com.byyang.tag.PageTag</tag-class>
- <body-content>pg</body-content>
- <attribute>
- <name>url</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
配置web.xml
- <!-- 自定义标签 -->
- <jsp-config>
- <taglib>
- <taglib-uri>/tag-page</taglib-uri>
- <taglib-location>/WEB-INF/mypage.tld</taglib-location>
- </taglib>
- </jsp-config>
在jsp页面中添加如下代码:
- <%@ taglib uri="/tag-page" prefix="pg"%>
你可以放到一个公共jsp里面一块引。
现在咱们可以测试一下了:
在jsp页面代码:
- <div align="center">
- <h2>图书列表</h2>
- <table border=1>
- <s:iterator var="book" status="index" value="books">
- <s:if test="#index.odd == true">
- <tr style="background-color: yellow">
- </s:if>
- <s:else>
- <tr>
- </s:else>
- <td align="right">图书名称:</td>
- <td align="left"><s:property value="book"/></td>
- </tr>
- </s:iterator>
- </table>
- <pg:page url="getBookList.action"></pg:page>
- </div>
Action中咱们设一个测试数据:
- public class BookAction extends BaseAction {
- private String[] books;
- private BookService bookService = new BookService();
- public String execute() throws Exception {
- setBooks(bookService.getBookList());
- Page page = new Page(1,65,5);
- request.setAttribute("pager", page);
- return SUCCESS;
- }
- public String[] getBooks() {
- return books;
- }
- public void setBooks(String[] books) {
- this.books = books;
- }
- }
好的,接下来启动tomcat,查看页面效果: