Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

Using <ui:repeat><h:inputText> on a List<String> doesn't update model values

Here is the scenario (simplified):

There is a bean (call it mrBean) with a member and the appropriate getters/setters:

private List<String> rootContext;

public void addContextItem() {
    rootContext.add("");
}

The JSF code:

<h:form id="a_form">
            <ui:repeat value="#{mrBean.stringList}" var="stringItem">
                    <h:inputText value="#{stringItem}" />
            </ui:repeat>
            <h:commandButton value="Add" action="#{mrBean.addContextItem}">
                <f:ajax render="@form" execute="@form"></f:ajax>
            </h:commandButton>
</h:form>

The problem is, when clicking the "Add" button, the values that were entered in the <h:inputText/> that represent the Strings in the stringList aren't executed.

Actually, the mrBean.stringList setter (setStringList(List<String> stringList)) is never called.

Any idea why?

Some info - I'm using MyFaces JSF 2.0 on Tomcat 6.

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

The String class is immutable and doesn't have a setter for the value. The getter is basically the Object#toString() method.

You need to get/set the value directly on the List instead. You can do that by the list index which is available by <ui:repeat varStatus>.

<ui:repeat value="#{mrBean.stringList}" varStatus="loop">
    <h:inputText value="#{mrBean.stringList[loop.index]}" />
</ui:repeat>

You don't need a setter for the stringList either. EL will get the item by List#get(index) and set the item by List#add(index,item).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...