Using <ui:repeat><h:inputText> on a List<String> doesn&#39;t update model values

随声附和 提交于 2019-11-26 01:01:53

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/10780858/using-uirepeathinputtext-on-a-liststring-doesnt-update-model-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!