前段时间参与项目,觉得用的分页有些繁琐, 工作之余,自己写了一个
IteratorTag.java(标签类)
/**
*
* collection : 需要迭代的List
* dataNum : 总共有多少页
* nowPage : 当前页
* name : 每次迭代保存对象的名称
* actionUrl : 分页的请求路径
*/
public
class
IteratorTag
extends
TagSupport
{
private static final long serialVersionUID = 1311335738605478571L;
private String collection;
private String dataNum;
private String nowPage;
private int dataNumint;
private int nowPageint;
private int i;
private String name;
private Iterator it;
private JspWriter out;
private String actionUrl;
private String posturl;

@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
return doNext(it);
}

@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
writePagination();
return EVAL_PAGE;
}

@Override
public int doStartTag() throws JspException {
out = pageContext.getOut();
Collection coll = (Collection) loadObject(pageContext,
collection);
if(coll == null) return SKIP_BODY;
dataNumint = (Integer) loadObject(pageContext, dataNum);
nowPageint = (Integer) loadObject(pageContext, nowPage);
it = coll.iterator();
if (it != null) {
writePagination();
return doNext(it);
} else {
return SKIP_BODY;
}
}

protected int doNext(Iterator it) {
if (it.hasNext()) {
pageContext.setAttribute(name, it.next(), PageContext.PAGE_SCOPE);
return EVAL_BODY_AGAIN;
} else {
return SKIP_BODY;
}
}
//打印分页
protected void writePagination(){
try {
out.print("<div class='turnPage'>");
actionUrl = actionUrl + (actionUrl.indexOf("?")!=-1?"&":"?");
if (nowPageint > 1) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ (nowPageint - 1);
out.print("<a href='" + posturl + "'>上一页</a>");
}
if(dataNumint <= 7){
for (int i = 1; i <= dataNumint; i++) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ i;
if (i == nowPageint) {
if(this.dataNumint!=1)
{
out.print("<a href='javascript:void(0);'>");
out.print("<font color='red'>" + i + "</font>");
out.print("</a>");
}
} else {
out.print("<a href='" + posturl + "'>");
out.print(i);
out.print("</a>");
}
}
}else {
if(nowPageint <= 4){
for (int i = 1; i <= 7; i++) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ i;
if (i == nowPageint) {
if(this.dataNumint!=1)
{
out.print("<a href='javascript:void(0);'>");
out.print("<font color='red'>" + i + "</font>");
out.print("</a>");
}
}else
{
out.print("<a href='" + posturl + "'>");
out.print(i);
out.print("</a>");
}
}
out.print("...");
}else if (nowPageint > 4 && dataNumint - nowPageint < 4) {
out.print("...");
for (int i = nowPageint - 3; i <= dataNumint; i++) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ i;
if (i == nowPageint) {
if(this.dataNumint!=1)
{
out.print("<a href='javascript:void(0);'>");
out.print("<font color='red'>" + i + "</font>");
out.print("</a>");
}
}else
{
out.print("<a href='" + posturl + "'>");
out.print(i);
out.print("</a>");
}
}
}else {
out.print("...");
for (int i = nowPageint - 3; i <= nowPageint + 3; i++) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ i;
if (i == nowPageint) {
if(this.dataNumint!=1)
{
out.print("<a href='javascript:void(0);'>");
out.print("<font color='red'>" + i + "</font>");
out.print("</a>");
}
}else
{
out.print("<a href='" + posturl + "'>");
out.print(i);
out.print("</a>");
}
}
out.print("...");
}
}
if (nowPageint < dataNumint) {
posturl = actionUrl + "dataNum=" + dataNumint + "&nowPage="
+ (nowPageint + 1);
out.print("<a href='" + posturl + "'>下一页</a>");
}
out.print("</div>");

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

//在四个范围内查询对象
public static Object loadObject(PageContext context, String name) {
if (null != context.getAttribute(name)) {
return context.getAttribute(name);
}
if (null != context.getRequest().getAttribute(name)) {
return context.getRequest().getAttribute(name);
}
if (null != context.getSession().getAttribute(name)) {
return context.getSession().getAttribute(name);
}
if (null != context.getServletContext().getAttribute(name)) {
return context.getServletContext().getAttribute(name);
}
return null;
}

public void setCollection(String collection) {
this.collection = collection;
}

public void setName(String name) {
this.name = name;
}

public void setDataNum(String dataNum) {
this.dataNum = dataNum;
}

public void setNowPage(String nowPage) {
this.nowPage = nowPage;
}

public void setActionUrl(String actionUrl) {
this.actionUrl = actionUrl;
}

}
一个iterator.tld与web.xml放在同一目录
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "web-jsptaglibrary_1_2.dtd"
>
<
taglib
>
<
tlib-version
>
tlib-version
</
tlib-version
>
<
jsp-version
>
jsp-version
</
jsp-version
>
<
short-name
>
short-name
</
short-name
>
<
tag
>
<
name
>
iterator
</
name
>
<
tag-class
>
com.web.util.IteratorTag
</
tag-class
>
<
attribute
>
<
name
>
collection
</
name
>
<
required
>
true
</
required
>
<
rtexprvalue
>
true
</
rtexprvalue
>
</
attribute
>
<
attribute
>
<
name
>
name
</
name
>
<
required
>
true
</
required
>
</
attribute
>
<
attribute
>
<
name
>
dataNum
</
name
>
<
required
>
true
</
required
>
<
rtexprvalue
>
true
</
rtexprvalue
>
</
attribute
>
<
attribute
>
<
name
>
nowPage
</
name
>
<
required
>
true
</
required
>
<
rtexprvalue
>
true
</
rtexprvalue
>
</
attribute
>
<
attribute
>
<
name
>
actionUrl
</
name
>
<
required
>
true
</
required
>
<
rtexprvalue
>
true
</
rtexprvalue
>
</
attribute
>
</
tag
>
</
taglib
>
最后在jsp中声明就可以使用了
<%
@ taglib prefix
=
"
iterator
"
uri
=
"
/WEB-INF/iterator.tld
"
%>

<
iterator:iterator name
=
"
Photo
"
actionUrl
=
"
EditPhoto_find.action?albumId=${albumId}
"
nowPage
=
"
nowPage
"
dataNum
=
"
dataNum
"
collection
=
"
photoList
"
>
$
{Photo}
</
iterator:iterator
>
效果如下
IteratorTag.java(标签类)











































































































































































































一个iterator.tld与web.xml放在同一目录





































最后在jsp中声明就可以使用了





效果如下
