--引用一个不错的S2SH分页的实力:
J2EE系统中分页显示,修改dao层,service层,action层,两个分页类文件,代码如下:
dao层接口:
package com.last999.im.news.dao; import java.util.*; import com.last999.im.news.entity.KindEntity; import com.last999.im.news.web.PageTool; public interface KindEntityDao{ public KindEntity get(String uuid); public void save(KindEntity kindEntity); public void update(KindEntity kindEntity); public void delete(String uuid); public void delete(KindEntity kindEntity); public List<KindEntity> findAll(); public KindEntity findKindEntityByName(String name); public List<KindEntity> getKindEntity(PageTool pageInfo); public int getRows(); }
实现类:
package com.last999.im.news.dao.impl; import java.util.*; import com.last999.im.news.dao.KindEntityDao; import com.last999.im.news.entity.KindEntity; import com.last999.im.news.web.PageTool; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.HibernateException; import org.springframework.orm.hibernate3.HibernateCallback; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class KindEntityDaoHibernate extends HibernateDaoSupport implements KindEntityDao{ public KindEntity get(String uuid){ return (KindEntity)getHibernateTemplate().get(KindEntity.class,uuid); } public void save(KindEntity kindEntity){ getHibernateTemplate().save(kindEntity); } public void update(KindEntity kindEntity){ getHibernateTemplate().saveOrUpdate(kindEntity); } public void delete(KindEntity kindEntity){ getHibernateTemplate().delete(kindEntity); } public void delete(String uuid){ getHibernateTemplate().delete((KindEntity)getHibernateTemplate().get(KindEntity.class,uuid)); } public List findAll(){ return getHibernateTemplate().find("from KindEntity"); } public KindEntity findKindEntityByName(String name){ List k1 = getHibernateTemplate().find("from KindEntity as ke where ke.name = ?",name); if (k1 != null && k1.size() == 1){ return (KindEntity)k1.get(0); } return null; } public int getRows(){ return getHibernateTemplate().find("from KindEntity").size(); } public List getKindEntity(PageTool pageInfo){ final int size1=pageInfo.getSize(); final int startRow=(pageInfo.getCur()-1)*pageInfo.getSize(); return this.getHibernateTemplate().executeFind(new HibernateCallback(){ public List doInHibernate(Session session) throws HibernateException{ Query query = session.createQuery("from KindEntity"); query.setFirstResult(startRow); query.setMaxResults(size1); return query.list(); } } ); } }
SERVICE层接口:
package com.last999.im.news.service; import java.util.List; import com.last999.im.news.web.PageTool; import com.last999.im.news.entity.KindEntity; public interface KindEntityManage{ public void addKindEntity(KindEntity entity); public List findAllKind(); public int getRows(); public List getKindEntity(PageTool pageInfo); }
实现类:
package com.last999.im.news.service.impl; import java.util.List; import com.last999.im.news.web.PageTool; import com.last999.im.news.entity.KindEntity; import com.last999.im.news.service.KindEntityManage; import com.last999.im.news.dao.KindEntityDao; public class KindEntityManageImpl implements KindEntityManage{ private KindEntityDao kindEntityDao; public KindEntityDao getKindEntityDao(){ return this.kindEntityDao; } public void setKindEntityDao(KindEntityDao kindEntityDao) { this.kindEntityDao=kindEntityDao; } public void addKindEntity(KindEntity kindEntity){ this.getKindEntityDao().save(kindEntity); } public List findAllKind(){ return this.getKindEntityDao().findAll(); } public int getRows(){ return this.getKindEntityDao().getRows(); } public List getKindEntity(PageTool pageInfo){ return this.getKindEntityDao().getKindEntity(pageInfo); } }
action类:
package com.last999.im.news.action; import java.util.List; import com.last999.im.news.web.PageTool; import com.last999.im.news.web.PageMaker; import com.last999.im.news.entity.KindEntity; import com.last999.im.news.service.KindEntityManage; public class KindViewAction extends SafeStruts2{ private List kindList; private KindEntity kindEntity; private KindEntityManage kindEntityManage; private int ppp; private PageMaker pageMaker; private String pager; public String getPager(){ return this.pager; } public void setPager(String pager) { this.pager=pager; } public PageMaker getPageMaker(){ return this.pageMaker; } public void setPageMaker(PageMaker pageMaker) { this.pageMaker=pageMaker; } public int getPpp(){ return this.ppp; } public void setPpp(int ppp) { this.ppp=ppp; } public KindEntityManage getKindEntityManage(){ return this.kindEntityManage; } public void setKindEntityManage(KindEntityManage kindEntityManage) { this.kindEntityManage=kindEntityManage; } public KindEntity getKindEntity(){ return this.kindEntity; } public void setKindEntity(KindEntity kindEntity) { this.kindEntity=kindEntity; } public List getKindList(){ return this.kindList; } public void setKindList(List kindList) { this.kindList=kindList; } public String kindView(){ PageTool pageInfo = new PageTool(); pageInfo.setCur(this.getPpp()); pageInfo.setTotal(this.getKindEntityManage().getRows()); setKindList(this.getKindEntityManage().getKindEntity(pageInfo)); PageMaker pageMaker = new PageMaker(); setPager(pageMaker.makeList(pageInfo,"kindView.action?ppp=")); return SUCCESS; } }
分页辅助工具类:
package com.last999.im.news.web; public class PageTool{ private int cur=1; private int size=6; private int total=1; public int getCur(){ return this.cur; } public void setCur(int cur) { this.cur=cur; } public int getSize(){ return this.size; } public void setSize(int size) { this.size=size; } public int getTotal(){ return this.total; } public void setTotal(int total) { this.total=total; } public PageTool(){} public PageTool(int cur,int size,int total){ this.cur=cur; this.size=size; this.total=total; } public PageTool(int size){ this.size=size; this.cur=1; this.total=1; } }
PageMaker.java
package com.last999.im.news.web; import com.last999.im.news.web.PageTool; public class PageMaker { public PageMaker(){} public String makeList(PageTool pageinfo,String url){ int linkNumber=10; int halfLinkNumber=5; String pageStyle="<style> .ptbl {border:1px solid #CEDBEF;font-size:12px;padding:0;font-family:Arial;width:auto} .ptbl a {text-decoration:none;color:#555555} .ptbl td {padding-top:0px;padding-bottom:0px;padding-left:4px;padding-right:4px} .strong {background:#CEDBEF;font-weight:800;color:#FF7D00} .strong a{color:#FF7D00} .page_input {background:#ffffff;border:1px solid #CEDBEF;border-top:none;border-bottom:none;color:#FF7D00;width:30px;margin:0px } </style>"; String pageTable="<table border='0'cellpadding='0' cellspacing='0' bgcolor='#CEDBEF' class='ptbl'><tr align='center' bgcolor='#FFFBFF'>"; //a string that will be decretion to show StringBuffer pagelist=new StringBuffer(""); //get current page. int curpage=pageinfo.getCur(); int pagenumber=pageinfo.getTotal()/pageinfo.getSize()+1; //make it's pre,next page int prepage=curpage-1; prepage=prepage<=0?1:prepage; int nextpage=curpage+1; nextpage=nextpage>=pagenumber?pagenumber:nextpage; //finger out how many page total pagelist.append(pageStyle); pagelist.append(pageTable); pagelist.append("<td>Record:<font color='red'>"+pageinfo.getTotal()+"</font></td>"); //frist page //pagelist.append("<td><a href='"+url+"1'>|<</a></td>"); //pagelist.append("<td><a href='"+url+prepage+"'><</a></td>"); if (pagenumber<=linkNumber){ for(int i=1;i<=pagenumber;i++){ if(i==curpage) pagelist.append("<td class='strong'>"+i+"</td>"); else pagelist.append("<td><a href='"+url+i+"'>"+i+"</a></td>"); } }else{ int begin=curpage-halfLinkNumber; begin=(begin<=0)?1:begin; begin=((halfLinkNumber+curpage)>pagenumber) ? (pagenumber-linkNumber+1):begin; for(int i=begin;i<=linkNumber+begin-1;i++){ if(i==curpage){ pagelist.append("<td class='strong'>"+i+"</td>"); pagelist.append("\n"); } else{ pagelist.append("<td><a href='<s:url action='"+url+"'><s:param name='ppp' value='"+i+"'/></s:url>'>"+i+"</a></td>"); pagelist.append("\n"); } } } //pagelist.append("<td><a href='"+url+nextpage+"'>></a></td>"); //pagelist.append("<td><a href='"+url+pagenumber+"'>>|</a></td>"); pagelist.append("<td>Page:<font color='red'>"+pagenumber+"</font></td></tr></table>"); return pagelist.toString(); } }
KindEntity.java
package com.last999.im.news.entity; /** * @hibernate.class * table="kind" * dynamic-update="true" */ public class KindEntity extends Persistent{ private String title; private String content; private String parentId; private String description; /** * @hibernate.property */ public String getDescription(){ return this.description; } public void setDescription(String description) { this.description=description; } private String em1; private String em2; private String em3; /** * @hibernate.property */ public String getEm3(){ return this.em3; } public void setEm3(String em3) { this.em3=em3; } /** * @hibernate.property */ public String getEm2(){ return this.em2; } public void setEm2(String em2) { this.em2=em2; } /** * @hibernate.property */ public String getEm1(){ return this.em1; } public void setEm1(String em1) { this.em1=em1; } /** * @hibernate.property */ public String getParentId(){ return this.parentId; } public void setParentId(String parentId) { this.parentId=parentId; } /** * @hibernate.property * @hibernate.column name="content" sql-type="TEXT" */ public String getContent(){ return this.content; } public void setContent(String content) { this.content=content; } /** * @hibernate.property */ public String getTitle(){ return this.title; } public void setTitle(String title) { this.title=title; } }
为什么没有在页面接收的代码:
<%@ page contentType="text/html;charset=utf-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<title>
showkind
</title>
</head>
<html>
<body>
<center>
<table border="1">
<tr>
<td>
<table border="0" width="755" bgcolor="#eeeeee">
<tr>
<font size="3">
<b>
These are all the kinds:
</b>
</font>
</tr>
<tr>
<td width="%25">title</td>
<td width="%25">description</td>
<td width="%25">parentId</td>
<td width="25%">Content</td>
</tr>
<s:iterator value="%{kindList}" id="kind">
<tr>
<td>
<s:property value="#kind.title"/>
</td>
<td>
<s:property value="#kind.description"/>
</td>
<td>
<s:property value="#kind.parentId"/>
</td>
<td>
<s:property value="#kind.content"/>
</td>
<!--
<td>
<a href="<s:url action='kindDelete'><s:param name='uuid' value='#kind.uuid'/></s:url>">Delete</a>
</td>
-->
</s:iterator>
</tr>
</table>
</td>
</tr>
</table>
<s:property value="pager" escape="false"/>
</center>
</body>
</html>