添加“未选择"的最佳方式JSF 中 selectOneMenu 的选项 [英] Best way to add a "nothing selected" option to a selectOneMenu in JSF

查看:25
本文介绍了添加“未选择"的最佳方式JSF 中 selectOneMenu 的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering what would be the best or easiest way to allow a user to select nothing in a selectOneMenu.

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

My example: I have a list of registered users and the administrator should be able to filter the list of displayed users by some criterias. These criterias, like the usertype (employee, customer, ...) can be chosen by selectOneMenus, like this:

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

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

When the corresponding selectOneMenu is being backed by a list of POJOs using a converter, how can I add an item to the list indicating that the user didn't choose any specific item? Currently I have a dummy usertype object displaying the label "---", but this is causing several problems in other areas of my application and I don't think that this is the best solution.

推荐答案

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

Just explicitly set the select item value to null.

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

不,像 itemValue="" 这样的空字符串是不够的.它确实必须是 null.否则,您会遇到此问答中所述的问题:使用请选择"f:selectItem 在 p:selectOneMenu 中具有 null/空值.

No, an empty string like itemValue="" is not sufficient. It really has to be null. Otherwise you run into trouble as described in this Q&A: Using a "Please select" f:selectItem with null/empty value inside a p:selectOneMenu.

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

If the item happen to be required="true" and you're using JSF 2.x, then you could add noSelectionOption="true" to the select item. This is only useful if you also set hideNoSelectionOption="true" on the selection component. It will then hide the empty option in the list once the enduser selects a different item, hereby making it impossible to re-select the empty option.

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

另见

See also page 114 of The Definitive Guide to JSF under section "SelectItem tags":

请注意,如果与选择组件的 value 属性关联的 bean 属性为 #{null},则可以使用值为 #{null}代码>空.如果您查阅了 <f:selectItem> 的标签文档,那么您可能已经注意到 noSelectionOption 属性并认为它旨在表示一个";没有选择选项".事实上,这不是真的.许多初学者确实如此认为,正如您在互联网上的许多论坛、问答网站和低质量教程中看到的那样.尽管具有误导性的属性名称,但它并不代表无选择选项".

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".

更好的属性名称应该是 hideWhenOtherOptionIsSelected,即使如此,它也只有在父选择组件明确具有 hideNoSelectionOption=true" 属性集时才有效.因此,hideWhenOtherOptionIsSelectedAndHideNoSelectionOptionIsTrue 最终将成为最不言自明的属性名称.不幸的是,当在 JSF 1.2 中实现 noSelectionOption 时,这并没有经过深思熟虑.不应该需要此属性的两个属性才能起作用.该属性对的主要目的是防止网站用户重新选择无选择选项".当组件已经选择了一个非null 值时.例如,通过在 @PostConstruct 方法中准备它,或者在使用非 null 值提交表单后重新渲染组件.

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 中 selectOneMenu 的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆