从 bean 遍历数据表 [英] Iterating through the datatable from the bean
问题描述
我有以下数据表:
<h:form>
<h:dataTable id = "notTable" value="#{editCategoryBean.allNotifications}" var="notification">
<h:column>
<f:facet name="header">Key</f:facet>
<h:inputText id = "notkey" value="#{notification.key}" />
</h:column>
<h:column>
<f:facet name="header">Lang</f:facet>
<h:inputText id = "notlanguage" value="#{notification.language}"/>
</h:column>
<h:column>
<f:facet name="header">Value</f:facet>
<h:inputText id = "notvalue" value="#{notification.value}"/>
</h:column>
</h:dataTable>
<h:commandButton action ="#{editCategoryBean.save()}" value = "Save" > </h:commandButton>
我想从数据表中的 allNotifications 列表中编辑我的通知,并一键保存所有更改.如何从 editCategoryBean 遍历数据表?或者我如何才能实现这种行为?
I want to edit my notifications from the allNotifications List in the datatable and save all changes with one button click. How can I iterate through the datatable from the editCategoryBean? Or how can I implement this behaviour otherwise?
推荐答案
JSF 已经以通常的方式使用提交的值更新了 value="#{editCategoryBean.allNotifications}"
背后的模型.所以,你基本上需要做的就是将它传递给服务层进行保存:
JSF has already updated the model behind value="#{editCategoryBean.allNotifications}"
with the submitted values the usual way. So, all you basically need to do is to pass it through to the service layer for save:
public void save() {
yourNotificationService.save(allNotifications);
}
否则,如果您出于某种原因确实坚持自己迭代它,也许打印提交的值用于测试目的,那么只需按照通常的 Java 方式进行:
Otherwise, if you really insist in iterating over it yourself for some reason, maybe to print the submitted values for testing purposes, then just do it the usual Java way:
public void save() {
for (Notification notification : allNotifications) {
System.out.println(notification.getKey());
System.out.println(notification.getLanguage());
System.out.println(notification.getValue());
}
}
这篇关于从 bean 遍历数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!