struts2+hibernate+spring分页实现(DAO,Service,Action三层架构搭配)-阿里云开发者社区

开发者社区> 开发与运维> 正文

struts2+hibernate+spring分页实现(DAO,Service,Action三层架构搭配)

简介: Java代码         import java.util.List;       public interface Pagination {     ...
Java代码 复制代码
  1. <PRE class=java name="code">  </PRE>   
  2.  import java.util.List;   
  3.   
  4. public interface Pagination {   
  5.     public boolean isFirst();//判断是否为第一页   
  6.     public boolean isLast();//判断是否为最后一页   
  7.     public boolean hasNext();//判断是否还有下一页   
  8.     public boolean hasPrevious();//判断是否还有上一页   
  9.     public int getMaxElements();//还回分页总记录数   
  10.     public int getMaxPages();//还回总页数   
  11.     public int getNext();//获得下一页的页面   
  12.     public int getPrevious();//获得上一页的页面   
  13.     public int getPageSize();//还回分页大小   
  14.     public int getPageNumber();//还回当前页的页面   
  15.     public List<Object> getList();//还回当前页的记录数据   
  16.     public void setPageSize(int pageSize);//设置分页大小   
  17.     public void setPageNumber(int pageNumber);//设置跳转的页面的页码   
  18. }  
[java] view plaincopy
  1. <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span>    </span></span></li></ol></div><pre class="java" style="display: none;" name="code">  </pre>  
  2.  import java.util.List;  
  3.   
  4. public interface Pagination {  
  5.     public boolean isFirst();//判断是否为第一页  
  6.     public boolean isLast();//判断是否为最后一页  
  7.     public boolean hasNext();//判断是否还有下一页  
  8.     public boolean hasPrevious();//判断是否还有上一页  
  9.     public int getMaxElements();//还回分页总记录数  
  10.     public int getMaxPages();//还回总页数  
  11.     public int getNext();//获得下一页的页面  
  12.     public int getPrevious();//获得上一页的页面  
  13.     public int getPageSize();//还回分页大小  
  14.     public int getPageNumber();//还回当前页的页面  
  15.     public List<Object> getList();//还回当前页的记录数据  
  16.     public void setPageSize(int pageSize);//设置分页大小  
  17.     public void setPageNumber(int pageNumber);//设置跳转的页面的页码  
  18. }  
Java代码 复制代码
  1.   Pagination 接口实现  
[java] view plaincopy
  1.   Pagination 接口实现  
Java代码 复制代码
  1. public class PaginationImpl  implements Pagination {   
  2.     private int pageSize = 20;   
  3.   
  4.     private int pageNumber = 1;   
  5.   
  6.     private int maxPages;   
  7.   
  8.     private int maxElements;   
  9.   
  10.     private List<Object> list;   
  11.   
  12.     public PaginationImpl() {   
  13.     }   
  14.   
  15.   
  16.     public PaginationImpl(List<Object> list, int maxElements, int pageSize, int pageNumber) {   
  17.         this.pageSize = pageSize;   
  18.         this.list=list;   
  19.         this.maxElements=maxElements;   
  20.         init();   
  21.         this.setPageNumber(pageNumber);   
  22.     }   
  23.   
  24.     private void init() {   
  25.         setMaxPages();   
  26.     }   
  27.   
  28.     private void setMaxPages() {   
  29.         if (maxElements != 0 && (maxElements % pageSize == 0)) {   
  30.             maxPages = maxElements / pageSize;   
  31.         } else {   
  32.             maxPages = maxElements / pageSize + 1;   
  33.         }   
  34.     }   
  35.   
  36.     public List<Object> getList() {   
  37.         return this.list;   
  38.     }   
  39.   
  40.     public void setList(List<Object> list) {   
  41.         this.list = list;   
  42.     }   
  43.   
  44.     public int getMaxElements() {   
  45.         return maxElements;   
  46.     }   
  47.   
  48.     public int getMaxPages() {   
  49.         return maxPages;   
  50.     }   
  51.   
  52.     public int getNext() {   
  53.         if (pageNumber + 1 >= this.getMaxPages()) {   
  54.             return getMaxPages();   
  55.         }   
  56.         return pageNumber + 1;   
  57.     }   
  58.   
  59.     public int getPageNumber() {   
  60.         return pageNumber;   
  61.     }   
  62.   
  63.     public int getPageSize() {   
  64.         return pageSize;   
  65.     }   
  66.   
  67.     public int getPrevious() {   
  68.         if (pageNumber - 1 <= 1) {   
  69.             return 1;   
  70.         } else {   
  71.             return pageNumber - 1;   
  72.         }   
  73.     }   
  74.   
  75.     public boolean hasNext() {   
  76.         return pageNumber < this.getMaxPages();   
  77.     }   
  78.   
  79.     public boolean hasPrevious() {   
  80.         return pageNumber > 1;   
  81.     }   
  82.   
  83.     public boolean isFirst() {   
  84.         return pageNumber == 1;   
  85.     }   
  86.   
  87.     public boolean isLast() {   
  88.         return pageNumber >= this.getMaxPages();   
  89.     }   
  90.   
  91.     public void setPageNumber(int pageNumber) {   
  92.         if (pageNumber > maxPages) {   
  93.             this.pageNumber = maxPages;   
  94.         } else if (pageNumber < 1) {   
  95.             this.pageNumber = 1;   
  96.         } else {   
  97.             this.pageNumber = pageNumber;   
  98.         }   
  99.     }   
  100.   
  101.     public void setPageSize(int pageSize) {   
  102.         this.pageSize = pageSize;   
  103.     }   
  104.   
  105.     public void setMaxElements(int maxElements) {   
  106.         this.maxElements = maxElements;   
  107.     }   
  108.   
  109.   
  110. }  
[java] view plaincopy
  1. public class PaginationImpl  implements Pagination {  
  2.     private int pageSize = 20;  
  3.   
  4.     private int pageNumber = 1;  
  5.   
  6.     private int maxPages;  
  7.   
  8.     private int maxElements;  
  9.   
  10.     private List<Object> list;  
  11.   
  12.     public PaginationImpl() {  
  13.     }  
  14.   
  15.   
  16.     public PaginationImpl(List<Object> list, int maxElements, int pageSize, int pageNumber) {  
  17.         this.pageSize = pageSize;  
  18.         this.list=list;  
  19.         this.maxElements=maxElements;  
  20.         init();  
  21.         this.setPageNumber(pageNumber);  
  22.     }  
  23.   
  24.     private void init() {  
  25.         setMaxPages();  
  26.     }  
  27.   
  28.     private void setMaxPages() {  
  29.         if (maxElements != 0 && (maxElements % pageSize == 0)) {  
  30.             maxPages = maxElements / pageSize;  
  31.         } else {  
  32.             maxPages = maxElements / pageSize + 1;  
  33.         }  
  34.     }  
  35.   
  36.     public List<Object> getList() {  
  37.         return this.list;  
  38.     }  
  39.   
  40.     public void setList(List<Object> list) {  
  41.         this.list = list;  
  42.     }  
  43.   
  44.     public int getMaxElements() {  
  45.         return maxElements;  
  46.     }  
  47.   
  48.     public int getMaxPages() {  
  49.         return maxPages;  
  50.     }  
  51.   
  52.     public int getNext() {  
  53.         if (pageNumber + 1 >= this.getMaxPages()) {  
  54.             return getMaxPages();  
  55.         }  
  56.         return pageNumber + 1;  
  57.     }  
  58.   
  59.     public int getPageNumber() {  
  60.         return pageNumber;  
  61.     }  
  62.   
  63.     public int getPageSize() {  
  64.         return pageSize;  
  65.     }  
  66.   
  67.     public int getPrevious() {  
  68.         if (pageNumber - 1 <= 1) {  
  69.             return 1;  
  70.         } else {  
  71.             return pageNumber - 1;  
  72.         }  
  73.     }  
  74.   
  75.     public boolean hasNext() {  
  76.         return pageNumber < this.getMaxPages();  
  77.     }  
  78.   
  79.     public boolean hasPrevious() {  
  80.         return pageNumber > 1;  
  81.     }  
  82.   
  83.     public boolean isFirst() {  
  84.         return pageNumber == 1;  
  85.     }  
  86.   
  87.     public boolean isLast() {  
  88.         return pageNumber >= this.getMaxPages();  
  89.     }  
  90.   
  91.     public void setPageNumber(int pageNumber) {  
  92.         if (pageNumber > maxPages) {  
  93.             this.pageNumber = maxPages;  
  94.         } else if (pageNumber < 1) {  
  95.             this.pageNumber = 1;  
  96.         } else {  
  97.             this.pageNumber = pageNumber;  
  98.         }  
  99.     }  
  100.   
  101.     public void setPageSize(int pageSize) {  
  102.         this.pageSize = pageSize;  
  103.     }  
  104.   
  105.     public void setMaxElements(int maxElements) {  
  106.         this.maxElements = maxElements;  
  107.     }  
  108.   
  109.   
  110. }  
Java代码 复制代码
  1.    
[java] view plaincopy
  1.    
Java代码 复制代码
  1. public interface IPaginationDao {   
  2.     public List<Object> listByPage(String hql,int pageSize, int pageNumber);   
  3.     public int listCount(String hql);   
  4. }  
[java] view plaincopy
  1. public interface IPaginationDao {  
  2.     public List<Object> listByPage(String hql,int pageSize, int pageNumber);  
  3.     public int listCount(String hql);  
  4. }  
Java代码 复制代码
  1.       
[java] view plaincopy
  1.       
Java代码 复制代码
  1.  IPaginationDao 接口实现  
[java] view plaincopy
  1.  IPaginationDao 接口实现  
Java代码 复制代码
  1. public class PaginationDaoImpl extends HibernateDaoSupport implements IPaginationDao{   
  2.   
  3. public List<Object> listByPage(final String hql, final int pageSize, final int pageNumber) {   
  4.            
  5.         return  (List<Object>)this.getHibernateTemplate().executeFind( new  HibernateCallback(){   
  6.             public  Object doInHibernate(Session session)  throws  SQLException,HibernateException {   
  7.               Query q  =  session.createQuery(hql);   
  8.               q.setFirstResult((pageNumber - 1) * pageSize);   
  9.               q.setMaxResults(pageSize);                  
  10.               List<Object> list = (List<Object>) q.list();     
  11.               return  list;     
  12.               }   
  13.        });   
  14.     }   
  15.   
  16.     public int listCount(String hql) {   
  17.         // TODO Auto-generated method stub   
  18.         return this.getHibernateTemplate().find(hql).size();   
  19.     }   
  20.   
  21. }  
[java] view plaincopy
  1. public class PaginationDaoImpl extends HibernateDaoSupport implements IPaginationDao{  
  2.   
  3. public List<Object> listByPage(final String hql, final int pageSize, final int pageNumber) {  
  4.           
  5.         return  (List<Object>)this.getHibernateTemplate().executeFind( new  HibernateCallback(){  
  6.             public  Object doInHibernate(Session session)  throws  SQLException,HibernateException {  
  7.               Query q  =  session.createQuery(hql);  
  8.               q.setFirstResult((pageNumber - 1) * pageSize);  
  9.               q.setMaxResults(pageSize);                 
  10.               List<Object> list = (List<Object>) q.list();    
  11.               return  list;    
  12.               }  
  13.        });  
  14.     }  
  15.   
  16.     public int listCount(String hql) {  
  17.         // TODO Auto-generated method stub  
  18.         return this.getHibernateTemplate().find(hql).size();  
  19.     }  
  20.   
  21. }  
Java代码 复制代码
  1.  服务接口  
[java] view plaincopy
  1.  服务接口  
Java代码 复制代码
  1. <PRE class=java name="code">public interface IPaginationServ { public Pagination getProductByPage(String hql,int pageSize, int pageNumber);}</PRE>   
  2.    
[java] view plaincopy
  1. <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">interface</span></strong></span><span> IPaginationServ { </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> Pagination getProductByPage(String hql,</span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageSize, </span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageNumber);}  </span></span></li></ol></div><pre class="java" style="display: none;" name="code">public interface IPaginationServ { public Pagination getProductByPage(String hql,int pageSize, int pageNumber);}</pre>  
  2.    
Java代码 复制代码
  1. IPaginationServ  接口实现  
[java] view plaincopy
  1. IPaginationServ  接口实现  
Java代码 复制代码
  1. public class PaginationServImpl implements IPaginationServ{   
  2.      private IPaginationDao pagingDao;   
  3.     public Pagination getProductByPage(String hql, int pageSize, int pageNumber) {   
  4.              int total =pagingDao.listCount(hql);   
  5.              List<Object> list=pagingDao.listByPage(hql, pageSize, pageNumber);   
  6.              Pagination paging=new PaginationImpl(list,total,pageSize,pageNumber);   
  7.              return paging;   
  8.     }   
  9.     public IPaginationDao getPagingDao() {   
  10.         return pagingDao;   
  11.     }   
  12.     public void setPagingDao(IPaginationDao pagingDao) {   
  13.         this.pagingDao = pagingDao;   
  14.     }   
  15.   
  16. }  
[java] view plaincopy
  1. public class PaginationServImpl implements IPaginationServ{  
  2.      private IPaginationDao pagingDao;  
  3.     public Pagination getProductByPage(String hql, int pageSize, int pageNumber) {  
  4.              int total =pagingDao.listCount(hql);  
  5.              List<Object> list=pagingDao.listByPage(hql, pageSize, pageNumber);  
  6.              Pagination paging=new PaginationImpl(list,total,pageSize,pageNumber);  
  7.              return paging;  
  8.     }  
  9.     public IPaginationDao getPagingDao() {  
  10.         return pagingDao;  
  11.     }  
  12.     public void setPagingDao(IPaginationDao pagingDao) {  
  13.         this.pagingDao = pagingDao;  
  14.     }  
  15.   
  16. }  
Java代码 复制代码
  1.  配置spring文件  
[java] view plaincopy
  1.  配置spring文件  
Java代码 复制代码
  1. <bean id="pagingDao"  
  2.         class="××××.dao.impl.PaginationDaoImpl">   
  3.         <property name="sessionFactory" >   
  4.             <ref bean="sessionFactory"/>   
  5.         </property>   
  6.     </bean>  
[java] view plaincopy
  1. <bean id="pagingDao"  
  2.         class="××××.dao.impl.PaginationDaoImpl">  
  3.         <property name="sessionFactory" >  
  4.             <ref bean="sessionFactory"/>  
  5.         </property>  
  6.     </bean>  
Java代码 复制代码
  1. <bean id="pagingServ"  
  2.   class="com.cnkf.wushi.service.impl.PaginationServImpl">   
  3.   <property name="pagingDao" >   
  4.       <ref bean="pagingDao"/>   
  5.   </property>   
  6.  </bean>  
[java] view plaincopy
  1. <bean id="pagingServ"  
  2.   class="com.cnkf.wushi.service.impl.PaginationServImpl">  
  3.   <property name="pagingDao" >  
  4.       <ref bean="pagingDao"/>  
  5.   </property>  
  6.  </bean>  
Java代码 复制代码
  1.  添加页面  
[java] view plaincopy
  1.  添加页面  
Java代码 复制代码
  1. <PRE class=JSP name="code"> </PRE>   
  2. <PRE class=JSP name="code"><<A href="mailto:%@taglib">%@taglib</A> uri="<A href="http://jsptags.com/tags/navigation/pager">http://jsptags.com/tags/navigation/pager</A>" prefix="pg"%></PRE>   
  3. <PRE class=JSP name="code"><a href="findAllDisplay.action"><h1>displayTag分页</h1></a>   
  4.             <s:iterator value="page.list" id="pr">   
  5.                 <table width="300" border="1" bordercolor="000000"  
  6.                     style="table-layout: fixed; word-break: break-all">   
  7.                     <tr>   
  8.                         <td width="100" bordercolor="ffffff">   
  9.                             编号:   
  10.                         </td>   
  11.                         <td width="500" bordercolor="ffffff">   
  12.                             <s:property value="id" />   
  13.                         </td>   
  14.                     </tr>   
  15.                     <tr>   
  16.                         <td bordercolor="ffffff">   
  17.                             名称:   
  18.                         </td>   
  19.                         <td bordercolor="ffffff">   
  20.                             <s:property value="name" />   
  21.                         </td>   
  22.                     </tr>   
  23.                     <tr>   
  24.                         <td bordercolor="ffffff">   
  25.                             价格:   
  26.                         </td>   
  27.                         <td bordercolor="ffffff">   
  28.                             <s:property value="price" />   
  29.                         </td>   
  30.                     </tr>   
  31.                     <tr>   
  32.                         <td bordercolor="ffffff">   
  33.                             类型:   
  34.                         </td>   
  35.                         <td bordercolor="ffffff">   
  36.                             <s:property value="type" />   
  37.                         </td>   
  38.                     </tr>   
  39.                 </table>   
  40.                 <br>   
  41.             </s:iterator>   
  42.             <pg:pager url="findAllPg.action"  
  43.                 items="${page.maxElements}"  
  44.                 maxPageItems="${pageSize}" maxIndexPages="5">   
  45.                 <pg:index>   
  46.                     <pg:first unless="current">   
  47.                         <a href="${pageUrl}&pageNumber=${pageNumber}">首页</a>   
  48.                     </pg:first>   
  49.                     <pg:prev>   
  50.                         <a href="${pageUrl}&pageNumber=${pageNumber}">上一页(${pageNumber})</a>   
  51.                     </pg:prev>   
  52.                     <pg:pages>   
  53.                         <a href="${pageUrl}&pageNumber=${pageNumber}">${pageNumber}</a>   
  54.                     </pg:pages>   
  55.                     <pg:next>   
  56.                         <a href="${pageUrl}&pageNumber=${pageNumber}">下一页(${pageNumber})</a>   
  57.                     </pg:next>   
  58.                     <pg:last unless="current">   
  59.                         <a href="${pageUrl}&pageNumber=${pageNumber}">尾页</a>   
  60.                     </pg:last>   
  61.                 </pg:index>   
  62.             </pg:pager>   
  63. </center></PRE>   
  64.    
[java] view plaincopy
  1. <div class="dp-highlighter"><div class="bar"><div class="tools">Jsp代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-default"><li><span><span>   </span></span></li></ol></div><pre class="JSP" style="display: none;" name="code"> </pre>  
  2. <div class="dp-highlighter">  
  3. <div class="bar">  
  4. <div class="tools">Jsp代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div>  
  5. </div>  
  6. <ol class="dp-default">  
  7. <li><span><span><<A href=</span><span class="string"><span style="color: #0000ff;">"mailto:%@taglib"</span></span><span>>%@taglib</A> uri=</span><span class="string"><span style="color: #0000ff;">"<A href="</span></span><span>http://jsptags.com/tags/navigation/pager</span><span class="string"><span style="color: #0000ff;">">http://jsptags.com/tags/navigation/pager</A>"</span></span><span> prefix=</span><span class="string"><span style="color: #0000ff;">"pg"</span></span><span>%>  </span></span></li>  
  8. </ol></div>  
  9. <pre class="JSP" style="display: none;" name="code"><<a href="mailto:%@taglib"><span style="color: #108ac6;">%@taglib</span></a> uri="<a href="http://jsptags.com/tags/navigation/pager"><span style="color: #108ac6;">http://jsptags.com/tags/navigation/pager</span></a>" prefix="pg"%></pre>  
  10. <div class="dp-highlighter">  
  11. <div class="bar">  
  12. <div class="tools">Jsp代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div>  
  13. </div>  
  14. <ol class="dp-default">  
  15. <li><span><span><a href=</span><span class="string"><span style="color: #0000ff;">"findAllDisplay.action"</span></span><span>><h1>displayTag分页</h1></a>   </span></span></li>  
  16. <li><span>            <s:iterator value=</span><span class="string"><span style="color: #0000ff;">"page.list"</span></span><span> id=</span><span class="string"><span style="color: #0000ff;">"pr"</span></span><span>>   </span></li>  
  17. <li><span>                <table width=</span><span class="string"><span style="color: #0000ff;">"300"</span></span><span> border=</span><span class="string"><span style="color: #0000ff;">"1"</span></span><span> bordercolor=</span><span class="string"><span style="color: #0000ff;">"000000"</span></span><span>  </span></li>  
  18. <li><span>                    style=</span><span class="string"><span style="color: #0000ff;">"table-layout: fixed; word-break: break-all"</span></span><span>>   </span></li>  
  19. <li><span>                    <tr>   </span></li>  
  20. <li><span>                        <td width=</span><span class="string"><span style="color: #0000ff;">"100"</span></span><span> bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  21. <li><span>                            编号:   </span></li>  
  22. <li><span>                        </td>   </span></li>  
  23. <li><span>                        <td width=</span><span class="string"><span style="color: #0000ff;">"500"</span></span><span> bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  24. <li><span>                            <s:property value=</span><span class="string"><span style="color: #0000ff;">"id"</span></span><span> />   </span></li>  
  25. <li><span>                        </td>   </span></li>  
  26. <li><span>                    </tr>   </span></li>  
  27. <li><span>                    <tr>   </span></li>  
  28. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  29. <li><span>                            名称:   </span></li>  
  30. <li><span>                        </td>   </span></li>  
  31. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  32. <li><span>                            <s:property value=</span><span class="string"><span style="color: #0000ff;">"name"</span></span><span> />   </span></li>  
  33. <li><span>                        </td>   </span></li>  
  34. <li><span>                    </tr>   </span></li>  
  35. <li><span>                    <tr>   </span></li>  
  36. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  37. <li><span>                            价格:   </span></li>  
  38. <li><span>                        </td>   </span></li>  
  39. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  40. <li><span>                            <s:property value=</span><span class="string"><span style="color: #0000ff;">"price"</span></span><span> />   </span></li>  
  41. <li><span>                        </td>   </span></li>  
  42. <li><span>                    </tr>   </span></li>  
  43. <li><span>                    <tr>   </span></li>  
  44. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  45. <li><span>                            类型:   </span></li>  
  46. <li><span>                        </td>   </span></li>  
  47. <li><span>                        <td bordercolor=</span><span class="string"><span style="color: #0000ff;">"ffffff"</span></span><span>>   </span></li>  
  48. <li><span>                            <s:property value=</span><span class="string"><span style="color: #0000ff;">"type"</span></span><span> />   </span></li>  
  49. <li><span>                        </td>   </span></li>  
  50. <li><span>                    </tr>   </span></li>  
  51. <li><span>                </table>   </span></li>  
  52. <li><span>                <br>   </span></li>  
  53. <li><span>            </s:iterator>   </span></li>  
  54. <li><span>            <pg:pager url=</span><span class="string"><span style="color: #0000ff;">"findAllPg.action"</span></span><span>  </span></li>  
  55. <li><span>                items=</span><span class="string"><span style="color: #0000ff;">"${page.maxElements}"</span></span><span>  </span></li>  
  56. <li><span>                maxPageItems=</span><span class="string"><span style="color: #0000ff;">"${pageSize}"</span></span><span> maxIndexPages=</span><span class="string"><span style="color: #0000ff;">"5"</span></span><span>>   </span></li>  
  57. <li><span>                <pg:index>   </span></li>  
  58. <li><span>                    <pg:first unless=</span><span class="string"><span style="color: #0000ff;">"current"</span></span><span>>   </span></li>  
  59. <li><span>                        <a href=</span><span class="string"><span style="color: #0000ff;">"${pageUrl}&pageNumber=${pageNumber}"</span></span><span>>首页</a>   </span></li>  
  60. <li><span>                    </pg:first>   </span></li>  
  61. <li><span>                    <pg:prev>   </span></li>  
  62. <li><span>                        <a href=</span><span class="string"><span style="color: #0000ff;">"${pageUrl}&pageNumber=${pageNumber}"</span></span><span>>上一页(${pageNumber})</a>   </span></li>  
  63. <li><span>                    </pg:prev>   </span></li>  
  64. <li><span>                    <pg:pages>   </span></li>  
  65. <li><span>                        <a href=</span><span class="string"><span style="color: #0000ff;">"${pageUrl}&pageNumber=${pageNumber}"</span></span><span>>${pageNumber}</a>   </span></li>  
  66. <li><span>                    </pg:pages>   </span></li>  
  67. <li><span>                    <pg:next>   </span></li>  
  68. <li><span>                        <a href=</span><span class="string"><span style="color: #0000ff;">"${pageUrl}&pageNumber=${pageNumber}"</span></span><span>>下一页(${pageNumber})</a>   </span></li>  
  69. <li><span>                    </pg:next>   </span></li>  
  70. <li><span>                    <pg:last unless=</span><span class="string"><span style="color: #0000ff;">"current"</span></span><span>>   </span></li>  
  71. <li><span>                        <a href=</span><span class="string"><span style="color: #0000ff;">"${pageUrl}&pageNumber=${pageNumber}"</span></span><span>>尾页</a>   </span></li>  
  72. <li><span>                    </pg:last>   </span></li>  
  73. <li><span>                </pg:index>   </span></li>  
  74. <li><span>            </pg:pager>   </span></li>  
  75. <li><span></center>  </span></li>  
  76. </ol></div>  
  77. <pre class="JSP" style="display: none;" name="code"><a href="findAllDisplay.action"><h1>displayTag分页</h1></a>  
  78.             <s:iterator value="page.list" id="pr">  
  79.                 <table width="300" border="1" bordercolor="000000"  
  80.                     style="table-layout: fixed; word-break: break-all">  
  81.                     <tr>  
  82.                         <td width="100" bordercolor="ffffff">  
  83.                             编号:  
  84.                         </td>  
  85.                         <td width="500" bordercolor="ffffff">  
  86.                             <s:property value="id" />  
  87.                         </td>  
  88.                     </tr>  
  89.                     <tr>  
  90.                         <td bordercolor="ffffff">  
  91.                             名称:  
  92.                         </td>  
  93.                         <td bordercolor="ffffff">  
  94.                             <s:property value="name" />  
  95.                         </td>  
  96.                     </tr>  
  97.                     <tr>  
  98.                         <td bordercolor="ffffff">  
  99.                             价格:  
  100.                         </td>  
  101.                         <td bordercolor="ffffff">  
  102.                             <s:property value="price" />  
  103.                         </td>  
  104.                     </tr>  
  105.                     <tr>  
  106.                         <td bordercolor="ffffff">  
  107.                             类型:  
  108.                         </td>  
  109.                         <td bordercolor="ffffff">  
  110.                             <s:property value="type" />  
  111.                         </td>  
  112.                     </tr>  
  113.                 </table>  
  114.                 <br>  
  115.             </s:iterator>  
  116.             <pg:pager url="findAllPg.action"  
  117.                 items="${page.maxElements}"  
  118.                 maxPageItems="${pageSize}" maxIndexPages="5">  
  119.                 <pg:index>  
  120.                     <pg:first unless="current">  
  121.                         <a href="${pageUrl}&pageNumber=${pageNumber}">首页</a>  
  122.                     </pg:first>  
  123.                     <pg:prev>  
  124.                         <a href="${pageUrl}&pageNumber=${pageNumber}">上一页(${pageNumber})</a>  
  125.                     </pg:prev>  
  126.                     <pg:pages>  
  127.                         <a href="${pageUrl}&pageNumber=${pageNumber}">${pageNumber}</a>  
  128.                     </pg:pages>  
  129.                     <pg:next>  
  130.                         <a href="${pageUrl}&pageNumber=${pageNumber}">下一页(${pageNumber})</a>  
  131.                     </pg:next>  
  132.                     <pg:last unless="current">  
  133.                         <a href="${pageUrl}&pageNumber=${pageNumber}">尾页</a>  
  134.                     </pg:last>  
  135.                 </pg:index>  
  136.             </pg:pager>  
  137. </center></pre>  
  138.    
Java代码 复制代码
  1. <PRE class=java name="code"><%@taglib uri="http://displaytag.sf.net" prefix="display"%>   
  2. <center>   
  3.                  <a href="findAllPg.action"><h1>Pager分页</h1></a>   
  4.             <display:table name="page.list" id="dt" export="true" class="" partialList="true" size="page.maxElements" pagesize="10"  
  5.                 requestURI="findAllDisplay.action">   
  6.                 <display:column sortable="true" property="id" title="编号">   
  7.                 </display:column>   
  8.   
  9.                 <display:column property="name" title="名称">   
  10.                 </display:column>   
  11.   
  12.                 <display:column property="price" title="价格">   
  13.                 </display:column>   
  14.   
  15.                 <display:column property="type" title="类型">   
  16.                 </display:column>   
  17.   
  18.                 <display:column title="操作">   
  19.                     <a href="#">编辑</a>   
  20.                     <a href="#">删除</a>   
  21.                 </display:column>   
  22.             </display:table>   
  23.   
  24. </center></PRE>   
  25.    
[java] view plaincopy
  1. <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span><%</span><span class="annotation"><span style="color: #646464;">@taglib</span></span><span> uri=</span><span class="string"><span style="color: #0000ff;">"http://displaytag.sf.net"</span></span><span> prefix=</span><span class="string"><span style="color: #0000ff;">"display"</span></span><span>%>   </span></span></li><li><span><center>   </span></li><li><span>                 <a href=</span><span class="string"><span style="color: #0000ff;">"findAllPg.action"</span></span><span>><h1>Pager分页</h1></a>   </span></li><li><span>            <display:table name=</span><span class="string"><span style="color: #0000ff;">"page.list"</span></span><span> id=</span><span class="string"><span style="color: #0000ff;">"dt"</span></span><span> export=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span>=</span><span class="string"><span style="color: #0000ff;">""</span></span><span> partialList=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> size=</span><span class="string"><span style="color: #0000ff;">"page.maxElements"</span></span><span> pagesize=</span><span class="string"><span style="color: #0000ff;">"10"</span></span><span>  </span></li><li><span>                requestURI=</span><span class="string"><span style="color: #0000ff;">"findAllDisplay.action"</span></span><span>>   </span></li><li><span>                <display:column sortable=</span><span class="string"><span style="color: #0000ff;">"true"</span></span><span> property=</span><span class="string"><span style="color: #0000ff;">"id"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"编号"</span></span><span>>   </span></li><li><span>                </display:column>   </span></li><li><span>  </span></li><li><span>                <display:column property=</span><span class="string"><span style="color: #0000ff;">"name"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"名称"</span></span><span>>   </span></li><li><span>                </display:column>   </span></li><li><span>  </span></li><li><span>                <display:column property=</span><span class="string"><span style="color: #0000ff;">"price"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"价格"</span></span><span>>   </span></li><li><span>                </display:column>   </span></li><li><span>  </span></li><li><span>                <display:column property=</span><span class="string"><span style="color: #0000ff;">"type"</span></span><span> title=</span><span class="string"><span style="color: #0000ff;">"类型"</span></span><span>>   </span></li><li><span>                </display:column>   </span></li><li><span>  </span></li><li><span>                <display:column title=</span><span class="string"><span style="color: #0000ff;">"操作"</span></span><span>>   </span></li><li><span>                    <a href=</span><span class="string"><span style="color: #0000ff;">"#"</span></span><span>>编辑</a>   </span></li><li><span>                    <a href=</span><span class="string"><span style="color: #0000ff;">"#"</span></span><span>>删除</a>   </span></li><li><span>                </display:column>   </span></li><li><span>            </display:table>   </span></li><li><span>  </span></li><li><span></center>  </span></li></ol></div><pre class="java" style="display: none;" name="code"><%@taglib uri="http://displaytag.sf.net" prefix="display"%>  
  2. <center>  
  3.                  <a href="findAllPg.action"><h1>Pager分页</h1></a>  
  4.             <display:table name="page.list" id="dt" export="true" class="" partialList="true" size="page.maxElements" pagesize="10"  
  5.                 requestURI="findAllDisplay.action">  
  6.                 <display:column sortable="true" property="id" title="编号">  
  7.                 </display:column>  
  8.   
  9.                 <display:column property="name" title="名称">  
  10.                 </display:column>  
  11.   
  12.                 <display:column property="price" title="价格">  
  13.                 </display:column>  
  14.   
  15.                 <display:column property="type" title="类型">  
  16.                 </display:column>  
  17.   
  18.                 <display:column title="操作">  
  19.                     <a href="#">编辑</a>  
  20.                     <a href="#">删除</a>  
  21.                 </display:column>  
  22.             </display:table>  
  23.   
  24. </center></pre>  
  25.    
Java代码 复制代码
  1. 添加action  
[java] view plaincopy
  1. 添加action  
Java代码 复制代码
  1.  <PRE class=java name="code">public class ProductAction extends ActionSupport {   
  2.     private static final long serialVersionUID = 3953659778944144652L;   
  3.   
  4.     private IProductServ productServ;   
  5.   
  6.     private IPaginationServ pagingServ;   
  7.   
  8.     private Product pr;   
  9.   
  10.     private List<Product> list;   
  11.   
  12.     private Pagination page;   
  13.   
  14.     private String pageNumber = "1";   
  15.   
  16.     private int pageSize = 10;   
  17.   
  18.     public String findAllDisplay() {   
  19.         String hql = "from Product p";   
  20.     // 这是displaytag获得pageNumber的方法,dt是displaytag 的id属性相对应的。比较特殊   
  21.         String paramName = (new ParamEncoder("dt")   
  22.                 .encodeParameterName(TableTagParameters.PARAMETER_PAGE));   
  23.         HttpServletRequest request = ServletActionContext.getRequest();   
  24.         try {   
  25.             Integer.parseInt(request.getParameter(paramName));   
  26.             pageNumber=request.getParameter(paramName);   
  27.         } catch (Exception e) {   
  28.             pageNumber = "1";   
  29.         }   
  30.         page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));   
  31.         return SUCCESS;   
  32.     }   
  33.     public String findAllPg() {   
  34.         String hql = "from Product p";   
  35.         try {   
  36.             Integer.parseInt(pageNumber);   
  37.         } catch (Exception e) {   
  38.             pageNumber = "1";   
  39.         }   
  40.         page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));   
  41.         return SUCCESS;   
  42.     }</PRE>   
  43.        
[java] view plaincopy
  1.  <div class="dp-highlighter"><div class="bar"><div class="tools">Java代码 <a title="复制代码" onclick="dp.sh.Toolbar.CopyToClipboard(this);return false;" href="http://115893520.javaeye.com/blog/397228#"><img src="http://115893520.javaeye.com/images/icon_copy.gif" alt="复制代码"></a></div></div><ol class="dp-j"><li><span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">class</span></strong></span><span> ProductAction </span><span class="keyword"><strong><span style="color: #7f0055;">extends</span></strong></span><span> ActionSupport {   </span></span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">static</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">final</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">long</span></strong></span><span> serialVersionUID = 3953659778944144652L;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> IProductServ productServ;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> IPaginationServ pagingServ;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> Product pr;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> List<Product> list;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> Pagination page;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> String pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">private</span></strong></span><span> </span><span class="keyword"><strong><span style="color: #7f0055;">int</span></strong></span><span> pageSize = </span><span class="number"><span style="color: #c00000;">10</span></span><span>;   </span></li><li><span>  </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> String findAllDisplay() {   </span></li><li><span>        String hql = </span><span class="string"><span style="color: #0000ff;">"from Product p"</span></span><span>;   </span></li><li><span>    </span><span class="comment"><span style="color: #008200;">// 这是displaytag获得pageNumber的方法,dt是displaytag 的id属性相对应的。比较特殊 </span></span><span>  </span></li><li><span>        String paramName = (</span><span class="keyword"><strong><span style="color: #7f0055;">new</span></strong></span><span> ParamEncoder(</span><span class="string"><span style="color: #0000ff;">"dt"</span></span><span>)   </span></li><li><span>                .encodeParameterName(TableTagParameters.PARAMETER_PAGE));   </span></li><li><span>        HttpServletRequest request = ServletActionContext.getRequest();   </span></li><li><span>        </span><span class="keyword"><strong><span style="color: #7f0055;">try</span></strong></span><span> {   </span></li><li><span>            Integer.parseInt(request.getParameter(paramName));   </span></li><li><span>            pageNumber=request.getParameter(paramName);   </span></li><li><span>        } </span><span class="keyword"><strong><span style="color: #7f0055;">catch</span></strong></span><span> (Exception e) {   </span></li><li><span>            pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>;   </span></li><li><span>        }   </span></li><li><span>        page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));   </span></li><li><span>        </span><span class="keyword"><strong><span style="color: #7f0055;">return</span></strong></span><span> SUCCESS;   </span></li><li><span>    }   </span></li><li><span>    </span><span class="keyword"><strong><span style="color: #7f0055;">public</span></strong></span><span> String findAllPg() {   </span></li><li><span>        String hql = </span><span class="string"><span style="color: #0000ff;">"from Product p"</span></span><span>;   </span></li><li><span>        </span><span class="keyword"><strong><span style="color: #7f0055;">try</span></strong></span><span> {   </span></li><li><span>            Integer.parseInt(pageNumber);   </span></li><li><span>        } </span><span class="keyword"><strong><span style="color: #7f0055;">catch</span></strong></span><span> (Exception e) {   </span></li><li><span>            pageNumber = </span><span class="string"><span style="color: #0000ff;">"1"</span></span><span>;   </span></li><li><span>        }   </span></li><li><span>        page = pagingServ.getProductByPage(hql, pageSize, Integer.parseInt(pageNumber));   </span></li><li><span>        </span><span class="keyword"><strong><span style="color: #7f0055;">return</span></strong></span><span> SUCCESS;   </span></li><li><span>    }  </span></li></ol></div><pre class="java" style=