写一个Iterator标签

本文介绍了一种自定义的JSP标签——IteratorTag,用于实现网页上的分页功能。该标签通过设置必要的属性参数,如集合、当前页数、总页数等,实现了对数据的迭代显示及分页导航。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 前段时间参与项目,觉得用的分页有些繁琐,  工作之余,自己写了一个
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 == nullreturn 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 >


效果如下
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值