分页标签



package com.spring.web.tag;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;

public class PageTag extends TagSupport {

Logger log = Logger.getLogger(PageTag.class);
/**
*
*/
private static final long serialVersionUID = 1L;

private int total;// 总记录数
private int currentPage = 1;// 当前页
private int pageSize = 10;// 每页显示信息数
private int showNum = 20;// 显示链接数
private String css = "";
private String style = "";

private final static String PARAM = "<input type=\"hidden\" id=\"currentPage\" name=\"currentPage\">" +
"<script type=\"text/javascript\">function do_page(p){document.getElementById(\"currentPage\").value=p;document.forms[0].submit();}</script>";

@Override
public int doStartTag() throws JspException {

String html = "";
if (total > 0)
html = html();
else
html = "<div style=\"text-align:right;padding-top:5px;padding-right:5px;\">未找到数据</div>";

try {
this.pageContext.getOut().print(html);
} catch (IOException e) {
log.error(e.getMessage());
e.printStackTrace();
}
return super.doStartTag();
}

private String html() {

String page = this.pageContext.getRequest().getParameter("currentPage");
log.debug("currentPage:"+ page);

if (page != null && page.length() > 0){
this.currentPage= Integer.parseInt(page);
}
int totalPage = this.totalPage();
StringBuffer result = new StringBuffer();
if (css.trim().length() == 0 && this.style.trim().length() == 0){
result.append("<div style=\"text-align:right;padding-top:5px;padding-right:5px;\">");
}else{
result.append("<div class=\"" + css + "\" style=\""+ style +"\">");
}
result.append("<div class=\"" + css + "\" style=\""+ style +"\">");
result.append(PARAM);
result.append("搜索到"+ this.total + "/" + totalPage +"条记录:  ");
if (this.currentPage > 1){
result.append("<a href=\"javascript:do_page(1);\">首页</a>  ");
result.append("<a href=\"javascript:do_page(" + previousPage() + ");\">上页</a>  ");
}
String link = " <a href=\"javascript:do_page({0});\">{1}</a>  ";

List<Integer> list = new ArrayList<Integer>();
if (totalPage <= showNum) {
for (int i = 1; i <= totalPage; i++) {
list.add(i);
}
} else {
Integer left = 0, right = 0;
if(showNum % 2 == 0){
left = showNum / 2 - 1;
right = showNum / 2;
}else{
left = (showNum -1) / 2;
right = (showNum -1) / 2;
}

if (currentPage - left < 1) {
for (int i = 1; i <= showNum; i++) {
list.add(i);
}
} else {
if (currentPage + right >= totalPage) {
for (int i = totalPage - showNum + 1; i <= totalPage; i++) {
list.add(i);
}
} else {
for (int i = currentPage - left; i <= currentPage + right; i++) {
list.add(i);
}
}
}
}

result.append("(");

MessageFormat fmt = new MessageFormat(link);
for (int i = 0; i < list.size(); i++) {
if (list.get(i) == currentPage) {
result.append(" "+ list.get(i) +" ");
} else {
Object [] p = {list.get(i), list.get(i)};
result.append(fmt.format(p));
}
}

result.append(")");
if (this.currentPage < totalPage){
result.append("  <a href=\"javascript:do_page(" + nextPage() + ");\">下页</a>  ");
result.append(" <a href=\"javascript:do_page(" + totalPage + ");\">末页</a> ");
}
result.append("</div>");

return result.toString();
}

private int previousPage() {
if (this.currentPage == 1) {
return 1;
} else {
return this.currentPage - 1;
}
}

private int nextPage() {
if (this.currentPage == totalPage()) {
return totalPage();
} else {
return this.currentPage + 1;
}
}

private int totalPage() {
if (this.total <= this.pageSize){
return 1;
}
if (this.total % this.pageSize == 0) {
return this.total / this.pageSize;
} else {
return this.total / this.pageSize + 1;
}
}

public int getTotal() {
return total;
}

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

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public String getCss() {
return css;
}

public void setCss(String css) {
this.css = css;
}

public int getShowNum() {
return showNum;
}

public void setShowNum(int showNum) {
this.showNum = showNum;
}

public String getStyle() {
return style;
}

public void setStyle(String style) {
this.style = style;
}
}



<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>
JSTL 2.0 core library
</description>
<tlib-version>1.1</tlib-version>
<short-name>page</short-name>
<uri>http://my.cbj.org/taglibs/page</uri>

<tag>
<description>分页标签</description>
<name>page</name>
<tag-class>com.spring.web.tag.PageTag</tag-class>
<body-content>JSP</body-content>

<attribute>
<description>总记录数</description>
<name>total</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>

<attribute>
<description>页大小</description>
<name>pageSize</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>

<attribute>
<description>样式Class</description>
<name>css</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>string</type>
</attribute>

<attribute>
<description>样式Style</description>
<name>style</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
<type>string</type>
</attribute>

<attribute>
<description>显示分页链接数</description>
<name>showNum</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
<type>int</type>
</attribute>
</tag>
</taglib>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值