如何在JSF 2中进行分页

时间:2014-10-06 17:22:13

标签: jsf pagination jsf-2.2

我是JSF的新手,正在尝试使用<ui:repeat>实现分页。我正在使用@ViewScoped,但目前我的状态未保存。每次辅助bean重置页面信息并且不显示下一页。附上源代码以供参考。

WorkItemList.xhtml

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html 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:ui="http://java.sun.com/jsf/facelets"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    template="/WEB-INF/template/default.xhtml">

<ui:define name="content">
    <h:form id="form">

          <ui:repeat value="#{repeatPaginator.model}" var="listItem">
            <div>
                <h:outputText value="#{listItem}"/>
            </div>
        </ui:repeat>
        <h:commandButton value="&lt; prev" action="#{repeatPaginator.prev}"/>
        <h:outputText value="#{repeatPaginator.pageIndex} / #{repeatPaginator.pages}"/>
        <h:commandButton value="next &gt;" action="#{repeatPaginator.next}"/>
        <h:inputHidden value="#{repeatPaginator.pageIndex}"/>
    </h:form>
</ui:define>
</ui:composition>

Backing Bean - RepeatPaginator.java

package test;
@ManagedBean
@ViewScoped
public class RepeatPaginator implements Serializable{

    private static final long serialVersionUID = 1L;
    private static final int DEFAULT_RECORDS_NUMBER = 2;
    private static final int DEFAULT_PAGE_INDEX = 1;
    private TestDelegate delegate;
    private int records;
    private int recordsTotal;
    private int pageIndex;
    private int pages;
    private List<String> model;
    public RepeatPaginator() {
        delegate = new TestDelegate();
        this.records = DEFAULT_RECORDS_NUMBER;
        this.pageIndex = DEFAULT_PAGE_INDEX;
        // Get Model
        this.model = delegate.fetchCurrentList(getFirst(), getFirst()+records);
        this.recordsTotal = delegate.getListSize();

        if (records > 0) {
            pages = records <= 0 ? 1 : recordsTotal / records;

            if (recordsTotal % records > 0) {
                pages++;
            }

            if (pages == 0) {
                pages = 1;
            }
        } else {
            records = 1;
            pages = 1;
        }

        updateModel();
    }

    public void updateModel() {
        int fromIndex = getFirst();
        int toIndex = getFirst() + records;

        if(toIndex > this.recordsTotal) {
            toIndex = this.recordsTotal;
        }

         setModel(delegate.fetchCurrentList(fromIndex, toIndex));
    }

    public void next() {
        if(this.pageIndex < pages) {
            this.pageIndex++;
        }

        updateModel();
    }

    public void prev() {
        if(this.pageIndex > 1) {
            this.pageIndex--;
        }

        updateModel();
    }   

    public int getRecords() {
        return records;
    }

    public int getRecordsTotal() {
        return recordsTotal;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public int getPages() {
        return pages;
    }

    public int getFirst() {
        return (pageIndex * records) - records;
    }

    public List<String> getModel() {
        if(model==null)
            updateModel();
        return model;
    }

    public void setModel(List<String> model) {
        this.model = model;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }
}

委托 - TestDelegate.java

@ViewScoped
public class TestDelegate {

    private List<String> list = new ArrayList<String>();
    public TestDelegate()
    {
        this.list.add("Item 1");
        this.list.add("Item 2");
        this.list.add("Item 3");
        this.list.add("Item 4");
        this.list.add("Item 5");
        this.list.add("Item 6");
        this.list.add("Item 7");
        this.list.add("Item 8");
        this.list.add("Item 9");
        this.list.add("Item 10");
        this.list.add("Item 11");
    }

    public List<String> fetchCurrentList(int from, int to)
    {
        return list.subList(from, to);
    }

    public int getListSize()
    {
        return this.list.size();
    }
}

1 个答案:

答案 0 :(得分:2)

@ViewScoped bean并非“一直神奇地存在”。在请求结束时,它的值将被序列化并且bean被破坏。

这导致在下一个请求开始时重新构造bean(单击下一个)。您的Container将注意再次反序列化存储的值,以便返回bean的实际状态。 但是:在 Bean-Construction之后发生,这意味着当您尝试使用bean的构造函数中的值时,不会恢复任何内容。

将您的逻辑移动到使用@PostConstruct注释的方法 - 然后将加载所有值,您可以获取正确的结果。一旦恢复了bean的先前状态,容器将调用用@PostConstruct注释的方法。

当您开始使用ManagedProperties或CDI-Injections时,这一点很重要。

public RepeatPaginator() {
   //no serialized data available here, Good location to initialize some stuff 
   //that is independent.
   delegate = new TestDelegate();
   this.records = DEFAULT_RECORDS_NUMBER;
   this.pageIndex = DEFAULT_PAGE_INDEX;
}

@PostConstruct
public void init() {
        //Here the ViewScoped values are restored. We can start to use them.

        // Get Model
        this.model = delegate.fetchCurrentList(getFirst(), getFirst()+records);
        this.recordsTotal = delegate.getListSize();

        if (records > 0) {
            pages = records <= 0 ? 1 : recordsTotal / records;

            if (recordsTotal % records > 0) {
                pages++;
            }

            if (pages == 0) {
                pages = 1;
            }
        } else {
            records = 1;
            pages = 1;
        }

        updateModel();
    }