使用 <h:dataTable><h:inputText>在列表<字符串>上不更新模型值 [英] Using <h:dataTable><h:inputText> on a List<String> doesn't update model values
问题描述
我有以下数据表:
<h:dataTable var="row" value="#{myBean.listOfStrings}">
<h:column>
<h:inputText value="#{row}" />
</h:column>
</h:dataTable>
绑定到List
:
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
类是不可变的.它没有实例值的设置器.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>
(注意:binding
的值表达式是原样的!不要将它绑定到 bean 属性)
(note: the value expression of binding
is as-is! don't bind it to a bean property)
这篇关于使用 <h:dataTable><h:inputText>在列表<字符串>上不更新模型值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!