隨筆-348  評論-598  文章-0  trackbacks-0
          jsp頁面中的相關(guān)代碼:
              <f:view>
                  
          <h:form>
                      
          <rich:dataTable id="carList" width="483" rows="10" columnClasses="col"
                          value
          ="#{user.allCars}" 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>
          后臺User類的代碼:
          public class User
          {

              
          private int scrollerPage = 1;
              
              
          private List<CarBean> allCars; 
              
              
          public User()
              
          {

              }

              
              
          public int getScrollerPage()
              
          {
                  
          return scrollerPage;
              }


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


              
          public List<CarBean> getAllCars()
              
          {
                  CarBeanDAO dao 
          = new CarBeanDAO();
                  Query q 
          = EntityManagerHelper.createQuery("from CarBean cars order by cars.id desc");

                  
          return q.getResultList();
              }

              
              
          public void setAllCars(List<CarBean> allCars)
              
          {
                  
          this.allCars = allCars;
              }

              
              
          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);
                  EntityManagerHelper.commit();
                  
          return null;
              }

          }

          datatable+datascroller使用的是偽分頁的方式,也就是說在數(shù)據(jù)庫層那并沒有分頁,只是程序一次讀出所有數(shù)據(jù)然后進(jìn)行程序分頁操作,這樣適合數(shù)據(jù)量不太大的分頁操作,數(shù)據(jù)量過大的話就需要自己寫數(shù)據(jù)模型進(jìn)行操作了。本分頁代碼加入了刪除記錄的功能,delete結(jié)果返回null會使刪除操作完成后返回當(dāng)前頁面,這樣就會發(fā)現(xiàn)剛才被刪除數(shù)據(jù)消失了,后面數(shù)據(jù)都上來一條。

          ---------------------------------------------------------
          專注移動開發(fā)

          Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian
          posted on 2008-10-28 21:50 TiGERTiAN 閱讀(2487) 評論(0)  編輯  收藏 所屬分類: Java 、JSF
          主站蜘蛛池模板: 沽源县| 郯城县| 渝中区| 佛坪县| 昭觉县| 枣阳市| 镇远县| 布尔津县| 海晏县| 营口市| 山东省| 平武县| 蒙自县| 嘉义市| 麦盖提县| 永寿县| 湟中县| 万源市| 荥经县| 黎川县| 永嘉县| 江达县| 龙川县| 张家港市| 肃宁县| 蓬溪县| 依兰县| 安龙县| 翁牛特旗| 额济纳旗| 类乌齐县| 惠州市| 正宁县| 广宁县| 临汾市| 安丘市| 武隆县| 万盛区| 江油市| 四川省| 汉沽区|