如何预选择h:selectOneMenu的选项 [英] How to preselect an option of h:selectOneMenu
问题描述
是否可以从选择菜单中预先选择其中一个选项?
Is it possible to preselect one of the options from the select menu?
我有这个UI组件:
<h:selectOneMenu value="#{authenticateController.country}">
<f:selectItems value="#{constants.countrySelectMenu}" />
</h:selectOneMenu>
#{constants.countrySelectMenu}
的值是国家/地区ID-国家/地区名称对的列表.
有没有一种方法可以用预先选择的值来呈现列表,或者至少有一种解决方法可以做到这一点?
The values of #{constants.countrySelectMenu}
are a list of country ID - country name pairs.
Is there a way to render the list with a preselected value or at least is there a work-around to get this done?
推荐答案
只需将<h:selectOneMenu value>
后面的属性设置为所需的值即可.您可以使用例如bean的(后)构造函数,action(侦听器)方法等来完成此操作.
Just preset the property behind <h:selectOneMenu value>
with the desired value. You can do it in for example the bean's (post)constructor, action(listener) method, etc.
在您的特定示例中,例如
In your specific example, thus e.g.
public class AuthenticateController {
private String country;
@PostConstruct
public void init() {
country = "NL";
}
// ...
}
对于所有其他 UIInput
组件,例如<h:inputText>
等.输入组件的值本身仅表示(预先)选择/检查/填充的值.
It works exactly the same way for all other UIInput
components like <h:inputText>
and so on. The input component's value itself simply represents the (pre)selected/checked/filled value.
这篇关于如何预选择h:selectOneMenu的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!