带有 selectOneListbox 的 JSF dataTable
我有一个列出一些对象的数据表,我想使用 selectOneListbox 为这些对象设置属性。这是我的 dataTable
<h:dataTable value="#{someHandler.entities}"
binding="#{someHandler.dataTable}" var="entitiy">
<h:column>
<f:facet name="header">
<t:outputText value="Level" />
</f:facet>
<h:selectOneListbox id="level" value="#{entitiy.level}" size="1"
valueChangeListener="#{someHandler.changeLevel}"
onchange="submit()">
<f:selectItem itemValue="-" itemLabel="-" />
<f:selectItem itemValue="ALL" itemLabel="ALL" />
(and so on)
</h:selectOneListbox>
</h:column>
<h:column>
<f:facet name="header">
<t:outputText value="Name" />
</f:facet>
<h:outputText value="#{entitiy.name}" />
</h:column>
</h:dataTable>
valueChangeListener 看起来像这样:
public void changeLevel(ValueChangeEvent event) {
String newLevel = (String) event.getNewValue();
Logger logger = (Logger) dataTable.getRowData();
logger.setLevel(Level.toLevel(newLevel));
}
(dataTable 是一个 HtmlDataTable 对象。)
但是,事件对象始终相同 - 无论 selectOneListbox 位于哪一行。(似乎总是第一行中的记录器)。我得到的 Logger 对象也不是我想要的。
有什么想法吗?谢谢你!
还有其他人的问题吗?即使我有 valueChangeListener,是否也会调用entitiy.setLevel() 方法?我使用entitiy.level,因为我想将所选级别显示为这些实体的默认级别。
谢谢你!
I have a dataTable that lists some objects and I want to set a property for those objects using a selectOneListbox. This is my dataTable
<h:dataTable value="#{someHandler.entities}"
binding="#{someHandler.dataTable}" var="entitiy">
<h:column>
<f:facet name="header">
<t:outputText value="Level" />
</f:facet>
<h:selectOneListbox id="level" value="#{entitiy.level}" size="1"
valueChangeListener="#{someHandler.changeLevel}"
onchange="submit()">
<f:selectItem itemValue="-" itemLabel="-" />
<f:selectItem itemValue="ALL" itemLabel="ALL" />
(and so on)
</h:selectOneListbox>
</h:column>
<h:column>
<f:facet name="header">
<t:outputText value="Name" />
</f:facet>
<h:outputText value="#{entitiy.name}" />
</h:column>
</h:dataTable>
The valueChangeListener looks like this:
public void changeLevel(ValueChangeEvent event) {
String newLevel = (String) event.getNewValue();
Logger logger = (Logger) dataTable.getRowData();
logger.setLevel(Level.toLevel(newLevel));
}
(dataTable is an HtmlDataTable object.)
However, the event object is always the same - no matter which row the selectOneListbox was in. (It seems always the logger in the first row). The Logger object I get is also not the one I want.
Any ideas? Thank you!
And anothers questions? Is the entitiy.setLevel() method called even though I have a valueChangeListener? I use entitiy.level because I want to show the chosen level as a default for those entity.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个潜在的问题:
首先,
onchange="submit()"
提交整个表单。valueChangeListener
将在所有提交值与初始值不同的输入元素上调用。您需要将
#{entitiy.level}
后面的值预设为与默认菜单选项相同的值。例如在构造函数中。或者,更好的方法是,将默认值设为
null
。这样当特定菜单未更改时,将不会调用
valueChangeListener
。或者,当您已经使用 JSF 2.x 时(请始终在 JSF 问题中提及确切的 JSF impl/版本),您可以使用
标记来实现此目的,而无需使用valueChangeListener
与 hackyonchange="submit()"
。其次,您需要确保数据表值
#{someHandler.entities}
在提交期间返回与初始请求期间完全相同相同的列表。因此,在 bean(后)构造函数中加载数据。在 JSF 2.x 中,您还希望将 bean 放入视图范围中。与具体问题无关,您也可以只使用< /代码>。
而不是There are two potential problems here:
First, the
onchange="submit()"
submits the entire form. ThevalueChangeListener
will be invoked on all input elements of which the submitted value differs from the initial value.You need to preset the value behind
#{entitiy.level}
with the same value as the default menu option. E.g. in the constructor.Or, better, make the default value
null
.so that the
valueChangeListener
won't be invoked when the particular menu is not changed.Or, when you are already on JSF 2.x (please always mention exact JSF impl/version in your JSF questions), you can use
<f:ajax>
tag for this without the need for avalueChangeListener
with a hackyonchange="submit()"
.Second, you need to ensure that the datatable value
#{someHandler.entities}
returns exactly the same list during the submit as it was during the initial request. So, do the data loading in the bean (post)constructor. In JSF 2.x you'd like to put the bean in the view scope as well.Unrelated to the concrete problem, you can also just use
<h:selectOneMenu>
instead of a<h:selectOneListbox size="1">
.