<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-348  評論-598  文章-0  trackbacks-0

     

    package com.jsecode.util.pagination;

    import java.util.ArrayList;

    import java.util.List;

    import javax.faces.context.FacesContext;

    import javax.faces.model.DataModel;

    import com.gcoresoft.jsfdemo.bean.CarBeanDAO;

    public class DataScrollerList extends BasePagedBackingBean
    {

        CarBeanDAO dao 
    = null;

        
    public DataScrollerList()
        
    {
            dao 
    = new CarBeanDAO();
        }


        
    public int getTotalCount()
        
    {

            
    int totalCount = 0;

            totalCount 
    = dao.getTotalCount();

            
    return totalCount;

        }


        
    /**
         * 
         * 在DataScrollerList這個 Backing Bean中加一些東西,<br>
         * 
         * 調用業務邏輯,并將數據交給PagedListDataModel,來幫我們完成最后的分頁工作。
         * 
         
    */


        
    public DataPage getDataPage(int startRow, int pageSize)
        
    {

            DataPage dataPage 
    = dao.getDataPage(startRow, pageSize);

            
    return dataPage;

        }


    }

    JSP頁面:

        <f:view>
            
    <h:form>
                
    <rich:dataTable id="carList" width="483" rows="12" columnClasses="col"
                    value
    ="#{scrollerList.dataModel}" var="car">
                    
    <f:facet name="header">
                        
    <rich:columnGroup>
                            
    <h:column>
                                
    <h:outputText styleClass="headerText" value="Name" />
                            
    </h:column>
                            
    <h:column>
                                
    <h:outputText styleClass="headerText" value="Decription" />
                            
    </h:column>
                            
    <h:column>
                                
    <h:outputText styleClass="headerText" value="Base Price" />
                            
    </h:column>
                            
    <h:column>
                                
    <h:outputText styleClass="headerText" value="Time" />
                            
    </h:column>
                            
    <h:column>
                                
    <h:outputText styleClass="headerText" value="操作操作" />
                            
    </h:column>                        
                        
    </rich:columnGroup>
                    
    </f:facet>
        
                    
    <h:column>
                        
    <h:outputText value="#{car.name}" />
                    
    </h:column>
                    
    <h:column>
                        
    <h:outputText value="#{car.description}" />
                    
    </h:column>
                    
    <h:column>
                        
    <h:outputText value="#{car.baseprice}" />
                    
    </h:column>
                    
    <h:column>
                        
    <h:outputText value="#{car.timestamp}" />
                    
    </h:column>
                    
    <h:column>
                        
    <h:commandLink action="#{user.delete}" value="刪除" >
                            
    <f:param name="id" value="#{car.id}"/>
                        
    </h:commandLink>
                    
    </h:column>                
                
    </rich:dataTable>
                
    <rich:datascroller for="carList" id="dc1"
                style
    ="width:483px" page="#{user.scrollerPage}"/>                        
            
    </h:form>
        
    </f:view>
    faces-config.xml:
     <managed-bean>
      
    <managed-bean-name>scrollerList</managed-bean-name>
      
    <managed-bean-class>com.jsecode.util.pagination.DataScrollerList</managed-bean-class>
      
    <managed-bean-scope>session</managed-bean-scope>
     
    </managed-bean> 
    DataPage.java:
    package com.jsecode.util.pagination;

    import java.util.List;

    public class DataPage
    {

        
    /**
         * 將需要的頁的數據封裝到一個DataPage中去, 這個類表示了我們需要的一頁的數據,<br>
         * 里面包含有三個元素:datasetSize,startRow,和一個用于表示具體數據的List。<br>
         * datasetSize表示了這個記錄集的總條數,查詢數據的時候,使用同樣的條件取count即可,<br>
         * startRow表示該頁的起始行在數據庫中所有記錄集中的位置
         
    */


        
    private int datasetSize;

        
    private int startRow;

        
    private List data;

        
    /**
         * 
         * 
    @param datasetSize
         *            數據集大小
         * 
    @param startRow
         *            起始行
         * 
    @param data
         *            數據list
         
    */

        
    public DataPage(int datasetSize, int startRow, List data)
        
    {

            
    this.datasetSize = datasetSize;

            
    this.startRow = startRow;

            
    this.data = data;

        }


        
    /**
         * 
         * 
    @return
         
    */

        
    public int getDatasetSize()
        
    {

            
    return datasetSize;

        }


        
    public int getStartRow()
        
    {

            
    return startRow;

        }


        
    /**
         * 
         * 
    @return 已填充好的數據集
         
    */

        
    public List getData()
        
    {

            
    return data;

        }


    }

    PagedListDataModel.java:
    package com.jsecode.util.pagination;

    import javax.faces.model.DataModel;

    /**

     * A special type of JSF DataModel to allow a datatable and datascroller to page

     * through a large set of data without having to hold the entire set of data in

     * memory at once.

     * <p>

     * Any time a managed bean wants to avoid holding an entire dataset, the managed

     * bean should declare an inner class which extends this class and implements

     * the fetchData method. This method is called as needed when the table requires

     * data that isn\'t available in the current data page held by this object.

     * <p>

     * This does require the managed bean (and in general the business method that

     * the managed bean uses) to provide the data wrapped in a DataPage object that

     * provides info on the full size of the dataset.

     
    */


    /**
     * 
     * 這個類里面的方法被dataTable調用順序為:<br>
     * 
     * 1.構造函數public PagedListDataModel(int pageSize) <br>
     * 
     * 2.getRowCount()<br>
     * 
     * 3.setRowIndex()<br>
     * 
     * 4.public boolean isRowAvailable()<br>
     * 
     * 5.public Object getRowData()
     * 
     
    */


    public abstract class PagedListDataModel extends DataModel
    {

        
    int pageSize;

        
    int rowIndex;

        
    private int rowCount = -1;

        DataPage page;

        
    /**
         * 
         * Create a datamodel that pages through the data showing the specified
         * 
         * number of rows on each page.
         * 
         
    */


        
    public PagedListDataModel(int pageSize)
        
    {

            
    super();

            
    this.pageSize = pageSize;

            
    this.rowIndex = -1;

            
    this.page = null;

        }


        
    /**
         * 
         * Not used in this class; data is fetched via a callback to the fetchData
         * 
         * method rather than by explicitly assigning a list.
         * 
         
    */


        
    public void setWrappedData(Object o)
        
    {

            
    if ( o instanceof DataPage ) {
                
    this.page = (DataPage) o;
            }
     else {
                
    throw new UnsupportedOperationException("setWrappedData");
            }


        }


        
    public int getRowIndex()
        
    {

            
    return rowIndex;

        }


        
    /**
         * 
         * Specify what the "current row" within the dataset is. Note that the
         * 
         * UIData component will repeatedly call this method followed by getRowData
         * 
         * to obtain the objects to render in the table.
         * 
         
    */


        
    public void setRowIndex(int index)
        
    {

            rowIndex 
    = index;

        }


        
    /**
         * 
         * Return the total number of rows of data available (not just the number of
         * 
         * rows in the current page!).
         * 
         
    */


        
    public int getRowCount()
        
    {

            
    if ( rowCount < 0 ) {
                rowCount 
    = fetchRowCount();
            }


            
    return rowCount;

        }


        
    /**
         * 
         * Return a DataPage object; if one is not currently available then fetch
         * 
         * one. Note that this doesn\'t ensure that the datapage returned includes
         * 
         * the current rowIndex row; see getRowData.
         * 
         
    */


        
    private DataPage getPage(String name)
        
    {

            
    if ( page != null ) {

                
    return page;

            }


            
    int rowIndex = getRowIndex();

            
    int startRow = rowIndex;

            
    if ( rowIndex == -1 ) {

                
    // even when no row is selected, we still need a page

                
    // object so that we know the amount of data available.

                startRow 
    = 0;

            }


            
    // invoke method on enclosing class

            page 
    = fetchPage(startRow, pageSize);

            
    return page;

        }


        
    /** */

        
    /**
         * 
         * Return the object corresponding to the current rowIndex. If the DataPage
         * 
         * object currently cached doesn\'t include that index then fetchPage is
         * 
         * called to retrieve the appropriate page.
         * 
         
    */


        
    public Object getRowData()
        
    {

            
    if ( rowIndex < 0 ) {

                
    throw new IllegalArgumentException(

                
    "Invalid rowIndex for PagedListDataModel; not within page");

            }


            
    // ensure page exists; if rowIndex is beyond dataset size, then

            
    // we should still get back a DataPage object with the dataset size

            
    // in it

            
    if ( page == null ) {

                page 
    = fetchPage(rowIndex, pageSize);

                rowCount 
    = page.getDatasetSize();//

            }


            
    int datasetSize = page.getDatasetSize();

            
    int startRow = page.getStartRow();

            
    int nRows = page.getData().size();

            
    int endRow = startRow + nRows;

            
    if ( rowIndex >= datasetSize ) {
                
    throw new IllegalArgumentException("Invalid rowIndex");
            }


            
    if ( rowIndex < startRow ) {// 向前取數據

                page 
    = fetchPage(rowIndex, pageSize);

                rowCount 
    = page.getDatasetSize();

                startRow 
    = page.getStartRow();

            }
     else if ( rowIndex >= endRow ) // 向后取數據

                page 
    = fetchPage(rowIndex, pageSize);

                rowCount 
    = page.getDatasetSize();//

                startRow 
    = page.getStartRow();

            }


            
    return page.getData().get(rowIndex - startRow);

        }


        
    public Object getWrappedData()
        
    {

            
    return page.getData();

        }


        
    /** */

        
    /**
         * 
         * Return true if the rowIndex value is currently set to a value that
         * 
         * matches some element in the dataset. Note that it may match a row that is
         * 
         * not in the currently cached DataPage; if so then when getRowData is
         * 
         * called the required DataPage will be fetched by calling fetchData.
         * 
         
    */


        
    public boolean isRowAvailable()
        
    {

            DataPage page 
    = getPage("isRowAvailable");

            
    if ( page == null ) {

                
    return false;

            }


            
    int rowIndex = getRowIndex();

            
    if ( rowIndex < 0 ) {

                
    return false;

            }
     else if ( rowIndex >= page.getDatasetSize() ) {

                
    return false;

            }
     else {

                
    return true;

            }


        }


        
    /** */

        
    /**
         * 
         * Method which must be implemented in cooperation with the managed bean
         * 
         * class to fetch data on demand.
         * 
         
    */


        
    public abstract DataPage fetchPage(int startRow, int pageSize);

        
    public abstract int fetchRowCount();

    }

    BasePagedBackingBean:
    package com.jsecode.util.pagination;

    import javax.faces.model.DataModel;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    public abstract class BasePagedBackingBean
    {

        
    protected abstract DataPage getDataPage(int startRow, int pageSize);

        
    public abstract int getTotalCount();

        
    private DataModel dataModel;

        
    private int i = 0;

        
    // 為什么getDataModel這個方法要調用兩次?非常不解啊
        public DataModel getDataModel()
        
    {

            
    if ( dataModel == null ) {
                dataModel 
    = new LocalDataModel(10);
            }


            
    return dataModel;

        }


        
    public Object getBean(String beanName)
        
    {

            
    return null;
        }


        
    private class LocalDataModel extends PagedListDataModel
        
    {

            
    public LocalDataModel(int pageSize)
            
    {

                
    super(pageSize);

            }


            
    public int fetchRowCount()
            
    {

                
    return getTotalCount();

            }


            
    public DataPage fetchPage(int startRow, int pageSize)
            
    {

                
    // call enclosing managed bean method to fetch the data

                
    return getDataPage(startRow, pageSize);

            }


        }


    }

    DataScrollerList.java:
    package com.jsecode.util.pagination;

    import java.util.ArrayList;

    import java.util.List;

    import javax.faces.context.FacesContext;

    import javax.faces.model.DataModel;

    import com.gcoresoft.jsfdemo.bean.CarBeanDAO;

    public class DataScrollerList extends BasePagedBackingBean
    {

        CarBeanDAO dao 
    = null;

        
    public DataScrollerList()
        
    {
            dao 
    = new CarBeanDAO();
        }


        
    public int getTotalCount()
        
    {

            
    int totalCount = 0;

            totalCount 
    = dao.getTotalCount();

            
    return totalCount;

        }


        
    /**
         * 
         * 在DataScrollerList這個 Backing Bean中加一些東西,<br>
         * 
         * 調用業務邏輯,并將數據交給PagedListDataModel,來幫我們完成最后的分頁工作。
         * 
         
    */


        
    public DataPage getDataPage(int startRow, int pageSize)
        
    {

            DataPage dataPage 
    = dao.getDataPage(startRow, pageSize);

            
    return dataPage;

        }


    }

    每個DAO都實現了這個接口:
    package com.gcoresoft.jsfdemo.bean;

    import java.util.List;

    import com.jsecode.util.pagination.DataPage;

    /**
     * 
    @author TiGERTiAN tigertian@gmail.com
     * 
    @version  
     * Oct 29, 2008 9:46:55 AM
     
    */


    public interface Pagination
    {
        
    public int getTotalCount();
        
    public DataPage getDataPage(int startRow, int pageSize);
        
    public List getPagedList(int startRow, int pageSize);
    }

    DAO中的相關代碼:
        public DataPage getDataPage(int startRow, int pageSize)
        
    {
            
    // TODO Auto-generated method stub
            return new DataPage(getTotalCount(), startRow, getPagedList(startRow, pageSize));
        }


        
    public int getTotalCount()
        
    {
            Query q 
    = EntityManagerHelper.createQuery("select count(*) from CarBean");
            System.out.println(
    "row count = " + q.getSingleResult().toString());
            
    return Integer.parseInt(q.getSingleResult().toString());
        }


        
    public List getPagedList(int startRow, int pageSize)
        
    {
               String sql 
    = "from CarBean model order by model.id desc";

               Query query 
    = EntityManagerHelper.createQuery(sql);

               query.setFirstResult(startRow);

               query.setMaxResults(pageSize);

               List list 
    = query.getResultList();
               System.out.println(
    "current row count = " + list.size());
               
    return list;

        }




    ---------------------------------------------------------
    專注移動開發

    Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
    posted on 2008-10-29 10:58 TiGERTiAN 閱讀(2840) 評論(3)  編輯  收藏 所屬分類: JavaJSF

    評論:
    # re: RichFaces中使用datatable和datascroller進行分頁(使用數據庫分頁)(含源碼)(JSF 1.2,RichFaces 3.2.1GA) 2008-12-12 20:31 | BioMed
    Hi!

    Can you post the user bean?
    I'm interested in how you manage the scroller control.

    Thanks you in advance!  回復  更多評論
      
    # re: RichFaces中使用datatable和datascroller進行分頁(使用數據庫分頁)(含源碼)(JSF 1.2,RichFaces 3.2.1GA) 2008-12-12 23:16 | TiGERTiAN
    @BioMed
    Hi,you can reference this one http://www.tkk7.com/TiGERTiAN/archive/2008/11/04/238692.html, and if you have more questions about JSF pagnation, welcome to ask.  回復  更多評論
      
    # re: RichFaces中使用datatable和datascroller進行分頁(使用數據庫分頁)(含源碼)(JSF 1.2,RichFaces 3.2.1GA) 2008-12-18 18:28 | BioMed
    Many thanks i'll try it.  回復  更多評論
      
    主站蜘蛛池模板: 日韩一区二区免费视频| 女人毛片a级大学毛片免费| 九月婷婷亚洲综合在线| 亚洲欧美日韩国产成人| 女人张腿给男人桶视频免费版| 亚洲国产成人精品无码一区二区| 亚欧免费视频一区二区三区| 亚洲视频精品在线观看| 国产精品1024永久免费视频| 亚洲三级在线播放| 成全影视免费观看大全二| 亚洲AV无码一区二区大桥未久 | 免费播放在线日本感人片| 亚洲人成色777777在线观看| 丝袜足液精子免费视频| 亚洲国产天堂在线观看| 免费视频爱爱太爽了| 亚洲色偷偷色噜噜狠狠99网| 在线日韩av永久免费观看| 一级做a爰片久久免费| 亚洲国产成人一区二区三区| 亚洲精品视频免费在线观看| 亚洲已满18点击进入在线观看| 精品国产一区二区三区免费看| 美女被爆羞羞网站在免费观看| 亚洲精品一品区二品区三品区| 16女性下面扒开无遮挡免费| 亚洲色偷偷综合亚洲AV伊人蜜桃 | 免费一级毛片在线播放不收费| 亚洲精品视频免费观看| 亚洲好看的理论片电影| 四虎免费大片aⅴ入口| 久久免费99精品国产自在现线| 久久亚洲春色中文字幕久久久| 免费理论片51人人看电影| 一级有奶水毛片免费看| 亚洲永久在线观看| 亚洲高清国产拍精品26U| 成人免费视频观看无遮挡| 成人自慰女黄网站免费大全| 亚洲国产精品xo在线观看|