如何在jsf datatable中访问Map键

Cat*_*ish 3 java jsf primefaces jsf-2

javax.el.PropertyNotFoundException: /member/apps/cms/edit.xhtml @228,49 value="#{props.key}": Property 'key' not found on type java.util.HashMap$Values在尝试显示下面的数据表时遇到错误.

<p:dataTable id="properties" var="props" value="#{contentEditorBacking.properties}" editable="true">

    <p:column headerText="Property">
        <p:cellEditor>
            <f:facet name="output"> 
                <h:outputText value="#{props.key}" />
            </f:facet>
            <f:facet name="input">
                <h:inputText value="#{props.key}" />
            </f:facet>
        </p:cellEditor>
    </p:column>
    <p:column headerText="Value">
        <p:cellEditor>
                        <f:facet name="output"> 
                <h:outputText value="#{props.value}" />
            </f:facet>
            <f:facet name="input">
                <h:inputText value="#{props.value}" />
            </f:facet>
        </p:cellEditor>
    </p:column>

    <p:column headerText="Edit">
        <p:rowEditor />
        <!-- Need to put an update on here yet -->
        <p:commandLink styleClass="ui-icon ui-icon-trash" id="deleteProperty" actionListener="#{contentEditorBacking.deleteProperty}">
                 <f:attribute name="key" value="#{props.key}" />
             </p:commandLink>
    </p:column>
</p:dataTable>
Run Code Online (Sandbox Code Playgroud)

这是我的contentEditorBacking的相关部分:

@ManagedBean
@ViewScoped
public class ContentEditorBacking {
    private Map<String, Properties> properties = new LinkedHashMap<String, Properties>();

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

    public static class Properties{

        private String key;
        private String value;

        public Properties(String key, String value) {
            super();
            this.key = key;
            this.value = value;
        }

        public String getKey() {
            return key;
        }
        public void setKey(String key) {
            this.key = key;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
        @Override
        public String toString() {
            return "key=" + key + ", value=" + value + "";
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

如何从属性映射中访问键值?

Bal*_*usC 6

直到即将推出的JSF 2.2,<h:dataTable>/ <p:dataTable>不支持Collection<E>.它只支持其他List<E>.

你需要更换

public Collection<Properties> getProperties() throws Exception{
    return properties.values();
}
Run Code Online (Sandbox Code Playgroud)

通过

private List<Properties> propertiesAsList;

public List<Properties> getProperties() throws Exception{
    return propertiesAsList;
}
Run Code Online (Sandbox Code Playgroud)

在地图初始化之后直接执行此操作

propertiesAsList = new ArrayList<Properties>(properties.values());
Run Code Online (Sandbox Code Playgroud)

(注意:不要在吸气剂内进行!)