騎士の圣殿

 

[转]通用分页标签【废弃以前的两个设计不合理的标签】

对于以前我写的两个分页标签,现在存在很多设计不合理和实现的不好的地方。如果有使用的,建议大家废弃掉
如果给大家带来的误导,表示抱歉。

希望这个能给大家一些启发,我信写得这个标签,在两个项目中使用,还是蛮灵活和简单的。

代码及其jar下载:http://www.blogjava.net/Files/qixin000/PagerTag_src.rar
导入其中的两个jar即可,源代码没有common部分,但是已经是所有的tag源码了,仅作参考。

使用方法:

public class TopicPageData extends AbstractPagerData {
    private ITopicDao topicDao;

    private int categoryId = -1;
    private String keyword = null;
    private int auditId = EAuditStatus.All.getValue();

    public TopicPageData() {
        topicDao = ApplicationContextFactory.getContentFacotry().getTopicDao();
    }

    public IPagerData getPagerData(PagerBean pagerBean) {
        init(pagerBean);
        return new IPagerData() {

            public Object execute(PagerBean pagerBean) {
                return topicDao.selectTopicByQuery(keyword, categoryId, EAuditStatus.valueOf(auditId), pagerBean.getFrom(), pagerBean.getPageSize());
            }

            public int getTotalCount(PagerBean pagerBean) {
                return topicDao.selectTopicByQueryCount(keyword, categoryId, EAuditStatus.valueOf(auditId));
            }
        };
    }

    protected void init(PagerBean pagerBean) {
        if (this.getParameterValue(pagerBean, "categoryId") != null)
            categoryId = Integer.parseInt(this.getParameterValue(pagerBean, "categoryId").toString());
        if (this.getParameterValue(pagerBean, "keyword") != null)
            keyword = this.getParameterValue(pagerBean, "keyword").toString();
        if (this.getParameterValue(pagerBean, "auditId") != null)
            auditId = Integer.parseInt(this.getParameterValue(pagerBean, "auditId").toString());

    }
}


继承抽象类AbstractPagerData,实现其中的函数就行了
init初始化从页面得到的参数
getPagerData获得分页后的数据函数,它的返回值是一个接口,要用匿名内部类实现该接口
IPagerData接口有两个函数要实现,一个是getTotalCount,返回数据总数量,execute返回要显示的一页数据。
然后页面就能够得到这些数据了

页面的使用方法:

<%@ taglib prefix="pager" uri="http://www.yushunkeji.cn/taglib" %>
<pager:pagerData id="topicListId" action="com.yushunkeji.cms.admin.topic.TopicPageData" pageSize="10"
                         dataId="topicList"/>

        <form id="topicListForm" action="cms_topicList.action" method="post">
            <table>
                <tr>
                    <td>
                        关键字<input type="text" name="keyword" id="keyword" value="${param.keyword}"/>
                        分类<select name="categoryId" id="categoryId" onchange="topicListForm.submit();">
                        <option value="-1">请选择...</option>
                        <c:forEach var="category" items="${categoryList}">
                            <option value="${category.categoryId}" ${param.categoryId==category.categoryId?'selected':''}>${category.categoryTitle}</option>
                        </c:forEach>
                    </select>
                        审核
                        <select name="auditId" id="auditId" onchange="topicListForm.submit();">
                            <option value="-1">所有</option>
                            <option value="0" ${param.auditId==0?"selected":""}>待审核</option>
                            <option value="1" ${param.auditId==1?"selected":""}>已审核</option>
                        </select>
                        <input type="submit" id="query" value="查询"/>
                        <input type="submit" id="reset" value="重置"
                               onclick="$('keyword').value='';$('categoryId').value=-1"/><br/>
                    </td>
                </tr>
            </table>
            <table class="adminlist" cellspacing="1">
                <thead>
                    <tr>
                        <th width="2%" class="title"><input type="checkbox" onclick="checkAll(this,$('topicListForm'),'selectedId')">
                        </th>
                        <th width="5%" class="title">编号</th>
                        <th class="title">标题</th>
                        <th width="8%" class="title">分类</th>
                        <th width="5%" class="title">作者</th>
                        <th width="4%" class="title">审核</th>
                        <th width="10%" class="title">操作</th>
                    </tr>
                </thead>
                <c:forEach var="topic" items="${topicList}">
                    <tr class="row0" onMouseOver="this.className='row1'" onMouseOut="this.className='row0'">
                        <td><input type="checkbox" id="chkTopic" name="selectedId"
                                   value="${topic.topicId}"/></td>
                        <td>${topic.topicId}</td>
                        <td>
                                <%--<a href="cms_topicTemplate.action?topic.topicId=${topic.topicId}"--%>
                                <%--target=_blank>${topic.title}</a>--%>
                            <c:choose>
                                <c:when test="${topic.auditStatus==0}">
                                    <a href="${base}/cms/admin/cms_topicTemplate.action?topic.topicId=${topic.topicId}"
                                       target=_blank>${topic.title}</a>
                                </c:when>
                                <c:when test="${topic.auditStatus==1}">
                                    <a href="${base}${topic.htmlFullPath}"
                                       target=_blank>${topic.title}</a>
                                </c:when>
                            </c:choose>
                        </td>
                        <td>${topic.category.categoryTitle}</td>
                        <td>${topic.author}</td>
                        <td>${topic.auditStatus==1?'通过':'待审'}</td>
                        <td>
                            <c:if test="${ex:isAllowOperation(user,topic.topicId)}">
                                <a href="#"
                                   onclick="document.popup.show('${base}/cms/admin/cms_referenceTopic.action?topic.topicId=${topic.topicId}', 660, 600, null);">相关</a>
                                <a href="#"
                                   onclick="window.location.href='cms_topicInfo.action?action=edit&topic.topicId=${topic.topicId}'">编辑</a>
                                <a href="#" onclick="deleteTopic(${topic.topicId})">删除</a>
                            </c:if>
                            &nbsp;
                        </td>
                    </tr>
                </c:forEach>
                <tfoot>
                    <td colspan="7">
                        <div class="pagination">
                            <pager:pager id="topicListId"/>
                        </div>
                    </td>
                </tfoot>
            </table>

        </form>

注意其中的的pager标签的使用,有两个一个是pager:pagerData,一个是pager:pager,两个id要一直,dataid是页面取数据的对象id,注意foreach循环topicList部分。

posted on 2007-11-02 15:33 最後の騎士 阅读(201) 评论(0)  编辑  收藏 所属分类: java技术


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

常用链接

留言簿(1)

随笔分类

随笔档案

文章分类

搜索

最新评论

阅读排行榜

评论排行榜