如何保存h:dataTable的h:inputText值?我的尝试只保存了最后一行的值 [英] How to save h:inputText values of a h:dataTable? My attempt only saves the value of last row
问题描述
我在制作dataTable时遇到麻烦,其中的每一行都有一个inputText和commandLink。单击链接时,仅提交该行的inputText的数据。
I'm having trouble making a dataTable where each row has a inputText and a commandLink. When the link is clicked, only it's row's inputText's data is submitted.
类似这样的事情?
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{bean.value}"/>
</h:column>
<h:column>
<h:commandLink action="#{bean.save}" value="save">
<f:setPropertyActionListener target="#{bean.item}" value="#{item}" />
</h:commandLink>
</h:column>
</h:dataTable>
Bean:
@RequestScoped
public class Bean {
private Item item;
private String value;
现在,它使用的是最后一行的 inputText
填充值
。我包装了另一个 h:form
,但是它弄坏了其他东西,而且我了解到嵌套的 h:form
不是正确的方法是这样
Right now, as it is, it's using the last row's inputText
to fill the value
. I wrapped another h:form
, but it broke other things and I've learned that nested h:form
is not the right way to do it hehe
正确的方法是什么?
谢谢。
推荐答案
您正在将所有HTML输入元素的值绑定到一个相同的bean属性。如果所有这些HTML输入元素都在同一表单内,那么这当然是行不通的。随后,按照与输入在表单中出现的顺序,在完全相同的属性上设置所有值。因此,您最终获得了最后的价值。您想将该表格移动到< h:column>
(移动;因此不要t添加/嵌套另一个)。
You're binding the value of all HTML input elements to one and same bean property. This is of course not going to work if all those HTML input elements are inside the same form. All values are subsequently set on the very same property in the order as the inputs appeared in the form. That's why you end up with the last value. You'd like to move that form to inside the <h:column>
(move; thus don't add/nest another one).
不过,通常的方法是将输入字段绑定到迭代对象上。
The usual approach, however, would be to just bind the input field to the iterated object.
<h:inputText value="#{item.value}"/>
如果确实需要将表格放在桌子旁,另一种方法是使用 Map< K,V>
作为bean属性,其中 K
表示<$ c $之后的对象的唯一标识符的类型c>#{item} 和 V
表示 value
的类型。假设它是 Long
和 String
:
An alternative, if you really need to have your form around the table, is to have a Map<K, V>
as bean property where K
represents the type of the unique identifier of the object behind #{item}
and V
represents the type of value
. Let's assume that it's Long
and String
:
private Map<Long, String> transferredValues = new HashMap<Long, String>();
// +getter (no setter necessary)
with
<h:inputText ... value="#{bean.values[item.id]}" />
这样,您可以通过以下操作方法来获取它:
This way you can get it in the action method as follows:
String value = values.get(item.getId());
如果您碰巧定位到支持EL 2.2的Servlet 3.0容器(Tomcat 7,Glassfish, 3等),那么您也可以只将#{req}
作为方法参数传递而无需< f:setPropertyActionListener>
。
By the way, if you happen to target Servlet 3.0 containers which supports EL 2.2 (Tomcat 7, Glassfish 3, etc), then you can also just pass the #{req}
as a method argument without the need for a <f:setPropertyActionListener>
.
<h:commandLink ... action="#{bean.save(item)}" />
另请参见:
- >如何以及何时加载模型从数据库中获取h:dataTable
- 如何将所选行传递到dataTable内部的commandLink?
- 如何动态添加JSF组件
- How and when should I load the model from database for h:dataTable
- How can I pass selected row to commandLink inside dataTable?
- How to dynamically add JSF components
See also:
这篇关于如何保存h:dataTable的h:inputText值?我的尝试只保存了最后一行的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!