分页技巧_实现第一个分页功能(回复列表中的分页)
========================================
假设共25条数据,每页显示10条,则共3页
first max
---------------------------------
第1页 0 10
第2页 10 10
第3页 20 10
first = (pageNum - 1) * pageSize(当前页-1)* 10
max = pageSize每页显示几条 最多获取几条 不够剩余的都给它
TopicAction.java
@Controller @Scope("prototype") public class TopicAction extends BaseAction<Topic> { private Long forumId; //并提供getset方法,方便准备分页信息 private int pageNum = 1; //当前页,默认第一页 private int pageSize = 10; //每页显示多少条记录,每页显示10条 /** 显示单个主题(主帖+回帖列表) */ public String show() { // 准备数据:topic Topic topic = topicService.getById(model.getId()); ActionContext.getContext().put("topic", topic); //准备数据:replyList // List<Reply> replyList = replyService.findByTopic(topic); // ActionContext.getContext().put("replyList", replyList); //准备分页信息 PageBean pageBean =replyService.getPageBeanByTopic(pageNum, pageSize, topic);//分页信息找service查询 ActionContext.getContext().getValueStack().push(pageBean);//放在栈顶 return "show"; } /** 发表新主题页面 */ public String addUI() { //准备数据 Forum forum = forumService.getById(forumId); ActionContext.getContext().put("forum", forum); return "addUI"; } /** 发表新主题 */ public String add() { //封装 // >>表单参数,已经封装了title,content //model.setTitle(title); //model.setContent(content); model.setForum(forumService.getById(forumId)); // >>当前直接获取的信息 model.setAuthor(getCurrentUser());//当前登录用户 model.setIpAddr(ServletActionContext.getRequest().getRemoteAddr());//当前请求中的ip model.setPostTime(new Date());//当前时间 topicService.save(model); return "toShow";//转到新主题的显示页面 } public Long getForumId() { return forumId; } public void setForumId(Long forumId) { this.forumId = forumId; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }
ReplyService.java
public interface ReplyService extends DaoSupport<Reply>{ /** * 查询指定主题中所有的回复列表,排序:按发表时间升序排序 * @param topic * @return */ List<Reply> findByTopic(Topic topic); /** * 查询分页信息 * @param pageNum * @param pageSize * @param topic * @return */ PageBean getPageBeanByTopic(int pageNum, int pageSize, Topic topic); }
ReplyServiceImpl.java
@Service @Transactional @SuppressWarnings("unchecked") public class ReplyServiceImpl extends DaoSupportImpl<Reply> implements ReplyService{ public List<Reply> findByTopic(Topic topic) {//当前主题所有的回复列表 return getSession().createQuery(// "FROM Reply r WHERE r.topic=? ORDER BY r.postTime ASC")// .setParameter(0, topic)// .list(); } @Override public void save(Reply reply) { //1.保存 getSession().save(reply); //2.维护相关的信息 Topic topic = reply.getTopic(); Forum forum = topic.getForum(); forum.setArticleCount(forum.getArticleCount() + 1); //文章数量(主题数+回复数) topic.setReplyCount(topic.getReplyCount() + 1); //回复数量 topic.setLastReply(reply); //最后发表的时间 topic.setLastUpdateTime(reply.getPostTime());//最后更新的时间(主题的发表时间) getSession().update(topic); getSession().update(forum); } public PageBean getPageBeanByTopic(int pageNum, int pageSize, Topic topic) { //查询本页的数据列表 List list = getSession().createQuery( "FROM Reply r WHERE r.topic=? ORDER BY r.postTime ASC")// .setParameter(0, topic)// .setFirstResult((pageNum - 1) * pageSize)// .setMaxResults(pageSize)// .list(); //查询总记录数量 Long count = (Long)getSession().createQuery(// "SELECT COUNT(*) FROM Reply r WHERE r.topic=?")// .setParameter(0, topic)// .uniqueResult(); return new PageBean(pageNum, pageSize, count.intValue(), list); } }
PageBean.java
/** * 分页功能中的一页的信息 * @author yejin * */ public class PageBean { //指定的或是页面参数 private int currentPage; // 当前页 private int pageSize; // 每页显示多少条 //查询数据库 private int recordCount; // 总记录数 private List recordList; // 本页的数据列表 //计算 private int pageCount; // 总页数 private int beginPageIndex; // 页码列表的开始索引(包含) private int endPageIndex; // 页码列表的结束索引(包含) /** * 只接受前四个必要的属性,会自动的计算出其他3个属性的值 * @param currentPage 当前页 * @param pageSize 每页显示多少条 * @param recordCount 总记录数 * @param recordList 本页的数据列表 */ public PageBean(int currentPage, int pageSize, int recordCount, List recordList) { this.currentPage = currentPage; this.pageSize = pageSize; this.recordCount = recordCount; this.recordList = recordList; //只接受前四个必要的属性,会自动的计算出其他3个属性的值 //计算总页码 pageCount = (recordCount + pageSize - 1) / pageSize; //计算beginPageIndex 和 endPageIndex // >> 总页数不多于10页,则全部显示 if(pageCount <= 10) { beginPageIndex = 1; endPageIndex = pageCount; } // >> 总页数多于10页,则显示当前页附近的10个页码 else{ //当前页附近的10个页码(前4个 + 当前页 + 后5个) beginPageIndex = currentPage - 4; endPageIndex = currentPage + 5; //当前面的页码不足4个时,则显示前10个页码 if(beginPageIndex < 1) { beginPageIndex = 1; endPageIndex = 10; } //当后面的页码不足5个时,则显示后10个页码 if(endPageIndex > pageCount) { endPageIndex = pageCount; beginPageIndex = pageCount - 10 + 1; } } } public List getRecordList() { return recordList; } public void setRecordList(List recordList) { this.recordList = recordList; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getRecordCount() { return recordCount; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } public int getBeginPageIndex() { return beginPageIndex; } public void setBeginPageIndex(int beginPageIndex) { this.beginPageIndex = beginPageIndex; } public int getEndPageIndex() { return endPageIndex; } public void setEndPageIndex(int endPageIndex) { this.endPageIndex = endPageIndex; } }
topicAction >>show.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <title>查看主题:${topic.title}</title> <%@ include file="/WEB-INF/jsp/public/commons.jspf" %> <link type="text/css" rel="stylesheet" href="${pageContext.request.contextPath}/style/blue/forum.css" /> <script language="javascript" src="${pageContext.request.contextPath}/script/fckeditor/fckeditor.js" charset="utf-8"></script> <script type="text/javascript"> $(function(){ var fck = new FCKeditor("content"); fck.Width = "90%"; fck.ToolbarSet = "bbs"; fck.BasePath = "${pageContext.request.contextPath}/script/fckeditor/"; fck.ReplaceTextarea(); }); </script> </head> <body> <!-- 标题显示 --> <div id="Title_bar"> <div id="Title_bar_Head"> <div id="Title_Head"></div> <div id="Title"><!--页面标题--> <img border="0" width="13" height="13" src="${pageContext.request.contextPath}/style/images/title_arrow.gif"/> 查看主题 </div> <div id="Title_End"></div> </div> </div> <!--内容显示--> <div id="MainArea"> <div id="PageHead"></div> <center> <div class="ItemBlock_Title1" style=" 98%"> <font class="MenuPoint"> > </font> <s:a action="forum_list">论坛</s:a> <font class="MenuPoint"> > </font> <s:a action="forum_show?id=%{#topic.forum.id}">${topic.forum.name}</s:a> <font class="MenuPoint"> >> </font> 帖子阅读 <span style="margin-left:30px;"> <s:a action="topic_addUI?forumId=%{#topic.forum.id}"> <img align="absmiddle" src="${pageContext.request.contextPath}/style/blue/images/button/publishNewTopic.png"/> </s:a> </span> </div> <div class="ForumPageTableBorder dataContainer" datakey="replyList"> <!--显示主题标题等--> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr valign="bottom"> <td width="3" class="ForumPageTableTitleLeft"> </td> <td class="ForumPageTableTitle"><b>本帖主题:${topic.title}</b></td> <td class="ForumPageTableTitle" align="right" style="padding-right:12px;"> <s:a cssClass="detail" action="reply_addUI?topicId=%{#topic.id}"> <img border="0" src="${pageContext.request.contextPath}/style/images/reply.gif" /> 回复 </s:a> <a href="moveUI.html"><img border="0" src="${pageContext.request.contextPath}/style/images/edit.gif" />移动到其他版块</a> <a href="#" onClick="return confirm('要把本主题设为精华吗?')"><img border="0" src="${pageContext.request.contextPath}/style/images/topicType_1.gif" />精华</a> <a href="#" onClick="return confirm('要把本主题设为置顶吗?')"><img border="0" src="${pageContext.request.contextPath}/style/images/topicType_2.gif" />置顶</a> <a href="#" onClick="return confirm('要把本主题设为普通吗?')"><img border="0" src="${pageContext.request.contextPath}/style/images/topicType_0.gif" />普通</a> </td> <td width="3" class="ForumPageTableTitleRight"> </td> </tr> <tr height="1" class="ForumPageTableTitleLine"><td colspan="4"></td></tr> </table> <!-- ~~~~~~~~~~~~~~~ 显示主帖(主帖只在第1页显示) ~~~~~~~~~~~~~~~ --> <s:if test="currentPage == 1"> <div class="ListArea"> <table border="0" cellpadding="0" cellspacing="1" width="100%"> <tr> <td rowspan="3" width="130" class="PhotoArea" align="center" valign="top"> <!--作者头像--> <div class="AuthorPhoto"> <img border="0" width="110" height="110" src="${pageContext.request.contextPath}/style/images/defaultAvatar.gif" onerror="this.onerror=null; this.src='${pageContext.request.contextPath}/style/images/defaultAvatar.gif';" /> </div> <!--作者名称--> <div class="AuthorName">${topic.author.name}</div> </td> <td align="center"> <ul class="TopicFunc"> <!--操作列表--> <li class="TopicFuncLi"> <a class="detail" href="${pageContext.request.contextPath}/BBS_Topic/saveUI.html"><img border="0" src="${pageContext.request.contextPath}/style/images/edit.gif" />编辑</a> <a class="detail" href="#" onClick="return confirm('确定要删除本帖吗?')"><img border="0" src="${pageContext.request.contextPath}/style/images/delete.gif" />删除</a> </li> <!-- 文章的标题 --> <li class="TopicSubject"> ${topic.title} </li> </ul> </td> </tr> <tr><!-- 文章内容 --> <td valign="top" align="center"> <div class="Content">${topic.content}</div> </td> </tr> <tr><!--显示楼层等信息--> <td class="Footer" height="28" align="center" valign="bottom"> <ul style="margin: 0px; 98%;"> <li style="float: left; line-height:18px;"><font color=#C30000>[楼主]</font> ${topic.postTime} </li> <li style="float: right;"><a href="javascript:scroll(0,0)"> <img border="0" src="${pageContext.request.contextPath}/style/images/top.gif" /></a> </li> </ul> </td> </tr> </table> </div> </s:if> <!-- ~~~~~~~~~~~~~~~ 显示主帖结束 ~~~~~~~~~~~~~~~ --> <!-- ~~~~~~~~~~~~~~~ 显示回复列表 ~~~~~~~~~~~~~~~ --> <s:iterator value="recordList" status="status"> <div class="ListArea template"> <table border="0" cellpadding="0" cellspacing="1" width="100%"> <tr> <td rowspan="3" width="130" class="PhotoArea" align="center" valign="top"> <!--作者头像--> <div class="AuthorPhoto"> <img border="0" width="110" height="110" src="${pageContext.request.contextPath}/style/images/defaultAvatar.gif" onerror="this.onerror=null; this.src='${pageContext.request.contextPath}/style/images/defaultAvatar.gif';" /> </div> <!--作者名称--> <div class="AuthorName">${author.name}</div> </td> <td align="center"> <ul class="TopicFunc"> <!--操作列表--> <li class="TopicFuncLi"> <a class="detail" href="${pageContext.request.contextPath}/BBS_Topic/saveUI.html"><img border="0" src="${pageContext.request.contextPath}/style/images/edit.gif" />编辑</a> <a class="detail" href="#" onClick="return confirm('确定要删除本帖吗?')"><img border="0" src="${pageContext.request.contextPath}/style/images/delete.gif" />删除</a> </li> <!-- 文章表情与标题 --> <li class="TopicSubject"> ${title} </li> </ul> </td> </tr> <tr><!-- 文章内容 --> <td valign="top" align="center"> <div class="Content">${content}</div> </td> </tr> <tr><!--显示楼层等信息--> <td class="Footer" height="28" align="center" valign="bottom"> <ul style="margin: 0px; 98%;"> <li style="float: left; line-height:18px;"><font color=#C30000>[${(currentPage - 1) * pageSize + status.count}楼]</font> ${postTime} </li> <li style="float: right;"><a href="javascript:scroll(0,0)"> <img border="0" src="${pageContext.request.contextPath}/style/images/top.gif" /></a> </li> </ul> </td> </tr> </table> </div> </s:iterator> <!-- ~~~~~~~~~~~~~~~ 显示回复列表结束 ~~~~~~~~~~~~~~~ --> </div> <!--分页信息--> <div id=PageSelectorBar> <div id=PageSelectorMemo> 页次:${currentPage}/${pageCount}页 每页显示:${pageSize}条 总记录数:${recordCount}条 </div> <div id=PageSelectorSelectorArea> <a href="javascript:gotoPage(1)" title="首页" style="cursor: hand;"> <img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/firstPage.png"/> </a> <s:iterator begin="%{beginPageIndex}" end="%{endPageIndex}" var="num"> <s:if test="#num == currentPage"><%--当前页 --%> <span class="PageSelectorNum PageSelectorSelected">${num}</span> </s:if> <s:else><%--非当前页 --%> <span class="PageSelectorNum" style="cursor: hand;" onClick="gotoPage(${num});">${num}</span> </s:else> </s:iterator> <a href="javascript:gotoPage{${pageCount}}" title="尾页" style="cursor: hand;"> <img src="${pageContext.request.contextPath}/style/blue/images/pageSelector/lastPage.png"/> </a> 转到: <select onchange="gotoPage(this.value)" id="_pn"> <s:iterator begin="1" end="%{pageCount}" var="num"> <option value="${num}">${num}</option> </s:iterator> </select> <script type="text/javascript"> $("#_pn").val("${currentPage}"); </script> </div> </div> <script type="text/javascript"> function gotoPage( pageNum ) { window.location.href = "topic_show.action?id=${id}&pageNum=" + pageNum; } </script> <div class="ForumPageTableBorder" style="margin-top: 25px;"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr valign="bottom"> <td width="3" class="ForumPageTableTitleLeft"> </td> <td class="ForumPageTableTitle"><b>快速回复</b></td> <td width="3" class="ForumPageTableTitleRight"> </td> </tr> <tr height="1" class="ForumPageTableTitleLine"> <td colspan="3"></td> </tr> </table> </div> </center> <!--快速回复--> <div class="QuictReply"> <form action=""> <div style="padding-left: 3px;"> <table border="0" cellspacing="1" width="98%" cellpadding="5" class="TableStyle"> <tr height="30" class="Tint"> <td width="50px" class="Deep"><b>标题</b></td> <td class="no_color_bg"> <input type="text" name="title" class="InputStyle" value="回复:昨天发现在表单里删除的图片" style="90%"/> </td> </tr> <tr class="Tint" height="200"> <td valign="top" rowspan="2" class="Deep"><b>内容</b></td> <td valign="top" class="no_color_bg"> <textarea name="content" style=" 95%; height: 300px"></textarea> </td> </tr> <tr height="30" class="Tint"> <td class="no_color_bg" colspan="2" align="center"> <input type="image" src="${pageContext.request.contextPath}/style/blue/images/button/submit.PNG" style="margin-right:15px;"/> </td> </tr> </table> </div> </form> </div> </div> <div class="Description"> 说明:<br /> 1,主帖只在第一页显示。<br /> 2,只有是管理员才可以进行“移动”、“编辑”、“删除”、“精华”、“置顶”的操作。<br /> 3,删除主帖,就会删除所有的跟帖(回复)。<br /> </div> </body> </html>
==============================
FROM 必须
实体名
WHERE 可选
条件1 AND 条件2 AND ... 条件n
ORDER BY 可选
属性1, 属性2, ... 属性n
==============================
QueryHelper
用于辅助拼接HQL语句
addCondition("t.type=?", "精华");
d.id BETWEEN ? AND ?
d.parent=?
d.parent IS NULL