0

我正在使用 jsf 开发网络应用程序。我尝试在 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;
}
4

1 回答 1

1

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

public List<MaterialLotSummaryData> getElectrolyserMaterialLotList() {
    if(electrolyserMaterialLotList == null)
    {
          MaterialLotFilter materialLotFilter = new MaterialLotFilter();
          materialLotFilter.setElectrolyserId(id);
         electrolyserMaterialLotList  = materialService.getMaterialLotByElectrolyserId(materialLotFilter);
    }
     return electrolyserMaterialLotList;
}
于 2013-07-11T06:42:12.937 回答