p:dataTable / p:dataGrid和f:setPropertyActionListener中的延迟加载

时间:2014-03-28 11:07:33

标签: jsf primefaces datatable lazy-loading jsf-2.2

我有<p:dataTable>如下。

<p:dataTable var="row" value="#{testManagedBean.list}">
    <p:column>
        <h:outputText value="#{row.subCatId}"/>
    </p:column>

    <p:column>
        <p:commandLink process="@this">
            <h:outputText value="#{row.subCatName}"/>
            <f:setPropertyActionListener target="#{testManagedBean.subCatName}" value="#{row.subCatName}"/>
        </p:commandLink>
    </p:column>
</p:dataTable>

相应的RequestScoped托管bean如下所示。

@ManagedBean
@RequestScoped
public final class TestManagedBean
{
    @EJB
    private final TestBeanLocal testService=null;
    private List<SubCategory>list;
    private String subCatName;

    @PostConstruct
    private void init()
    {
        list=testService.getSubCategoryList();
    }

    public List<SubCategory> getList() {
        return list;
    }

    public String getSubCatName() {
        return subCatName;
    }

    public void setSubCatName(String subCatName) {
        System.out.println("setSubCatName() called : "+subCatName);
        this.subCatName = subCatName;
    }
}

单击<p:commandLink>内的<p:dataTable>时,将调用与<f:setProperyActionListener>setSubCatName())关联的相应setter方法,并将值正确设置为target属性,即此托管bean中的subCatName

我已经避免了许多事情,例如分页,只有一个最小的例子。


现在,我需要懒洋洋地填充这个<p:dataTable>。我正在更改此<p:dataTable>和相应的托管bean,如下所示。

<p:dataTable var="row" value="#{testManagedBean}" lazy="true" rows="10" rowIndexVar="rowIndex">
    <p:column>
        <h:outputText value="#{row.subCatId}"/>
    </p:column>

    <p:column>
        <p:commandLink process="@this">
            <h:outputText value="#{row.subCatName}"/>
            <f:setPropertyActionListener target="#{testManagedBean.subCatName}" value="#{row.subCatName}"/>
        </p:commandLink>
    </p:column>
</p:dataTable>

下面给出了相关的JSF管理。

@ManagedBean
@RequestScoped
public final class TestManagedBean extends LazyDataModel<SubCategory> implements Serializable
{
    @EJB
    private final TestBeanLocal testService=null;
    private String subCatName;
    private static final long serialVersionUID = 1L;

    public String getSubCatName() {
        return subCatName;
    }

    public void setSubCatName(String subCatName) {
        System.out.println("setSubCatName() called : "+subCatName);
        this.subCatName = subCatName;
    }

    @Override
    public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
    {
        setRowCount(4); //Just an example. Otherwise, fetch from the database.
        return testService.getSubCategoryList(first, pageSize, sortField, sortOrder, filters);
    }
}

现在,当点击<p:commandLink>内的<p:dataTable>时,与<f:setProperyActionListener>setSubCatName())关联的相应setter方法被调用。< / p>

仅当托管bean使用更广泛的范围(例如setSubCatName()设置为true)进行修饰时,才会调用@ViewScoped

为什么lazy不适用于请求范围的托管托管bean,<f:setPropertyActionListener>(对于<p:dataTable>也是如此)延迟加载?< / p>

在这种情况下,是否必须指定一个范围大于请求范围的托管bean?

1 个答案:

答案 0 :(得分:1)

是的,它是强制性的,因为底层模型必须是同一个实例。即使它具有相同的值,它也不起作用。

和你一样,我遇到过这种行为:primefaces datatable lazy loading and commandbutton per row