在JSF中实现分页

****** home.xhtml ******
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml" xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html" xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml"><ui:define name="body">   <h:messages globalOnly="true" styleClass="message" />
   <h:form>
    <rich:panel>
     <f:facet name="header">推荐资源列表</f:facet>
     <h:dataTable id="ct" var="url" value="#{pageDataModel}" preserveDataModel="true" rowClasses="tr1,tr2" columnClasses="td1,td2,td3,td4,td5" headerClass="th1" footerClass="ft1" styleClass="tb1" rows="5">
      <h:column>
       <f:facet name="header">
        <h:outputText value="资源URL" />
       </f:facet>
       <a href="#{url.content}" target="_blank">#{url.content}</a>
      </h:column>
      <h:column>
       <f:facet name="header">
        <h:outputText value="资源描述" />
       </f:facet>
       <h:outputText value="#{url.info}" />
      </h:column>
      <h:column>
       <f:facet name="header">
        <h:outputText value="推荐人" />
       </f:facet>
       <h:outputText value="#{url.auth}" />
      </h:column>
      <h:column>
       <f:facet name="header">
        <h:outputText value="更新时间" />
       </f:facet>
       <h:outputText value="#{url.updateDate}">
        <f:convertDateTime type="both" dateStyle="medium" timeStyle="short" />
       </h:outputText>
      </h:column>
      <h:column>
       <h:commandLink action="#{urlLogicAction.delete}">
        <h:outputText value="删除" />
        <f:param name="url.id" value="#{url.id}" />
       </h:commandLink>
      </h:column>
      <f:facet name="footer">
       <rich:datascroller for="ct" align="right" rowsCountVar="rowsCount" displayedRowsCountVar="displayedRowsCountVar"
        firstRowIndexVar="firstRowIndex" lastRowIndexVar="lastRowIndex" pageCountVar="pageCount" pageIndexVar="pageIndex">
        <h:outputFormat value="#{example_messages['dataScroller_pages']}" styleClass="standard">
         <f:param value="#{rowsCount}" />
         <f:param value="#{displayedRowsCountVar}" />
         <f:param value="#{firstRowIndex}" />
         <f:param value="#{lastRowIndex}" />
         <f:param value="#{pageIndex}" />
         <f:param value="#{pageCount}" />
        </h:outputFormat>
       </rich:datascroller>
      </f:facet>
     </h:dataTable>
    </rich:panel>
   </h:form>
</ui:define>
</ui:composition>
 import java.util.List public class DataPage { private int datasetSize;private int startRow;private List data;/** * Create an object representing a sublist of a dataset. * @param datasetSize * is the total number of matching rows available. * @param startRow is the index within the complete dataset of the first element in the data list. * @param data is a list of consecutive objects from the dataset. */ public DataPage(int datasetSize, int startRow, List data) { this.datasetSize = datasetSize; this.startRow = startRow; this.data = data; }/** * Return the number of items in the full dataset. */ public int getDatasetSize() { return datasetSize; }/** * Return the offset within the full dataset of the first element in the * list held by this object. */ public int getStartRow() { return startRow; }/** * Return the list of objects held by this object, which is a continuous * subset of the full dataset. */ public List getData() { return data; } }

 

import java.util.Date;import javax.ejb.Stateless;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.Factory;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Out;
import org.jboss.seam.log.Log;
import org.jboss.seam.security.Identity;import com.c2one.util.DataPage;
import com.c2one.util.PagedListDataModel;@Stateless
@Name("urlLogicAction")
public class UrlLogicAction implements UrlLogic {@In(required = false)
private UrlResource urlResource;@Logger
Log log;@PersistenceContext
private EntityManager em;@In
Identity identity;@Out(required = false, scope = ScopeType.SESSION)
private PagedListDataModel pageDataModel;public String save() {
   urlResource.setUpdateDate(new Date());
   urlResource.setAuth(identity.getUsername());
   em.persist(urlResource);   if (pageDataModel != null) {
    pageDataModel.reflash();
   }   return "home";
}public String delete() {
   FacesContext ctx = FacesContext.getCurrentInstance();
   String urlId = (String) ctx.getExternalContext()
     .getRequestParameterMap().get("url.id");
   UrlResource rur = em.find(UrlResource.class, Long.parseLong(urlId));
   if (rur != null)
    em.remove(rur);   if (pageDataModel != null) {
    pageDataModel.reflash();
   }   return null;
}public int getTotalCount() {
   Query q = em.createQuery("select count(*) from UrlResource urls");
   Object result = q.getSingleResult();
   return Integer.parseInt(result.toString());
}public DataPage getDataPage(int startRow, int pageSize) {
   Query q = em
     .createQuery("from UrlResource urls order by urls.updateDate desc");
   q.setFirstResult(startRow);
   q.setMaxResults(pageSize);
   DataPage dataPage = new DataPage(getTotalCount(), startRow, q
     .getResultList());
   return dataPage;
}@Factory("pageDataModel")
public void getDataModel() {
   if (pageDataModel == null) {
    pageDataModel = new PagedListDataModel(20) {
     public DataPage fetchPage(int startRow, int pageSize) {
      return getDataPage(startRow, pageSize);
     }
    };
   }
}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值