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

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

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

    posts - 5,  comments - 7,  trackbacks - 0

    之前第一版有個問題,就是如果進行了刪除等操作并立即返回列表頁,被刪除的項還在那,但其實數(shù)據(jù)庫已經(jīng)被刪除了,需要做一個刷新列表操作才可以,本版就是修正此問題的。
    如果要修正此問題,就需要將分頁的擴展代碼也就是
    第一版中TestBean中的代碼放入業(yè)務(wù)相關(guān)管理Bean中。

    頁面代碼(其中testBean已經(jīng)變成業(yè)務(wù)Managed-bean user了):

        <f:view>
            
    <h:form id="formlist">    
                
    <rich:dataTable id="carList" width="483" columnClasses="col" rows="#{user.pageSize}"

                    value
    ="#{user.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>

    DataPage.java(沒有變化):
    import java.util.List;

    public class
     DataPage
    {

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


        
    private int datasetSize;

        
    private int
     startRow;

        
    private
     List data;

        
    /**
         * 
         * 
    @param datasetSize
         *            數(shù)據(jù)集大小
         * 
    @param
     startRow
         *            起始行
         * 
    @param
     data
         *            數(shù)據(jù)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 已填充好的數(shù)據(jù)集
         
    */

        
    public List getData()
        
    {

            
    return
     data;

        }


    }


    PagedListDataModel.java(添加了一些注釋):

    import java.util.List;

    import
     javax.faces.model.DataModel;

    /**
     * 
     * TODO 分頁所使用的類

     * 
    @author <a href="mailto:tianlu@jsecode.com">TianLu</a>
     * 
    @version
     $Rev$ <br>
     *          $Id$
     
    */

    /* 使用方法:
     * 前臺的功能模塊Bean(例如User)中加入類似下面的代碼,可根據(jù)您的需要進行相應(yīng)修改
     * private PagedListDataModel dataModel;
        private int pageSize = 10;
        public int getPageSize()
        {
            return pageSize;
        }

        public PagedListDataModel getDataModel()
        {
            
            if ( dataModel == null ) {
                dataModel = new PagedListDataModel(pageSize)
                {
                    //查詢分頁函數(shù)
                    public DataPage fetchPage(int startRow, int pageSize)
                    {
                        // call enclosing managed bean method to fetch the data
                        CarBeanDAO dao = new CarBeanDAO();
                        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());
                        Query q = EntityManagerHelper.createQuery("select count(*) from CarBean");
                        return new DataPage(Integer.parseInt(q.getSingleResult().toString()), startRow, list);                    
                    }
                };
            }
            
            return dataModel;
        }
    */


    /* 前臺控件像這樣使用
     * <rich:dataTable id="carList" width="483" columnClasses="col" rows="#{user.pageSize}"
        value="#{user.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}"/>
    */


    /*
     * 進行刪除等操作后會立即改變列表項并且返回列表頁的,請調(diào)用本類的refresh方法刷新當(dāng)前頁面
     * 使用方法:
     *             dao.delete(bean);
     *            dataModel.refresh();
     
    */

    public abstract class PagedListDataModel extends DataModel {
        
    int
     pageSize;
        
    int
     rowIndex;
        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() {
            
    return
     getPage().getDatasetSize();
        }


        
    /**
         * 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() {
            
    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);
            }

            
    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);
                startRow 
    =
     page.getStartRow();
            }
     else if (rowIndex >= endRow) {
                page 
    =
     fetchPage(rowIndex, pageSize);
                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();
            
    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);

        
    /**
         * 進行刪除等操作后會立即改變列表項并且返回列表頁的,請調(diào)用此方法,用于刷新列表。
         
    */

        
    public void refresh() {
            
    if (this.page != null
    {
                
    this.page = null
    ;
                getPage();
            }

        }

    }



    User中的delete方法:
        public String delete()
        
    {
            FacesContext ctx 
    =
     FacesContext.getCurrentInstance();
            
    int id = Integer.parseInt(ctx.getExternalContext().getRequestParameterMap().get("id"
    ));
            EntityManagerHelper.beginTransaction();
            CarBeanDAO dao 
    = new
     CarBeanDAO();
            CarBean bean 
    =
     dao.findById(id);
            
    if(bean != null
    )
            
    {            
                dao.delete(bean);
                dataModel.refresh();
            }

                
            EntityManagerHelper.commit();
            
    return null;
        }

    User中scrollerPage的相關(guān)代碼:
        private int scrollerPage = 1;

        
    public int
     getScrollerPage()
        
    {
            
    return
     scrollerPage;
        }


        
    public void setScrollerPage(int scrollerPage)
        
    {
            
    this.scrollerPage =
     scrollerPage;
            System.out.println(
    "current page = " +
     scrollerPage);
        }

    Faces-config.xml:
     <managed-bean>
      
    <managed-bean-name>user</managed-bean-name>
      
    <managed-bean-class>com.gcoresoft.jsfdemo.bean.User</managed-bean-class>
      
    <managed-bean-scope>session</managed-bean-scope>
     
    </managed-bean>

    有人反映會出現(xiàn)獲取兩次DataModel的情況,為什么呢?
    經(jīng)過我的測試,是因為設(shè)置的datatable的rows屬性的值大于了程序中pageSize的值,比如你設(shè)置rows="12",而你的pageSize設(shè)置的是10,那么JSF為了滿足顯示12條得條件,就會取兩次數(shù)據(jù)集組成一個12跳得數(shù)據(jù)集顯示出來,所以給定的rows屬性最好可以使用相關(guān)管理bean的pageSize變量,這樣前后臺統(tǒng)一數(shù)據(jù)條目,提高性能和可操作性。

    為了方便大家JSF的項目開發(fā),后面我會推出一套我自己寫的一個基于JSF的框架,當(dāng)然此框架是基于實際項目成功實施后提取出來的,主要是簡化了對表單的操作,增加一些常用的工具套件,例如分頁,驗證器等等。對表單操作部分的簡化主要體現(xiàn)在:每種控件都可以和固定的模型綁定,這樣操作模型就可以做到對前臺控件的控制,比如后臺取值可以使用attributes.get("username"),設(shè)置前臺控件值可以使用attributes.put("username","admin"),對選擇控件操作也更加簡便,當(dāng)使用put的方法的時候,前臺會自動選擇到那項,更重要的是這些簡化不需要你寫程序代碼,寫一些簡單的配置文件即可,而且選擇項不僅可以設(shè)置在配置文件中,還可以通過數(shù)據(jù)庫等其他數(shù)據(jù)源獲取,大大提高開發(fā)效率。
    posted on 2008-11-23 01:00 Vincent-chen 閱讀(555) 評論(0)  編輯  收藏 所屬分類: richfaces
    主站蜘蛛池模板: 久久性生大片免费观看性| 亚洲的天堂av无码| 日本一区二区三区在线视频观看免费| 毛片免费在线观看网址| 亚洲首页国产精品丝袜| av无码久久久久不卡免费网站| 亚洲国产美女精品久久| 国产曰批免费视频播放免费s| 亚洲国产av美女网站| 免费99精品国产自在现线| 亚洲国产精品久久丫| 蜜桃精品免费久久久久影院| 亚洲av成人中文无码专区| 免费观看四虎精品国产永久| 无遮挡免费一区二区三区| 中文字幕亚洲综合久久菠萝蜜| 免费无码一区二区三区蜜桃| 亚洲av无码成h人动漫无遮挡 | 好吊色永久免费视频大全| 国产亚洲精品无码专区| a国产成人免费视频| 亚洲男人第一av网站| 国产福利在线观看免费第一福利| 亚洲日本中文字幕天天更新| 亚洲精品成人久久久| 免费h视频在线观看| 色老板亚洲视频免在线观| 免费人成年激情视频在线观看| 亚洲免费视频一区二区三区| 亚洲视频一区网站| 一级特黄录像免费播放中文版| 亚洲热线99精品视频| 香蕉免费在线视频| 亚洲婷婷天堂在线综合| 人人狠狠综合久久亚洲高清| 久操视频免费观看| 亚洲国产精品成人午夜在线观看| 国产日产亚洲系列| 啦啦啦高清视频在线观看免费| 51午夜精品免费视频| 亚洲偷自精品三十六区|