在前一篇文章中实现了webwork 分页功能封装,优点是使用非常方便。
缺点也比较明显:
1.一次读取全部数据,然后list = list.subList(startIndex,endIndex)取得相应页的数据。 这样导致查看每一页数据的时候服务器端都得从数据库取出全部数据,然后把其中的大部分舍弃,取其中的一页数据传到客户端。这个错误想必大家都想得明白怎么修改。
改进:
DAO中添加相应的有分页功能的接口并实现之:
public List selectFromInbox(User user, int startIndex, int length){
Map<String, Object> map = new HashMap<String, Object>();
map.put("user", user);
map.put("startIndex", startIndex);
map.put("length", length);
return getSqlMapClientTemplate().queryForList("message.inbox", map);
}
简单修改action代码为:
/**inbox and sendbox's message List,differed by type*/
public String getMessagesList() throws Exception{
if (session.get("userId") != null) {
int userId = (Integer) session.get("userId");
User user = new User(userId);
//inboxCount is valued firstly,just because both the sendbox and inbox should show it.
inboxCount = pmDAO.selectCountFromInbox(user);
if(type != null && type.equalsIgnoreCase("sendbox")){//sendBox List
//pmList = pmDAO.selectFromSent(user);
initPage(pmDAO.selectCountFromSend(user));
pmList = pmDAO.selectFromSend(user, (page.getCurrentPage() - 1) * page.getPageSize(),
page.getPageSize());
}else{
/**deal with inBox List*/
initPage(inboxCount, 6);
pmList = pmDAO.selectFromInbox(user,(page.getCurrentPage() - 1) * page.getPageSize(),page.getPageSize());
}
return SUCCESS;
}else{
return FAIL;
}
}
注意以前的dealPageWithList()方法改为initPage(int elementsCount);和另外一种参数形式initPage(int elementsCount,int pageSize);调用第一种方法时,没有定义每页的大小,使用默认值5,使用第二种调用方式时,利用第二个参数把分页大小传进去。详细代码为:
PageActionSupport :
protected void initPage(int elementsCount, int pageSize){
if(page == null)
page = new Page();
page.setElementsCount(elementsCount);
page.setPageSize(pageSize);
int counts = page.getPagesCount();
for(int i = 1;i <= counts;i++){
page.getPages().add(new Integer(i));
}
if(page.getCurrentPage() > counts || page.getCurrentPage() < 1)
page.setCurrentPage(1);
}
protected void initPage(int elementsCount){
initPage(elementsCount, 5);
}
以上对上篇分页代码的重构可以实现每次页面调用只读取相应页的数据,从而减轻了对数据库的压力,更适合于实际应用。
2.在上篇分页日志中提出可以用<ww:property value="page.pageCode"/>直接调出分页样式,也就是类似于:"首页,上一页,1 2 3``` 下一页 末页",这种方式不方便修改。在此,增加第二种方式来写分页样式,以提供更大的灵活性:
/common/page.jsp:
<%@ page language="java" pageEncoding="GB2312"%>
<%@ taglib prefix="ww" uri="/webwork"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<div id="page">
<ww:if test="page.currentPage <= 1">
<font color="#c0c0c0">首页</font>
<font color="#c0c0c0">上一页</font>
</ww:if>
<ww:else>
<a href="javascript:gotoPage(1);">首页</a>
<a href="javascript:gotoPage(<ww:property value="page.currentPage - 1"/>);">上一页</a>
</ww:else>
<ww:iterator value="page.pages" status="loop">
<a href="javascript:gotoPage(<ww:property value="page.pages[#loop.index]"/>);"/>
<ww:property value="page.pages[#loop.index]"/> </a>
</ww:iterator>
<ww:if test="page.currentPage >= page.pagesCount">
<font color="#c0c0c0">下一页</font>
<font color="#c0c0c0">末页</font>
</ww:if>
<ww:else>
<a href="javascript:gotoPage(<ww:property value="page.currentPage + 1"/>);">下一页</a>
<a href="javascript:gotoPage(<ww:property value="page.pagesCount"/>);">末页</a>
</ww:else>
</div>
把以前的<ww:property value="page.pageCode"/>修改为:
<jsp:include page="/common/page.jsp"/>
在page.jsp里可以实现非常灵活的分页样式定义,并且容易修改。这也使得分页功能更加接近实际应用。
希望以上记录对正在阅读的你有帮助。
如需要完整的分页代码,可以留下你的邮箱;或给我发邮件:jen...@126.com.^_^..