tld标签分页处理

my-tag.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>1.0</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>mytag</short-name>
 <uri>/mytag</uri>
 <!-- 继承struts2标签ComponentTagSupport -->
    <tag>
     <name>page</name>
        <tag-class>com.itmg.tools.mytags.PageTag</tag-class>
  <attribute>
      <name>action</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
      <name>index</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
      <name>maxIndex</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
      <name>size</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
   <name>groupSize</name>
   <required>false</required>
   <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
      <name>styleClass</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
  <attribute>
      <name>theme</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
  </attribute>
    </tag>
</taglib>


PageBean.java
package com.itmg.tools.page;

/**
 * 分页信息的数据模型
 *@author Jason
 */
public class PageBean implements java.io.Serializable{
	private static final long serialVersionUID = 1L;
	
	private int offset;//偏离量

	private int length;
	private int begin;
	private int end;
	private int total;
	private int curPage;
	private int totalPage;

    private int startPage;
    private int endPage;

    private int gotoPage;//要到哪一页去

    public PageBean() {
    }

    public int getStartPage() {
        return startPage;
    }

    public void setStartPage(int startPage) {
        this.startPage = startPage;
    }

    public int getEndPage() {
        return endPage;
    }

    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }


    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public int getBegin() {
        return begin;
    }

    public void setBegin(int begin) {
        this.begin = begin;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getCurPage() {
        return curPage;
    }

    public void setCurPage(int curPage) {
        this.curPage = curPage;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

	public int getGotoPage() {
		return gotoPage;
	}

	public void setGotoPage(int gotoPage) {
		this.gotoPage = gotoPage;
	}
}



PageHandler.java
package com.itmg.tools.page;

import javax.servlet.http.HttpServletRequest;
import com.itmg.util.Constants;

/**
 * 分页处理类,处理分页的逻辑
 * 
 * @author Jason
 */
public class PageHandler {

	/**
	 * 得到初始化的分页信息
	 * 
	 * @param request
	 * @return
	 */
	public static PageBean initPage(HttpServletRequest request) {
		int length = Constants.PART_PAGE_PER_COUNT;
		int offset = 0;
		int gotoPageIndex = 1;
		int total = 0;
		PageBean page = new PageBean();
		page.setOffset(offset);
		page.setLength(length);
		page.setGotoPage(gotoPageIndex);
		page.setTotal(total);
		return page;
	}

	/**
	 * 得到初始化的分页信息
	 * 
	 * @return
	 */
	public static PageBean initPage() {
		int length = Constants.PART_PAGE_PER_COUNT;
		int offset = 0;
		int gotoPageIndex = 1;
		int total = 0;
		PageBean page = new PageBean();
		page.setOffset(offset);
		page.setLength(length);
		page.setGotoPage(gotoPageIndex);
		page.setTotal(total);
		return page;
	}

	/**
	 * 根据传入的PageBean的total,offset,已经请求的动作,等信息 处理出完成此请求动作,正确的分页信息 请求的动作包括

	 * 
	 * 上一页,下一页,具体某页等

	 * 
	 * 
	 * 分页信息包括 数据总数,从那条数据开始取数,取多少条等

	 * 
	 * 
	 * @param page
	 * @throws Exception
	 */
	public static PageBean handle(PageBean page) throws Exception {
		int offset, curPage, begin, end, total, totalPage, gotoPage, length, startPage, endPage;
		offset = page.getOffset();
		curPage = page.getCurPage();
		begin = page.getBegin();
		end = page.getEnd();
		total = page.getTotal();
		totalPage = page.getTotalPage();
		gotoPage = page.getGotoPage();
		length = page.getLength();
		startPage = page.getStartPage();
		endPage = page.getEnd();

		if (gotoPage != 1) {
			offset = length * (gotoPage - 1);
		}
		begin = offset + 1;
		if (offset + length > total)
			end = total;
		else
			end = offset + length;

		curPage = offset / length + 1;
		if (total % length == 0) {
			totalPage = total / length;
		} else {
			totalPage = total / length + 1;
		}

		startPage = curPage - 5;
		if (startPage < 1)
			startPage = 1;
		endPage = startPage + 9;
		if (endPage > totalPage)
			endPage = totalPage;
		if (end == 0)
			begin = 0;

		page.setOffset(offset);
		page.setLength(length);
		page.setBegin(begin);
		page.setEnd(end);
		page.setCurPage(curPage);
		page.setEndPage(endPage);
		page.setGotoPage(gotoPage);
		page.setLength(length);
		page.setStartPage(startPage);
		page.setTotalPage(totalPage);
		page.setTotal(total);

		return page;
	}

	/**
	 * 从HttpServletRequest 参数中 得到分页请求的值,设置在PageSelect中

	 * 
	 * 
	 * @param request
	 * @return
	 */
	public static PageBean getPageRequest(HttpServletRequest request) {
		int length = Constants.PART_PAGE_PER_COUNT;
		PageBean page = (PageBean) request.getAttribute(Constants.PART_PAGE_KEY);
		if (page == null) {
			page = new PageBean();
			page.setOffset(0);
			page.setLength(length);
			page.setGotoPage(1);
		}
		return page;
	}

	/**
	 * 把分页信息 设置在 request 的Attribute中

	 * 
	 * @param request
	 * @param page
	 *            分页信息
	 * @throws Exception
	 */
	public static void putPage(HttpServletRequest request, PageBean page) throws Exception {
		try {
			request.setAttribute(Constants.PART_PAGE_KEY, page);
		} catch (Exception e) {
			return;
		}
	}
}


PageTag.java 这里构造的链接是静态化链接,按自己的需求而定
package com.itmg.tools.mytags;

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 org.apache.struts2.ServletActionContext;

public class PageTag extends TagSupport {
	private static final long serialVersionUID = -1122449622265941160L;
	// action url
	private String action;
	// 当前页码
	private Integer index;
	// 最大页码数
	private Integer maxIndex;
	// 每页最多显示个数


	private Integer size;
	// 每组显示页码个数
	private Integer groupSize;
	// css
	private String styleClass;
	// 模板
	private String theme;

	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = ServletActionContext.getRequest();	
		JspWriter out = pageContext.getOut();
		String keywords = request.getParameter("keywords");
        String category = request.getParameter("category");
        String minPrice = request.getParameter("minPrice");
        String maxPrice = request.getParameter("maxPrice");
        String sort = request.getParameter("sort");
        String isAuction = request.getParameter("isAuction");
        if(isAuction == null)
        	isAuction = "0";
		try {
			if(category != null && !"".equals(category)){
				if(minPrice != null || maxPrice != null)
					action += keywords+"/category/"+category+"/price/"+minPrice+"-"+maxPrice+"/";
				else
					action += keywords+"/category/"+category+"/";
			}else{
				if(minPrice != null || maxPrice != null)
					action += keywords+"/price/"+minPrice+"-"+maxPrice+"/";
				else
					action += keywords+"/";
			}
			StringBuffer html = new StringBuffer();
			String url = null;
			// 如果只有一页,则无需分页
			if (maxIndex > 1) {
				// 数字样式 << prev 1 2 3 4 5 6 7 8 9 10 next >>
				html.append("<div ");
				if (styleClass != null) {
					html.append("class='" + styleClass + "'>");
				} else {
					html.append(">");
				}
				if (index > 1) {
					// 当前不是第一组,要显示"<< Prev"
					// << Prev返回前一页

					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + "/"+sort+"/"+isAuction;
					else
						url = action;
					
//					html.append("<a href='" + url + "'>先頭ページへ</a>&nbsp;");
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
					    url = action + (index - 1) +"/"+sort+"/"+isAuction;
					else
						if(index>2)
							url = action + (index - 1);
						else
							url = action;
					html.append("<a rel=\"nofollow\" href='" + url + "'>前ページへ</a>&nbsp;");
				}
				// 从第begin页开始显示到end页,每组有groupSize个页数


				
				int begin, end;
				// groupSize不为空,分组显示,否则全部显示
				if (groupSize != null && groupSize < maxIndex) {
					// 每组显示页码个数的一半


					int halfGroupSize = (groupSize - 1) / 2;
					begin = (index - (halfGroupSize + 1)) > 0 ? (index - (halfGroupSize + 1)) : 1;
					end = (index + halfGroupSize) < maxIndex ? (index + halfGroupSize) : maxIndex;
					if (end == maxIndex) {
						if (maxIndex - groupSize < 1) {
							begin = maxIndex;
						}
						begin = maxIndex - groupSize;
					} else if (begin == 1 && end < maxIndex) {
						end = maxIndex > groupSize ? groupSize : maxIndex;
					}
				} else {
					begin = 1;
					end = maxIndex;
				}
				// groupSize个为一组显示


				for (int i = begin; i <= end; i++) {
					if (index == i) { // 当前页要加粗显示
						html.append("<span class=\"current\">" + i + "</span>&nbsp;");
					} else {
						if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
							url = action + i + "/"+sort+"/"+isAuction;
						else
							if(i==1)
								url = action;
							else
								url = action + i;
						
						html.append("<a rel=\"nofollow\" href='" + url + "'>" + i + "</a>&nbsp;");
					}
				}
				if (index < maxIndex) {
					// >>:返回下一组最后一页


					// >:返回下一页

					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + (index + 1) + "/"+sort+"/"+isAuction;
					else
						url = action + (index + 1);
					
					html.append("<a rel=\"nofollow\" href='" + url + "'>次ページへ</a>&nbsp;");
					
					if((isAuction != null && !"0".equals(isAuction)) || (sort != null && !"0".equals(sort)))
						url = action + maxIndex + "/"+sort+"/"+isAuction;
					else
						url = action + maxIndex;
					
//					html.append("<a href='" + url + "'>最終ページへ</a>");
				}
				html.append("</div>");
			}
			out.write(html.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
		return (SKIP_BODY);
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public Integer getIndex() {
		return index;
	}

	public void setIndex(Integer index) {
		this.index = index;
	}

	public Integer getMaxIndex() {
		return maxIndex;
	}

	public void setMaxIndex(Integer maxIndex) {
		this.maxIndex = maxIndex;
	}

	public Integer getSize() {
		return size;
	}

	public void setSize(Integer size) {
		this.size = size;
	}

	public Integer getGroupSize() {
		return groupSize;
	}

	public void setGroupSize(Integer groupSize) {
		this.groupSize = groupSize;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getTheme() {
		return theme;
	}

	public void setTheme(String theme) {
		this.theme = theme;
	}

}

在action中做如下定义:
//分页信息
		PageBean page = new PageBean();
		if (request.getParameter(Constants.PART_PAGE_PAGESEARCH) != null && request.getParameter(Constants.PART_PAGE_PAGESEARCH).equals("true")) {
			page = PageHandler.initPage(request);
		}
		page.setLength(Constants.PART_PAGE_PER_COUNT_10);
		if (index != null) {
			page.setGotoPage(index);
		} else {
			page.setGotoPage(1);
		}


jsp页面:
<div id="page">
				<div class="page_styl">
					<mytag:page index="${resultView.page.curPage}" maxIndex="${resultView.page.totalPage}" action="${pageContext.request.contextPath}/word/" size="${resultView.page.length}" styleClass="page_styl" groupSize="12"></mytag:page>
				</div>
		      </div>


css设置:
#page {
	margin: 40px auto;
	margin-bottom: 25px;
	text-align: center;
}

div .page_styl {
	text-align: center;
	padding-right: 1px;
	padding-left: 1px;
	padding-bottom: 3px;
	margin: 3px;
	padding-top: 3px;
	text-align: center;
	font-size: 14px;
}

div .page_styl a {
	border-right: #fff 1px solid;
	padding-right: 1px;
	border-top: #fff 1px solid;
	padding-left: 1px;
	padding-bottom: 1px;
	margin: 2px;
	border-left: #fff 1px solid;
	color: #0032ab;
	padding-top: 1px;
	border-bottom: #fff 1px solid;
	text-decoration: underline
}

div .page_styl span.current {
	border-right: #fff 1px solid;
	padding-right: 1px;
	border-top: #fff 1px solid;
	padding-left: 1px;
	font-weight: bold;
	padding-bottom: 1px;
	margin: 2px;
	border-left: #fff 1px solid;
	color: #000;
	padding-top: 1px;
	border-bottom: #fff 1px solid;
	/*	background-color: #d5d5d5;*/
}

div .page_styl span.disabled {
	border-right: #eee 1px solid;
	padding-right: 5px;
	border-top: #eee 1px solid;
	padding-left: 5px;
	padding-bottom: 2px;
	margin: 2px;
	border-left: #eee 1px solid;
	color: #ddd;
	padding-top: 2px;
	border-bottom: #eee 1px solid
}

最后在业务逻辑层:
page.setTotal(searchResult_temp.size());
page = PageHandler.handle(page);

你可能感兴趣的:(html,jsp,servlet,css,sun)