JSF中的访问器/ getter方法内的业务逻辑

时间:2014-04-01 22:59:54

标签: jsf primefaces getter-setter jsf-2.2 accessor

我在MySQL数据库中有三个表。

  • 类别(从此问题中排除)
  • sub_category
  • 产品

这些表之间的关系是直观的 - 按照它们出现的顺序一对多。


我按照以下方式使用<p:dataGrid>遍历SubCategoryList<SubCategory>列表。

<p:dataGrid var="row" value="#{featuredProductManagedBean}" rows="4" first="0" columns="1" rowIndexVar="rowIndex" paginator="true" paginatorAlwaysVisible="false" pageLinks="10" lazy="true" rowsPerPageTemplate="5,10,15">
    <h:panelGrid columns="1" style="width:100%;">
        <p:carousel value="#{featuredProductManagedBean.getProducts(row.subCatId)}" var="prodRow" numVisible="4" pageLinks="5" headerText="#{row.subCatName}" style="text-align: left;">

            <!--Display product image in <p:graphicImage>-->

        </p:carousel>
        <h:outputLink rendered="#{featuredProductManagedBean.count gt 4}" value="xxx">View More</h:outputLink>
    </h:panelGrid>

    <p:ajax event="page" onstart="PF('blockDataPanelUIWidget').block()" oncomplete="PF('blockDataPanelUIWidget').unblock()"/>
</p:dataGrid>

有一个参数化的getter方法与</p:carousel> featuredProductManagedBean.getProducts(row.subCatId)的value属性相关联。

多次调用该方法会导致多次调用昂贵的业务服务。

托管bean:

@ManagedBean
@ViewScoped
public final class FeaturedProductManagedBean extends LazyDataModel<SubCategory> implements Serializable
{
    @EJB
    private final FeaturedProductBeanLocal service=null;
    private Product selectedProduct;  //Getter and setter.
    private Long count;               //Getter and setter.  
    private static final long serialVersionUID = 1L;

    public FeaturedProductManagedBean() {}

    public List<Product>getProducts(Long subCatId)
    {
        List<Product>products=null;

        if(FacesContext.getCurrentInstance().getCurrentPhaseId().getOrdinal()==6)
        {
            setCount(service.countProducts(subCatId));
            products=service.getProductList(subCatId);
        }

        return products;
    }

    @Override
    public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
    {
        int rowCount = service.rowCount().intValue();
        setRowCount(rowCount);

        if(pageSize<=0)
        {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_FATAL, Utility.getMessage("faces.message.error"), Utility.getMessage("pageSize.error.message"));
            FacesContext.getCurrentInstance().addMessage(null, message);
            return Collections.emptyList();
        }
        else if(first>=pageSize&&rowCount<=Utility.currentPage(first, pageSize)*pageSize-pageSize) {
            first-=pageSize;
        }

        setPageSize(pageSize);
        return service.getList(first, pageSize);
    }
}

这里不能使用@PostConstruct延迟加载。目前我已经进行了条件检查if(FacesContext.getCurrentInstance().getCurrentPhaseId().getOrdinal()==6)以防止多次调用服务方法。这个有条件的检查是否正确执行?它有副作用吗?

This回答维护Map,但为大型数据源维护Map的费用很高。

是否有一种精确的方法可以防止此类业务逻辑被多次执行?

1 个答案:

答案 0 :(得分:1)

我建议将当前加载的subCategory的产品存储在Map中,就在它们由<p:dataGrid>的惰性模型加载之后。地图永远不会增长太多,因为你会在每次调用load方法时清空它:

@ManagedBean
@ViewScoped
public final class FeaturedProductManagedBean extends LazyDataModel<SubCategory> implements Serializable
{
    @EJB
    private final FeaturedProductBeanLocal service=null;
    private Product selectedProduct;  //Getter and setter.
    private Long count;               //Getter and setter.  
    private Map<Long, Product> productsBySubCategory = new HashMap<>(); //Getter only.
    private static final long serialVersionUID = 1L;

    @Override
    public List<SubCategory> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters)
    {
        int rowCount = service.rowCount().intValue();
        setRowCount(rowCount);

        List<SubCategory> subCategories = service.getList(first, pageSize);
        productsBySubCategory.clear();
        for (SubCategory sc : subCategories) 
        {
            productsBySubCategory.put(sc.getSubCatId(), service.getProductList(sc.getSubCatId());
        }
        return subCategories;
    }
}

直接从EL访问Map

<p:carousel value="#{featuredProductManagedBean.productsBySubCategory[row.subCatId]}" />

请注意,您不需要检查页面边界并设置pagesize的所有样板文件。

相关问题