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
2.1k views
in Technique[技术] by (71.8m points)

jsf - How to set a Map value in h:inputText

I'm struggling to implement a fairly trivial functionality with JSF which involves dynamically displaying the content of a nested map on a page and editing capabilities for its values. But it has turned out that the MappedValueExpression$Entry that you get when iterating over a map with c:forEach is not writable!

<c:forEach items='#{inflectionBean.word.inflectionalForms}' var="number" >
    <p:fieldset legend="#{number.key}">
        <c:forEach items="#{number.value}" var="case" >
            <p:panel header="#{case.key}">
                <h:inputText value="#{case.value}" />
            </p:panel>
        </c:forEach>
    </p:fieldset>
</c:forEach>

When I am trying to submit the above form I'm getting:

javax.el.PropertyNotWritableException: /inflection.xhtml @39,56 value="#{case.value}": The class 'com.sun.faces.facelets.tag.jstl.core.MappedValueExpression$Entry' does not have a writable property 'value'.

I wonder if there are reasonable workarounds or if I am approaching the problem in a wrong way. Thanks!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Basically, what your code is attempting is invoking Map.Entry#setValue(value). This is indeed not possible in EL. Instead, you should be referencing the map value directly on the map itself by key, so that EL can do Map#put(key, value).

<c:forEach items="#{number.value}" var="case">
    ...
    <h:inputText value="#{number.value[case.key]}" />

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

...