使用 <ui:repeat><h:inputText>在列表<字符串>上不更新模型值 [英] Using <ui:repeat><h:inputText> on a List<String> doesn't update model values
问题描述
这是场景(简化):
有一个 bean(称之为 mrBean
),它有一个成员和适当的 getter/setter:
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
setter (setStringList(List
) 永远不会被调用.
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
类是不可变的,并且没有值的 setter.getter 基本上是 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
上获取/设置值.您可以通过
提供的列表索引来实现.
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
的 setter.EL 将通过 List#get(index)
获取 item 并通过 List#add(index,item)
设置 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)
.
这篇关于使用 <ui:repeat><h:inputText>在列表<字符串>上不更新模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!