【问题标题】:use an arrayList as a source for a repeat control in XPages使用 arrayList 作为 XPages 中重复控件的源
【发布时间】:2015-09-10 16:51:39
【问题描述】:

我开发了一个名为 payObj 的 java Bean,它被定义为 HashMap<String, PaymentItem>,其中 PaymentItem 是一个类,它定义了与每个单独的支付相关的许多字段。 payObj 最初是从一些相关的 Notes 文档中填充的。所有这些都很好。我在 payObj.allItems() 中有一个方法,它返回一个 PaymentItem(s) 的 ArrayList——也许有更好的方法来获取它们,但现在这个方法有效:

public ArrayList<PaymentItem> allItems(){
    ArrayList<PaymentItem> rtn = new ArrayList<PaymentItem>();
    try{
        for (Integer n = 1; n <= internalMap.size(); n++) {
            String thisKey = n.toString();
            PaymentItem pItem = this.internalMap.get((thisKey ).toString());
            if (debug) System.out.println("Copy item key = " + thisKey );
            rtn.add((n - 1),pItem);
        }
        if (debug) System.out.println("Return allItems " + rtn.toString());
        return rtn;
    }catch(Exception e){
        rtn = null;
        System.out.println("Error in PaymentMap allItems " + e.toString());
        return rtn;
    }
}

编辑 --- 我更改了 allItems 并添加了一些额外的输出:

public ArrayList<PaymentItem> allItems(){
        ArrayList<PaymentItem> rtn = new ArrayList<PaymentItem>();
        try{
            for (Integer n = 1; n <= internalMap.size(); n++) {
                String thisKey = n.toString();
                PaymentItem pItem = new PaymentItem();
                pItem =  this.internalMap.get((thisKey ).toString());
                if (debug) System.out.println("Copy item key = " + pItem.getExpPayDate().toString() );
                rtn.add((n - 1),pItem);
            }
            for (Integer n = 0; n< rtn.size(); n++){
                if (debug) System.out.println("dates from ArrayList = " + rtn.get(n).getExpPayDate().toString());
            }
            if (debug) System.out.println("Return allItems " + rtn.toString());
            return rtn;
        }catch(Exception e){
            rtn = null;
            System.out.println("Error in PaymentMap allItems " + e.toString());
            return rtn;
        }
    }

并在日志中获取此输出:

10/09/2015 01:21:04 PM  HTTP JVM: Copy item key = Thu Sep 24 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: Copy item key = Sat Sep 05 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: Copy item key = Fri Aug 28 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: dates from ArrayList = Thu Sep 24 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: dates from ArrayList = Sat Sep 05 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: dates from ArrayList = Fri Aug 28 12:00:00 MDT 2015
10/09/2015 01:21:04 PM  HTTP JVM: Return allItems [ca.wfsystems.core.PaymentItem@39393939, ca.wfsystems.core.PaymentItem@4ef64ef6, ca.wfsystems.core.PaymentItem@51ca51ca]

所以看起来 ArrayList 填充正确。

结束编辑 ------

然后我使用 payObj.allItems() 作为重复控件的源:

<xp:repeat id="repeat1" rows="30" var="pItem"
    indexVar="rIndex" value="#{javascript:payObj.allItems()}">
    <xp:text escape="true" id="computedField5"
            value="#{javascript:pItem.expPayDate}">
    <xp:this.converter>
        <xp:convertDateTime type="date"></xp:convertDateTime>
    </xp:this.converter>
    </xp:text>
    <xp:br></xp:br>
</xp:repeat>

当重复调用 payObj.allItems() 我在日志中得到这个打印输出:

10/09/2015 10:01:32 AM  HTTP JVM: Copy item key = 1
10/09/2015 10:01:32 AM  HTTP JVM: Copy item key = 2
10/09/2015 10:01:32 AM  HTTP JVM: Copy item key = 3
10/09/2015 10:01:32 AM  HTTP JVM: Return allItems [ca.wfsystems.core.PaymentItem@4e664e66, ca.wfsystems.core.PaymentItem@52aa52aa, ca.wfsystems.core.PaymentItem@543a543a]

这看起来是正确的,并标识了 3 个 PaymentItem;

getter 和 setter 是:

public Date getExpPayDate() {
        return expPayDate;
    }

public void setExpPayDate(Date expPayDate) {
        this.expPayDate = expPayDate;
    }

但是,虽然重复显示 3 行(应该如此),但它会显示最后一个项目的日期 3 次,而不是每个 pItem 中的不同日期:

展示

2015 年 8 月 28 日
2015 年 8 月 28 日
2015 年 8 月 28 日

而不是

2015 年 9 月 15 日
2015 年 9 月 4 日
2015 年 8 月 28 日

【问题讨论】:

  • 如果向 getExtPayDate 方法添加 sysout,它是否会按预期打印出不同的值?可能是,虽然它们是三个不同的对象,但是expPayDate的设置不正确。
  • 顺便说一句,有一种更快的方法可以将 Map 作为 ArrayList。 return new ArrayList(internalMap,values()) 不过这个日期很奇怪,所以我同意 Jesse,试试 cyst
  • 我在 allItems 中添加了一些 println 以在加载时打印日期,然后循环遍历 arrayList 并且 ArrayList 看起来加载正确
  • @BillF 顺便说一句:既然你提到它是一个 bean,为了遵守 bean 约定,你会希望你的属性被称为 allItems,带有一个公共 getter,例如-getAllItems()(而不是一个名为allItems()的方法)。在您的绑定中,而不是 xp:repeat 中的 value="#{javascript:payObj.allItems()}",然后您可以通过 EL 使用 value="#{payObj.allItems}" 绑定它。这是更被接受的 Java bean 约定,尽管您正在做的工作。
  • 你的代码非常适合我。

标签: java xpages repeat


【解决方案1】:

顺便说一下,查看 entrySet 和 keySet 以获得更好的方法来遍历 Map。我假设您只想获取 Map 中的所有键值?它们返回一组条目或键,您可以循环访问它们。

for (Map.Entry<String, String> entry : map.entrySet())
{
    System.out.println(entry.getKey() + "/" + entry.getValue());
}

【讨论】:

    【解决方案2】:

    请参阅我上面的编辑。不确定是什么导致了问题,但似乎已解决。它现在如图所示工作

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多