使用< ui:repeat>< h:inputText>在List< String>上不更新模型值
这是场景(简化):
有一个具有成员和适当的getter/setter的bean(称为mrBean
):
There is a bean (call it mrBean
) with a member and the appropriate getters/setters:
private List<String> rootContext;
public void addContextItem() {
rootContext.add("");
}
JSF代码:
<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>
问题是,当单击添加"按钮时,不会执行在<h:inputText/>
中输入的表示stringList
中的字符串的值.
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.
实际上,从不调用mrBean.stringList
设置器(setStringList(List<String> stringList)
).
Actually, the mrBean.stringList
setter (setStringList(List<String> stringList)
) is never called.
知道为什么吗?
一些信息- 我在Tomcat 6上使用MyFaces JSF 2.0.
Some info - I'm using MyFaces JSF 2.0 on Tomcat 6.
String
类是不可变的,没有值的设置方法.吸气剂基本上是Object#toString()
方法.
The String
class is immutable and doesn't have a setter for the value. The getter is basically the Object#toString()
method.
您需要直接在List
上获取/设置值.您可以通过<ui:repeat varStatus>
可用的列表索引来做到这一点.
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>
您也不需要stringList
的二传手. EL将通过List#get(index)
获取项目,并通过List#add(index,item)
设置项目.
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)
.