0

例子 :

<p:dataGrid var="dRow" value="#{testBean}" lazy="true" rows="1" columns="1">
    <p:carousel var="cRow" value="#{dRow.subCategories}" headerText="#{dRow.catName}" itemStyle="width : 300px">
        <!--Show images but that part is excluded for brevity.-->
        <p:commandLink process="@this" value="#{cRow.subCatName}" actionListener="#{testBean.action(cRow)}"/>
    </p:carousel>
</p:dataGrid>

托管bean:

@Named
@RequestScoped
public class TestBean extends LazyDataModel<Category> {

    @Inject
    private DataStore dataStore;
    private List<Category> categories;

    public TestBean() {}

    @PostConstruct
    private void init() {
        categories = dataStore.getCategories();
    }

    @Override
    public List<Category> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {
        List<Category> list = dataStore.getCategories();
        setRowCount(list.size());
        return list;
    }

    public List<Category> getCategories() {
        return categories;
    }

    public void action(SubCategory subCategory) {
        System.out.println("action() invoked : " + subCategory.getSubCatName());
    }
}

豆子用@RequestScoped. DataStore是一个应用程序范围的 bean,在其中List<Category>创建和存储 。

给定的命令链接不会触发action()托管 bean 中的操作侦听器方法 ( ),除非 bean 被装饰@ViewScoped(或更宽)或惰性数据模型被删除。

因此,改变,

<p:dataGrid var="dRow" value="#{testBean}" .../>

<p:dataGrid var="dRow" value="#{testBean.categories}" .../>

将起作用,<p:commandLink>即将调用给定的动作侦听器(这里不包括惰性数据模型。给定<p:dataGrid>的直接由List<Category>支持 bean 的 a 填充)。

另一种选择是使用范围更广的 bean,如前面所述的视图范围 bean。

是否可以在给定情况下使用请求范围的 bean 和惰性数据模型,以便这些组件中的命令组件可以工作?我有很多这样的视图范围 bean,只是因为<p:commandLink>s 在 a中<p:carousel>被 a 包裹,<p:dataGrid>否则它可能被简单地声明为请求范围。


如果需要维护列表和域模型类的应用程序范围的 bean。

应用程序范围的bean:

@Named
@ApplicationScoped
public class DataStore {

    private List<Category> categories;

    public DataStore() {}

    @PostConstruct
    private void init() {
        categories = new ArrayList<>();

        Category category = new Category();
        category.setCatId(1L);
        category.setCatName("Animals");
        categories.add(category);

        List<SubCategory> subCategories = category.getSubCategories();

        SubCategory subCategory = new SubCategory();
        subCategory.setSubCatId(1L);
        subCategory.setSubCatName("Aquatic");
        subCategory.setCategory(category);
        subCategories.add(subCategory);

        subCategory = new SubCategory();
        subCategory.setSubCatId(2L);
        subCategory.setSubCatName("Aerial");
        subCategory.setCategory(category);
        subCategories.add(subCategory);

        subCategory = new SubCategory();
        subCategory.setSubCatId(3L);
        subCategory.setSubCatName("Mammals");
        subCategory.setCategory(category);
        subCategories.add(subCategory);
    }

    public List<Category> getCategories() {
        return categories;
    }
}

域类Category

public class Category implements Serializable {

    private Long catId;
    private String catName;
    private List<SubCategory> subCategories = new ArrayList<>();
    private static final long serialVersionUID = 1L;

    public Category() {}

    public Long getCatId() {
        return catId;
    }

    public void setCatId(Long catId) {
        this.catId = catId;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public List<SubCategory> getSubCategories() {
        return subCategories;
    }

    public void setSubCategories(List<SubCategory> subCategories) {
        this.subCategories = subCategories;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + Objects.hashCode(getCatId());
        return hash;
    }

    @Override
    public boolean equals(Object that) {
        if (!(that instanceof Category)) {
            return false;
        }

        return this == that || Objects.equals(getCatId(), ((Category) that).getCatId());
    }

    @Override
    public String toString() {
        return String.format("%s[catId=%d]", getClass().getCanonicalName(), getCatId());
    }
}

域类SubCategory

public class SubCategory implements Serializable {

    private Long subCatId;
    private String subCatName;
    private Category category;
    private static final long serialVersionUID = 1L;

    public SubCategory() {}

    public Long getSubCatId() {
        return subCatId;
    }

    public void setSubCatId(Long subCatId) {
        this.subCatId = subCatId;
    }

    public String getSubCatName() {
        return subCatName;
    }

    public void setSubCatName(String subCatName) {
        this.subCatName = subCatName;
    }

    public Category getCategory() {
        return category;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 47 * hash + Objects.hashCode(getSubCatId());
        return hash;
    }

    @Override
    public boolean equals(Object that) {
        if (!(that instanceof SubCategory)) {
            return false;
        }

        return this == that || Objects.equals(getSubCatId(), ((SubCategory) that).getSubCatId());
    }

    @Override
    public String toString() {
        return String.format("%s[subCatId=%d]", getClass().getCanonicalName(), getSubCatId());
    }
}
4

0 回答 0