在素数面数据表中显示键和值Map



在我的Java EE 6/JSF 2项目中,我在Employee实体中有一个Map属性advantages

@ElementCollection
private Map<String, Float> advantages;

键表示优势名称,值表示与优势名称相关联的成本。这被映射到具有三列Employee_IdAdvantagesAdvantages_Key的表。我需要在显示List<Employee><p:dataTable>中显示所有地图条目。我怎样才能做到这一点?

如果您的环境支持EL 2.2(Java EE 6支持),并且下例中的#{employee}来自<p:dataTable var>,那么这应该执行

<ui:repeat value="#{employee.advantages.entrySet().toArray()}" var="entry">
    Name: #{entry.key}, Cost: #{entry.value}
</ui:repeat>

这对我有效,这是我在beanMap:中的映射

<p:dataTable id="dtbAddedRoles" value="#{controllerUserCreationMain.mapUtil.entrySet().toArray()}"
                                var="roleAdded">
                                <p:column headerText="Role">
                                    <p:outputLabel value="#{roleAdded.key.roleName}" />
                                </p:column>
                            </p:dataTable>

我知道答案太晚了,但也许对其他人来说会有用。

假设您的haspmap定义如下:

HashMap<String, BigDecimal> myHashMap;
public List<Entry<String, BigDecimal>> getMyHashMapEntryList() {
return new ArrayList(myHashMap.entrySet());
}

在您的UI中:

<p:dataTable value="#{yourBean.myHashMapEntryList}" var="myEntry">
    in here reference as: #{myEntry.key} #{myEntry.value}
</p:dataTable>

最新更新