使用< h:dataTable>< h:inputText>在列表< String>不更新模型值
我有以下数据表:
<h:dataTable var="row" value="#{myBean.listOfStrings}">
<h:column>
<h:inputText value="#{row}" />
</h:column>
</h:dataTable>
与列表< String>
:
private List<String> listOfStrings = new ArrayList<String>();
public List<String> getListOfStrings() {
return listOfStrings;
}
public void setListOfStrings(List<String> listOfStrings) {
this.listOfStrings = listOfStrings;
}
当我在字段中输入一个值并保存表单,它不通过在列表中的字段的值,它是设置 null
,我在这里做错什么?
When I enter a value in the field and save the form it is not passing the value to the field in the list, it is setting null
, what am I doing wrong here?
String
类是不可变的。它没有实例值的setter。 getter在这个构造中基本上是由EL隐式调用的 Object#toString()
方法,它巧妙地返回字符串值本身。
The String
class is immutable. It doesn't have a setter for the instance value. The getter is in this construct basically the Object#toString()
method as implicitly called by EL, which coincidentally returns the string value itself.
您需要将更改的值设置为新的列表项。您可以通过列表中的大括号进行此操作,您可以通过列表索引:#{myBean.listOfStrings [index]}
。
You need to set the changed value as a new list item instead. You can do this via the brace notation on the list whereby you pass the list index: #{myBean.listOfStrings[index]}
.
所以,这应该做,使用 UIData#getRowIndex()
作为列表索引:
So, this should do, making use of UIData#getRowIndex()
as list index:
<h:dataTable binding="#{table}" value="#{myBean.listOfStrings}" var="row">
<h:column>
<h:inputText value="#{myBean.listOfStrings[table.rowIndex]}" />
</h:column>
</h:dataTable>
(注意:绑定的值表达式
是原样的!不要绑定到bean属性)
(note: the value expression of binding
is as-is! don't bind it to a bean property)
- Using <ui:repeat><h:inputText> on a List<String> doesn't update model values
- What is component binding in JSF? When it is preferred to be used?