在前面的我们已经写好了一个自定义的分页标签,在jsp中引用该标签:
<%@taglib uri="http://hi.csdn.net/tjcyjd/tags" prefix="myTag" %>
<myTag:pager pageSize="${pm.pageSize}" pageNo="${pm.pageNo}" url="index.action" recordCount="${pm.recordCount}"/>
pm其实是一个分页模型,里边包括了:当前页号(pageNo)、每页要显示的记录数(pageSize)、总记录数(recordCount)、符合分页条件的数据列表(data)。
这个分页模型的封转如下:
PageModel.java
/** * ClassName: PageModel.java * Created on 2011 * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved. * site: http://hi.csdn.net/tjcyjd * email: [email protected] */ package com.yjd.domain.common; import java.util.List; /** * 分页数据的模型类 ,此类的实例不需要持久化 * * @author yjd */ public class PageModel<T> { /** 当前页号 */ private int pageNo; /** 每页要显示的记录数 */ private int pageSize; /** 总记录数 */ private int recordCount; /** 符合分页条件的数据列表 */ private List<T> data; public int getPageNo() { int pageCount = getPageCount(); if (pageNo < 1) { pageNo = 1; } if (pageNo > pageCount) { pageNo = pageCount; } return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } 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 List<T> getData() { return data; } public void setData(List<T> data) { this.data = data; } /** 计算出总页数 */ public int getPageCount() { if (recordCount <= 0) { return 0; } else { return (this.recordCount + this.pageSize - 1) / this.pageSize; } } }
MessageDao.java
/** * ClassName: MessageDao.java * Created on 2011 * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved. * site: http://hi.csdn.net/tjcyjd * email: [email protected] */ package com.yjd.nb.dao; import com.yjd.domain.common.PageModel; import com.yjd.nb.domain.Message; /** * 留言类的Dao接口 * * @author yjd */ public interface MessageDao { /** * 获取留言的分页列表 * * @param pageNo * 当前页号 * @param pageSize * 每页要显示的记录数 * @return 符合条件的留言实例分页模型,如果没有符合的数据,返回模型实例中的recordCount为0 */ public PageModel<Message> findByPage(int pageNo, int pageSize); }还是先把这个接口的实现类给它实现了吧:
MessageDaoImpl.java
/** * ClassName: MessageDaoImpl.java * Created on 2011 * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved. * site: http://hi.csdn.net/tjcyjd * email: [email protected] */ package com.yjd.nb.dao.impl; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.Transaction; import com.yjd.domain.common.PageModel; import com.yjd.nb.common.HibernateUtils; import com.yjd.nb.dao.MessageDao; import com.yjd.nb.domain.Message; /** * 留言Dao接口的实现类 * * @author yjd */ public class MessageDaoImpl implements MessageDao { public PageModel<Message> findByPage(int pageNo, int pageSize) { PageModel<Message> pm = new PageModel<Message>(); pm.setPageNo(pageNo); pm.setPageSize(pageSize); int recordCount = this.countAll(); if (recordCount > 0) { pm.setRecordCount(recordCount); List<Message> list = new ArrayList<Message>(); Session session = null; Transaction tx = null; try { session = HibernateUtils.getSession(); tx = session.beginTransaction(); list = session.createQuery("from Message order by id desc") .setFirstResult((pageNo - 1) * pageSize).setMaxResults( pageSize).list(); tx.commit(); pm.setData(list); } catch (Exception e) { if (tx != null) { tx.rollback(); } e.printStackTrace(); } finally { if (session != null) { session.close(); } } } return pm; } }
MessageService.java
/** * ClassName: MessageService.java * Created on 2011 * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved. * site: http://hi.csdn.net/tjcyjd * email: [email protected] */ package com.yjd.nb.service; import com.yjd.domain.common.PageModel; import com.yjd.nb.dao.MessageDao; import com.yjd.nb.dao.impl.MessageDaoImpl; import com.yjd.nb.domain.Message; /** * 留言相关操作的业务类 * * @author yjd */ public class MessageService { private MessageDao dao = new MessageDaoImpl(); public PageModel<Message> findByPage(int pageNo, int pageSize) { return dao.findByPage(pageNo, pageSize); } }该做的我们都做了,最后在action中调用service层的方法即可返回一个分页模型的列表,jsp通过EL表达式可获取到相应的数据:
IndexAction.java
/** * ClassName: IndexAction.java * Created on 2011 * Copyrights 2011 hi.csdn.net/tjcyjd All rights reserved. * site: http://hi.csdn.net/tjcyjd * email: [email protected] */ package com.yjd.nb.web.action; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.yjd.domain.common.PageModel; import com.yjd.nb.domain.Message; import com.yjd.nb.service.MessageService; /** * 首页的请求处理类 * * @author yjd */ public class IndexAction extends ActionSupport { private static final long serialVersionUID = -3634083788212536690L; private MessageService messageService = new MessageService(); private int pageNo = 1; private int pageSize = 10; public String execute() { PageModel<Message> pm = messageService.findByPage(pageNo, pageSize); //ServletActionContext.getRequest().setAttribute("pm", pm); ActionContext.getContext().put("pm", pm); return SUCCESS; } public MessageService getMessageService() { return messageService; } public void setMessageService(MessageService messageService) { this.messageService = messageService; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } }main.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%@taglib uri="http://hi.csdn.net/tjcyjd/tags" prefix="myTag" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><title>在线留言系统</title> <meta http-equiv=content-type content="text/html; charset=UTF-8"> <link href="images/style.css" type=text/css rel=stylesheet></link> <style type="text/css"> body{font-size: 12px} table{font-size: 12px} a{font-size:12px} .current{font-size:12px;} </style> </head> <body><!--留言表单 --> <form name=form1 action="msg/add.action" method=post> <table class=tab cellspacing=1 align=center border=0> <tbody> <tr> <td class=title background=images/titlebg.jpg colspan=2 height=25><span>请 签 写 留 言</span> </td></tr> <tr> <td align=right width="20%">您的称呼: </td> <td width="80%"><input id="username" maxlength=50 name="msg.nickname"/> </td></tr> <tr> <td align=right width="20%">您的性别: </td> <td width="80%"><input id=sex type=radio value="true" name="msg.gender"> 男 <input type=radio checked value="false" name="msg.gender"> 女 </td></tr> <tr> <td align=right>选择头像: </td> <td><select id=image name="msg.header_img"> <option value=1.gif selected>1.gif</option> <option value=2.gif>2.gif</option> <option value=3.gif>3.gif</option> <option value=4.gif>4.gif</option> <option value=5.gif>5.gif</option> <option value=6.gif>6.gif</option> <option value=7.gif>7.gif</option> <option value=8.gif>8.gif</option> <option value=9.gif>9.gif</option> <option value=10.gif>10.gif</option> <option value=11.gif>11.gif</option> <option value=12.gif>12.gif</option> <option value=13.gif>13.gif</option> <option value=14.gif>14.gif</option> <option value=15.gif>15.gif</option> <option value=16.gif>16.gif</option> <option value=17.gif>17.gif</option> <option value=18.gif>18.gif</option> <option value=19.gif>19.gif</option> <option value=20.gif>20.gif</option></select> </td></tr> <tr> <td align=right>您的qq: </td> <td><input id=qq maxlength=50 name="msg.qq"> </td></tr> <tr> <td align=right>您的邮箱: </td> <td><input id=email maxlength=50 name="msg.email"> </td></tr> <tr> <td align=right>留言内容: </td> <td><textarea id=content name="msg.content" rows=5 cols=50></textarea> </td></tr> <tr> <td align="center" colspan=2><input type=submit value=提交> <input type=reset value=重置> </td></tr></tbody></table></form><!--留言表单结束 --><!--留言列表 --> <table class=tab cellspacing=1 align=center border=0> <tbody> <tr> <td class=title background=images/titlebg.jpg colspan=2 height=25><span>留 言 列 表【<a href="login.jsp">管理登录</a>】 </span></td></tr> <c:forEach items="${pm.data}" var="msg"> <tr> <td align="center" width="20%" rowspan=4>你好 : ${msg.nickname} ${msg.gender ? "帅哥" : "靓妹"} <br><br><img src="images/${msg.header_img}"> </td></tr> <tr> <td width="80%" height="100%">发表于: <fmt:formatDate value="${msg.pubTime}" pattern="yyyy-MM-dd HH:mm:ss"/> <img src="images/8_online.gif" border=0> <a href="mailto:${msg.email}"><img src="images/email.gif" border=0></a> <img src="images/ip.gif" border=0> 来自:${msg.ip}</td></tr> <tr> <td>${msg.content}</td></tr> <tr> <td><font color=#ff0000>管理员回复:</font> </td></tr> </c:forEach> </tbody></table><!--留言列表显示结束--> <!--分页链接 --> <myTag:pager pageSize="${pm.pageSize}" pageNo="${pm.pageNo}" url="index.action" recordCount="${pm.recordCount}"/> <!--分页链接结束 --> </body></html>