1,jsp页面:
一般的数据绑定,<s:iterator
加入分页数据,
<s:div><s:property value="pageParam"/> <s:property value="pageStr" escape="false"/></s:div>
2,Action:
public String list() {
DaoDemoUtils.buildJndi();
ApplicationContext ac = new ClassPathXmlApplicationContext("/dao/datasource-context-dao.xml");
NewsDao newsDao = (NewsDao) ac.getBean("NewsDao");
PublicMethodDao publicMethodDao = (PublicMethodDao) ac.getBean("PublicMethodDao");
int pageSizeDefault = 2;//从xml中获取,页面可以设置大小,全局数字。
List list2 = newsDao.getAll();
int recordcount = list2.size();//总记录数
int pageIndex = 1;//默认第1页;
HttpServletRequest request=ServletActionContext.getRequest();
if(request.getParameter("pageIndex")!=null)
{
pageIndex = Integer.parseInt(request.getParameter("pageIndex")); //页码参数;
}
String pram =""; //查询参数;| 暂空;
int pageCount = recordcount % pageSizeDefault == 0 ? recordcount / pageSizeDefault : recordcount / pageSizeDefault + 1;
if(pageCount>1)
{
pageParam = "总记录:" + recordcount + ",页码:" + pageIndex + "/" + pageCount + ";";
pageStr = getPageStyleService().pageList(pageCount,pageSizeDefault,pageIndex,pram);//公共分页样式;
}
String modleName = "News";
List<Object> list = publicMethodDao.getPageByPram(modleName, pageSizeDefault, pageIndex);//公共查询方法;
ActionContext.getContext().put("newss", list);
return "success";
}
公共分页样式:
public class PageStyleService {
//公共分页样式方法
public int setViewPageNumber = 5; //设定前后显示的页码数
public int viewPageNumber = 0; //实际显示的页码数
public int startPage = 0; //实际显示的页码的开始数
public int endPage = 0; //实际显示的页码的结尾数
public String pageList(int pageCount,int pageSizeDefault,int pageIndex,String pram) {
StringBuffer stringbuil = new StringBuffer();
if (pageCount == 1) //1页的不显示分页;
{
stringbuil.append("");
}
else //大于1页的显示分页
{
if (pageCount <= setViewPageNumber) //总页数小于等于设定向前后各显示页码的---全部显示,
{
viewPageNumber = pageCount;
startPage = 1;
}
else //不能完全显示页码的情况 ---省略显示;
{
endPage = pageCount - pageIndex; //显示的结束页码数
if (endPage > setViewPageNumber)
{
endPage = setViewPageNumber;
}
if (pageIndex <= setViewPageNumber)
{
startPage = 1;
}
else
{
startPage = pageIndex - setViewPageNumber; //显示的起始页码数
}
viewPageNumber = pageIndex + endPage;
}
if (pageIndex > 1) //上一页的显示控制
{
stringbuil.append("<a href='?pageIndex=" + (pageIndex - 1) + " '><font style='TEXT-DECORATION:underline;'>上一页</font></a>");
}
for (int i = startPage; i <= viewPageNumber; i++) //显示页面数字
{
//当前页面和其它页面的修饰不同;
if (i == pageIndex)
{
stringbuil.append("<font style='TEXT-DECORATION:none;color:Red;font-size:10pt'>" + i + "</font>");
}
else
{
stringbuil.append(" <a href='?pageIndex=" + i + " '><font id='fonts' style=' color:Navy;'>[<font style='TEXT-DECORATION:underline;'>" + i + "</font>]</font></a> ");
}
}
endPage = pageCount - pageIndex;
if (endPage > setViewPageNumber)
{
stringbuil.append("......");
}
if (pageIndex < pageCount) //下一页的显示控制
{
stringbuil.append("<a href='?pageIndex=" + (pageIndex + 1) + "'><font style='TEXT-DECORATION:underline;'>下一页</font></a>");
}
}
return stringbuil.toString();
}
}
公共查询方法:
public class HibernatePublicMethodDao extends HibernateDaoSupport implements PublicMethodDao{
//分页查询方法
@SuppressWarnings("unchecked")
public List<Object> getPageByPram(final String modleName, final int pageSize,final int pageindex) {
return (List<Object>) getHibernateTemplate().execute(new HibernateCallback() {
public List<Object> doInHibernate(Session session) throws HibernateException, SQLException {
StringBuffer hql = new StringBuffer();
hql.append("from "+modleName+"");
Query query = session.createQuery(hql.toString()).
setFirstResult(pageSize*(pageindex-1)).
setMaxResults(pageSize);
return query.list();
}
});
}
}
参考:---特色,分页bean的使用;
http://www.cnblogs.com/shiyangxt/archive/2008/12/06/1349099.html