How to access Map key in jsf datatable

前端 未结 4 1688
天命终不由人
天命终不由人 2020-12-21 07:30

I\'m getting the error javax.el.PropertyNotFoundException: /member/apps/cms/edit.xhtml @228,49 value=\"#{props.key}\": Property \'key\' not found on type java.util.Has

相关标签:
4条回答
  • 2020-12-21 07:49

    The dataTable must receive a Collection (for example, a List). Map doesn't implement the Collection interface. You should convert your map into a list and modify it, then convert the list back into your map. Here's a good link showing how to do it:

    • Display HashMap Content in JSF Page

    Of course, I won't recommend you to add logic in the getter/setter, instead use 2 different attributes and keep the getter and setter the cleanest possible way.

    0 讨论(0)
  • 2020-12-21 07:56

    Your properties class needs to implement the map interface here: http://docs.oracle.com/javase/6/docs/api/java/util/Map.html

    The better question is though, why are you creating your own class? Java offers a Properties class in the SDK: http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html

    EDIT: Update my answer per request of OP

    Change the following line, then fix the compile errors in Eclipse:

    public static class Properties{
    

    to:

    public static class Properties implements Map<String, String> {
    

    EDIT #2: Actually my answer is probably wrong. The OP didn't post his entire xhtml file, and I made an assumption that was incorrect.

    0 讨论(0)
  • 2020-12-21 07:57

    In the JSF code, you are trying to access the key property of the thing returned by getProperties() method, which is the entire collection. You need to iterate through the props variable.

    0 讨论(0)
  • 2020-12-21 08:00

    Until the upcoming JSF 2.2, the <h:dataTable>/<p:dataTable> doesn't support Collection<E>. It only supports among others List<E>.

    You need to replace

    public Collection<Properties> getProperties() throws Exception{
        return properties.values();
    }
    

    by

    private List<Properties> propertiesAsList;
    
    public List<Properties> getProperties() throws Exception{
        return propertiesAsList;
    }
    

    and somewhere directly after map's initialization do this

    propertiesAsList = new ArrayList<Properties>(properties.values());
    

    (note: don't do it inside the getter!)

    0 讨论(0)
提交回复
热议问题