简体   繁体   English

Tapestry通过tml文件中的嵌套映射循环

[英]Tapestry loop through nested map in tml file

I have following map 我有以下地图

Map<String, Map<String, Long>>

This map is accessible through following method in java file. 可以通过java文件中的以下方法访问此映射。

public List<Entry<String, List<Entry<String, Long>>>> getByEventTypeSorted() {
    List<Entry<String, List<Entry<String, Long>>>> ret = new ArrayList<Entry<String, List<Entry<String, Long>>>>();
    ret.addAll((Collection<? extends Entry<String, List<Entry<String, Long>>>>) byEventType.entrySet());
    return ret;
}

Thus I am converting Map<String, Map<String, Long>> into List<Entry<String, List<Entry<String, Long>>>> and returning it. 因此,我将Map<String, Map<String, Long>>转换为List<Entry<String, List<Entry<String, Long>>>>并将其返回。

Now, in my tml file I am trying to loop through it like this. 现在,在我的tml文件中,我试图像这样循环遍历它。

<tr t:type="Loop" t:source="summarizer.byEventTypeSorted" t:value="entry">
    <td style="border: 1px solid #EEEEEE; padding: 3px">${entry.key}</td>
    <td style="border: 1px solid #EEEEEE; padding: 3px" t:type="Loop" t:source="${entry.value}" t:value="entry2"> 
        ${entry2.key}
    </td>
</tr>

Corresponding java file has following properties. 相应的java文件具有以下属性。

@Property
private Entry<String,Long> entry;
@Property
private Entry<String,Long> entry2;

When I run the above code. 当我运行上面的代码。 Following exception is generated. 产生以下异常。

Failure writing parameter 'value' of component ConceptSummaries:loop_1: Could not find a coercion from type java.lang.String to type java.util.Map$Entry.

If I change the type of property entry2 to String in java file like below..... 如果我在java文件中将属性entry2的类型更改为String,如下所示.....

@Property
private Entry<String,Long> entry;
@Property
private String entry2;

And loop in tml file like below. 并在tml文件中循环,如下所示。

<tr t:type="Loop" t:source="summarizer.byEventTypeSorted" t:value="entry">
    <td style="border: 1px solid #EEEEEE; padding: 3px">${entry.key}</td>
    <td style="border: 1px solid #EEEEEE; padding: 3px" t:type="Loop" t:source="${entry.value}" t:value="entry2"> 
        ${entry2}
    </td>
</tr>

Then I get the whole list rendered as a single string. 然后,我将整个列表呈现为单个字符串。

This means that tapestry is converting the List<Entry<String, Long>> into a String. 这意味着挂毯正在将List<Entry<String, Long>>转换为字符串。 I do not want that to happen as I want to loop through the list and access individual entries. 我不希望发生这种情况,因为我想遍历列表并访问各个条目。

How to loop through these nested maps? 如何遍历这些嵌套地图? And is there any way to retain the type of inner list and prevent tapestry from converting it into a string? 还有什么方法可以保留内部列表的类型并防止挂毯将其转换为字符串?

Update: I was able to solve this problem using Lance Java's suggestions. 更新:我能够使用Lance Java的建议解决此问题。 Following are the details. 以下是详细信息。

code in tml file. tml文件中的代码。

<tr t:type="Loop" t:source="summarizer.byEventTypeSorted.entrySet()" t:value="entry">
    <td style="border: 1px solid #EEEEEE; padding: 3px">${entry.key}</td>
    <td style="border: 1px solid #EEEEEE; padding: 3px" t:type="Loop" t:source="KeySetForEntryValue" t:value="entry2"> 
        ${entry2.key}
    </td>
</tr>

I have a getter method for "KeySetForEntryValue" and Map<String, Map<String, Long>> in corresponding java class. 我在对应的Java类中有一个用于"KeySetForEntryValue"Map<String, Map<String, Long>>的getter方法。

public Set<Entry<String, Long>> getKeySetForEntryValue(){
    return entry.getValue().entrySet();
}

public Map<String, Map<String, Long>> getByEventTypeSorted() {
    return byEventType;
}

And following are the properties in the same java class. 以下是同一java类中的属性。

@Property
private Entry<String, Map<String, Long>> entry;
@Property
private Entry<String, Long> entry2;

Your problem is with t:source="${entry.value}" 您的问题出在t:source="${entry.value}"

Using ${...} in a template attribute causes the value to be coerced to a string 在模板属性中使用${...}会导致将值强制转换为字符串

Try t:source="entry.value" instead. 尝试改用t:source="entry.value"

Here's how I'd do it: 这是我的处理方式:

@Property
private Map<String, Map<String, Long>> byEventType;

@Property
private Entry<String, Map<String, Long>> entry;

@Property
private Entry<String, Long> entry2;

<tr t:type="Loop" t:source="byEventType.entrySet()" t:value="entry">
<td style="border: 1px solid #EEEEEE; padding: 3px">${entry.key}</td>
<td style="border: 1px solid #EEEEEE; padding: 3px" t:type="Loop" t:source="entry.value.entrySet()" t:value="entry2"> 
    ${entry2.key} = ${entry2.value}
</td>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM