jsf中ArrayList的奇怪行为

时间:2013-07-11 05:32:19

标签: jsf primefaces arraylist

我正在使用jsf开发Web应用程序。我尝试使用ArrayList“materialsSummaryBean.electrolyserMaterialLotList”中的索引为0的元素显示属性“brandName”,并使用“c:forEach”显示所有元素的此属性。

            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            #{materialsSummaryBean.electrolyserMaterialLotList.get(0).material.material.brandName}
            <br/>
            <hr/>

            <c:forEach
                    items="#{materialsSummaryBean.electrolyserMaterialLotList}"
                    var="item" varStatus="loop">
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        #{item.material.material.brandName}
                        <br/>
                        <hr/>
                </c:forEach>

结果我绝对随机化了索引为0的元素的值列表:

ШБ-5 
ЗШБ класс 4 
МШ-39 
ЗШБ класс 4 
МШ-39 
КПД-400И 
ШБ-5 
МШ-39 

列表中的所有元素:

                        ЗШБ класс 4
                        ЗШБ класс 4
                        КПД-400И
                        ЗШБ класс 4
                        ШБ-5
                        МШ-39
                        ЗШБ класс 4

                        <hr />

                        ЗШБ класс 4
                        ЗШБ класс 4
                        МШ-39
                        КПД-400И
                        ЗШБ класс 4
                        ШБ-5
                        МШ-39

                        <hr />

                        МШ-39
                        КПД-400И
                        ЗШБ класс 4
                        КПД-400И
                        ЗШБ класс 4
                        ШБ-5
                        МШ-39

                        <hr />

                        ШБ-5
                        ЗШБ класс 4
                        МШ-39
                        МШ-39
                        ШБ-5
                        КПД-400И
                        МШ-39

                        <hr />

我不知道它是什么,问题是什么。

MaterialsSummaryBean方法“getElectrolyserMaterialLotList”:

public List<MaterialLotSummaryData> getElectrolyserMaterialLotList() {
    MaterialLotFilter materialLotFilter = new MaterialLotFilter();
    materialLotFilter.setElectrolyserId(id);
    return materialService.getMaterialLotByElectrolyserId(materialLotFilter);
}

和“getMaterialLotByElectrolyserId”方法:

public List<MaterialLotSummaryData> getMaterialLotByElectrolyserId(MaterialLotFilter materialLotFilter) {
    Long electrolyserId = materialLotFilter.getElectrolyserId();
    if (electrolyserId == null) {
        throw new IllegalArgumentException("Electrolyser id cannot be null.");
    }

    TypedQuery<Unit> queryUnit = em.createQuery(
            "select u from Unit u where u.electrolyser.id = :selectedElectrolyser", 
            Unit.class)
            .setParameter("selectedElectrolyser", electrolyserId);


    List<Unit> unitList = queryUnit.getResultList();
    HashSet<MaterialLot> materialLotList = new HashSet<MaterialLot>();

    HashMap<Long, Double> materialLotUsageMap = new HashMap<Long, Double>();

    for (Unit unit : unitList) {
        MaterialLot currentMaterialLot = unit.getMaterialLot();

        if (currentMaterialLot == null) continue;

        materialLotList.add(currentMaterialLot);


        Double usage = unit.getActualMaterial().getAmount();
        if (!materialLotUsageMap.containsKey(currentMaterialLot.getId())) {
            materialLotUsageMap.put(currentMaterialLot.getId(), (usage == null) ? 0 : usage);
        } else {
            if(usage == null) continue;
            Double currentUsage = materialLotUsageMap.get(currentMaterialLot.getId());
            materialLotUsageMap.put(currentMaterialLot.getId(), currentUsage + usage);
        }
    }

    List<MaterialLotData> result = mapper.createList(materialLotList, MaterialLotData.class);

    MaterialLotSummaryData summaryData;
    ArrayList<MaterialLotSummaryData> summaries = new ArrayList<MaterialLotSummaryData>();
    for (MaterialLotData materialLot : result) {
        if (materialLotUsageMap.containsKey(materialLot.getId())) {
            summaryData = new MaterialLotSummaryData();
            summaryData.setId(materialLot.getId());
            summaryData.setAcceptanceDate(materialLot.getAcceptanceDate());
            summaryData.setLotNumber(materialLot.getLotNumber());
            summaryData.setMaterial(materialLot.getActualMaterial());
            summaryData.setNumber(materialLot.getNumber());
            summaryData.setManufacturer(materialLot.getManufacturer());

            Double usage = materialLotUsageMap.get(materialLot.getId());
            summaryData.setUsage(usage);
            summaries.add(summaryData);

            materialLotUsageMap.remove(materialLot.getId());
        }
    }

    return summaries;
}

1 个答案:

答案 0 :(得分:1)

服务方法称为electrolyserMaterialLotList的每次访问,并且通过访问数据库并再次执行相同的操作来准备列表,即使是单项访问也是如此。你应该避免这样做。基本上,您可以在getter方法中使用null check。

public List<MaterialLotSummaryData> getElectrolyserMaterialLotList() {
    if(electrolyserMaterialLotList == null)
    {
          MaterialLotFilter materialLotFilter = new MaterialLotFilter();
          materialLotFilter.setElectrolyserId(id);
         electrolyserMaterialLotList  = materialService.getMaterialLotByElectrolyserId(materialLotFilter);
    }
     return electrolyserMaterialLotList;
}