使用java.util。地图在h:dataTable

时间:2021-07-27 16:16:26

I need to display Map using <h:dataTable>. My backing bean has a Map property as below:

我需要使用 来显示地图。我的支持bean有如下图属性:

public class Bean {

    private Map<Integer,String> map; // +getter

    @PostConstruct
    public void init() {
        map = new TreeMap<Integer,String>();
        map.put(1,"Sasi");
        map.put(2,"Pushparaju");
        map.put(3,"Venkat Raman");
        map.put(3,"Prabhakaran");
    }

}

Then in JSF page I am trying to bind this Map property to the value attribute of <h:dataTable>.

然后在JSF页面中,我尝试将此映射属性绑定到 的值属性。

 <h:dataTable border="1" value="#{bean.map}" var="map">
    <h:column id="column1">
        <f:facet name="header">
            <h:outputText value="UserId"></h:outputText>
        </f:facet>
        <h:outputText value="#{map.getKey}"></h:outputText>
    </h:column>
    <h:column id="column2">
        <f:facet name="header">
            <h:outputText value="Email Id"></h:outputText>
        </f:facet>
        <h:outputText value="#{map.getValue}"></h:outputText>
    </h:column>
</h:dataTable>

It is giving en error that getKey and getValue is not present. I can understand that this is not the correct way to do it. How can I present a Map using <h:dataTable>?

它给出的错误是getKey和getValue不存在。我能理解这不是正确的方法。如何使用 提供地图?

3 个解决方案

#1


25  

Until upcoming JSF 2.3, UIData components such as <h:dataTable>, <p:dataTable>, etc and <ui:repeat> does not support iterating over a Map. This is only supported in <c:forEach>.

在即将发布的JSF 2.3之前,UIData组件,如 , etc和 不支持在地图上迭代。这只在 中得到支持。

One way is to convert the map entries to an array (alone entrySet() won't work as UIData also doesn't support Set until upcoming JSF 2.3).

一种方法是将映射条目转换为一个数组(单独的entrySet()不会起作用,因为UIData也不支持设置到即将到来的JSF 2.3)。

<h:dataTable value="#{bean.map.entrySet().toArray()}" var="entry">
    <h:column>#{entry.key}</h:column>
    <h:column>#{entry.value}</h:column>
</h:dataTable>

Another way is to wrap the map's entry set in a collection which the <h:dataTable> can iterate over, such as an ArrayList.

另一种方法是将map的条目集封装在一个集合中,该集合中 可以迭代,如ArrayList。

public class Bean {

    private Map<Integer, String> map;
    private List<Entry<Integer, String>> entries; // +getter (no setter necessary)

    @PostConstruct
    public void init() {
        map = new TreeMap<>();
        map.put(1, "Sasi");
        map.put(2, "Pushparaju");
        map.put(3, "Venkat Raman");
        map.put(4, "Prabhakaran");
        entries = new ArrayList<>(map.entrySet());
    }

    // ...
}
<h:dataTable value="#{bean.entries}" var="entry">
    <h:column>#{entry.key}</h:column>
    <h:column>#{entry.value}</h:column>
</h:dataTable>

However, more clean, self documenting and reusable is to use a List<User> instead wherein the User class has the necessary properties id and name.

但是,更干净、更清晰、更可重用的是使用List ,其中User类具有必需的属性id和名称。

public class Bean {

    private List<User> users; // +getter (no setter necessary)

    @PostConstruct
    public void init() {
        users = new ArrayList<>();
        users.add(new User(1, "Sasi"));
        users.add(new User(2, "Pushparaju"));
        users.add(new User(3, "Venkat Raman"));
        users.add(new User(4, "Prabhakaran"));
    }

    // ...
}
<h:dataTable value="#{bean.users}" var="user">
    <h:column>#{user.id}</h:column>
    <h:column>#{user.name}</h:column>
</h:dataTable>

#2


4  

You can try this alternative too.

你也可以试试这个方法。

<h:dataTable border="1" value="#{myBean.map.keySet().toArray()}" var="myVar">
    <h:column id="column1">
        <f:facet name="header">
            <h:outputText value="UserId"></h:outputText>
        </f:facet>
            <h:outputText value="#{myVar}"></h:outputText>
    </h:column>
    <h:column id="column2">
        <f:facet name="header">
            <h:outputText value="Email Id"></h:outputText>
        </f:facet>
            <h:outputText value="#{myBean.map.get(myVar)}"></h:outputText>
    </h:column>
</h:dataTable>

#3


3  

Regarding to the last answer from prageeth, you may use entrySet instead of keySet; then you can get rid of myBean.map.get. See this example:

关于prageeth的最后一个答案,您可以使用entrySet而不是keySet;然后你就可以去掉mybean.map。看这个例子:

<h:dataTable border="1" value="#{myBean.map.entrySet().toArray()}" var="map">
<h:column id="column1">
    <f:facet name="header">
        <h:outputText value="UserId"></h:outputText>
    </f:facet>
        <h:outputText value="#{map.key}"></h:outputText>
</h:column>
<h:column id="column2">
    <f:facet name="header">
        <h:outputText value="Email Id"></h:outputText>
    </f:facet>
        <h:outputText value="#{map.value}"></h:outputText>
</h:column>
</h:dataTable>

This works on myfaces 2.2.3 as I've just used it myself.

这在myfaces 2.2.3中起作用,因为我刚刚自己用过。

Annotation: I'd better had commented the last post, but my reputation is not high enough, therefore this is an extra answer.

注释:我最好是评论了最后一篇文章,但是我的声望不够高,所以这是一个额外的答案。

#1


25  

Until upcoming JSF 2.3, UIData components such as <h:dataTable>, <p:dataTable>, etc and <ui:repeat> does not support iterating over a Map. This is only supported in <c:forEach>.

在即将发布的JSF 2.3之前,UIData组件,如 , etc和 不支持在地图上迭代。这只在 中得到支持。

One way is to convert the map entries to an array (alone entrySet() won't work as UIData also doesn't support Set until upcoming JSF 2.3).

一种方法是将映射条目转换为一个数组(单独的entrySet()不会起作用,因为UIData也不支持设置到即将到来的JSF 2.3)。

<h:dataTable value="#{bean.map.entrySet().toArray()}" var="entry">
    <h:column>#{entry.key}</h:column>
    <h:column>#{entry.value}</h:column>
</h:dataTable>

Another way is to wrap the map's entry set in a collection which the <h:dataTable> can iterate over, such as an ArrayList.

另一种方法是将map的条目集封装在一个集合中,该集合中 可以迭代,如ArrayList。

public class Bean {

    private Map<Integer, String> map;
    private List<Entry<Integer, String>> entries; // +getter (no setter necessary)

    @PostConstruct
    public void init() {
        map = new TreeMap<>();
        map.put(1, "Sasi");
        map.put(2, "Pushparaju");
        map.put(3, "Venkat Raman");
        map.put(4, "Prabhakaran");
        entries = new ArrayList<>(map.entrySet());
    }

    // ...
}
<h:dataTable value="#{bean.entries}" var="entry">
    <h:column>#{entry.key}</h:column>
    <h:column>#{entry.value}</h:column>
</h:dataTable>

However, more clean, self documenting and reusable is to use a List<User> instead wherein the User class has the necessary properties id and name.

但是,更干净、更清晰、更可重用的是使用List ,其中User类具有必需的属性id和名称。

public class Bean {

    private List<User> users; // +getter (no setter necessary)

    @PostConstruct
    public void init() {
        users = new ArrayList<>();
        users.add(new User(1, "Sasi"));
        users.add(new User(2, "Pushparaju"));
        users.add(new User(3, "Venkat Raman"));
        users.add(new User(4, "Prabhakaran"));
    }

    // ...
}
<h:dataTable value="#{bean.users}" var="user">
    <h:column>#{user.id}</h:column>
    <h:column>#{user.name}</h:column>
</h:dataTable>

#2


4  

You can try this alternative too.

你也可以试试这个方法。

<h:dataTable border="1" value="#{myBean.map.keySet().toArray()}" var="myVar">
    <h:column id="column1">
        <f:facet name="header">
            <h:outputText value="UserId"></h:outputText>
        </f:facet>
            <h:outputText value="#{myVar}"></h:outputText>
    </h:column>
    <h:column id="column2">
        <f:facet name="header">
            <h:outputText value="Email Id"></h:outputText>
        </f:facet>
            <h:outputText value="#{myBean.map.get(myVar)}"></h:outputText>
    </h:column>
</h:dataTable>

#3


3  

Regarding to the last answer from prageeth, you may use entrySet instead of keySet; then you can get rid of myBean.map.get. See this example:

关于prageeth的最后一个答案,您可以使用entrySet而不是keySet;然后你就可以去掉mybean.map。看这个例子:

<h:dataTable border="1" value="#{myBean.map.entrySet().toArray()}" var="map">
<h:column id="column1">
    <f:facet name="header">
        <h:outputText value="UserId"></h:outputText>
    </f:facet>
        <h:outputText value="#{map.key}"></h:outputText>
</h:column>
<h:column id="column2">
    <f:facet name="header">
        <h:outputText value="Email Id"></h:outputText>
    </f:facet>
        <h:outputText value="#{map.value}"></h:outputText>
</h:column>
</h:dataTable>

This works on myfaces 2.2.3 as I've just used it myself.

这在myfaces 2.2.3中起作用,因为我刚刚自己用过。

Annotation: I'd better had commented the last post, but my reputation is not high enough, therefore this is an extra answer.

注释:我最好是评论了最后一篇文章,但是我的声望不够高,所以这是一个额外的答案。