Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
685 views
in Technique[技术] by (71.8m points)

java - How to access Map key in jsf datatable

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.HashMap$Values when trying to display the datatable below.

<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>

Here's the relevant part of my 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 + "";
        }

    }
}

How can i access the key value from my properties map?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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!)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...