gpt4 book ai didi

jsf - 在 JSF 中向 selectOneMenu 添加 "nothing selected"选项的最佳方法

转载 作者:行者123 更新时间:2023-12-03 06:44:01 26 4
gpt4 key购买 nike

我想知道允许用户在 selectOneMenu 中不选择任何内容的最佳或最简单的方法是什么。

我的示例:我有一个注册用户列表,管理员应该能够按某些条件过滤显示的用户列表。这些标准,例如用户类型(员工、客户……)可以通过 selectOneMenus 选择,如下所示:

<h:selectOneMenu value="#{myBean.selectedUsertype}" converter="#{usertypeConverter}">
<f:selectItems value={myBean.usertypes}" />
</h:selectOneMenu>

当使用转换器的 POJO 列表支持相应的 selectOneMenu 时,如何向列表中添加一个项目以指示用户未选择任何特定项目?目前,我有一个显示标签“---”的虚拟用户类型对象,但这在我的应用程序的其他区域引起了一些问题,我认为这不是最好的解决方案。

最佳答案

只需将选择项值显式设置为 null .

<h:selectOneMenu value="#{bean.selectedItem}">
<f:selectItem itemValue="#{null}" itemLabel="--select--" />
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

不,空字符串如 itemValue=""还不够。确实必须是null 。否则,您会遇到此问答中所述的麻烦:Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu .

如果该项目恰好是 required="true"并且您使用的是 JSF 2.x,那么您可以添加 noSelectionOption="true"到选择的项目。如果您还设置了 hideNoSelectionOption="true",这才有用。在选择组件上。一旦最终用户选择不同的项目,它就会隐藏列表中的空选项,从而无法重新选择空选项。

<h:selectOneMenu value="#{bean.selectedItem}" hideNoSelectionOption="true">
<f:selectItem itemValue="#{null}" itemLabel="--select--" noSelectionOption="true" />
<f:selectItems value="#{bean.availableItems}" />
</h:selectOneMenu>

另请参阅 The Definitive Guide to JSF 的第 114 页在“SelectItem 标签”部分下:

Note that a select item with value of #{null} can be used to present the default selection in case the bean property associated with selection component's value attribute is null. If you have consulted the tag documentation of <f:selectItem>, then you'll perhaps have noticed the noSelectionOption attribute and have thought that it was intended to represent a "no selection option". Actually, this isn't true. Many starters indeed think so, as you can see in many forums, Q&A sites, and poor-quality tutorials on the Internet. In spite of the misleading attribute name, it does not represent a "no selection option".

A better attribute name would have been hideWhenOtherOptionIsSelected, and even then it works only when the parent selection component has explicitly a hideNoSelectionOption="true" attribute set. So, hideWhenOtherOptionIsSelectedAndHideNoSelectionOptionIsTrue would ultimately have been the most self-explanatory attribute name. Unfortunately, this wasn't very well thought out when the noSelectionOption was implemented in JSF 1.2. Requiring two attributes for this attribute to function shouldn't have been necessary. The primary purpose of this attribute pair is to prevent the web site user from being able to re-select the "no selection option" when the component has already a non-null value selected. For example, by having it prepared in a @PostConstruct method, or by re-rendering the component after a form submit with a non-null value.

版权免责声明:本书由我编写。

关于jsf - 在 JSF 中向 selectOneMenu 添加 "nothing selected"选项的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11360030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com