How to preselect an option of h:selectOneMenu

随声附和 提交于 2019-11-26 22:07:19

问题


Is it possible to preselect one of the options from the select menu?

I have this UI Component:

<h:selectOneMenu value="#{authenticateController.country}">
    <f:selectItems value="#{constants.countrySelectMenu}" />
</h:selectOneMenu>

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?


回答1:


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";
    }

    // ...
}

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.

See also:

  • How to populate options of h:selectOneMenu from database?


来源:https://stackoverflow.com/questions/8884910/how-to-preselect-an-option-of-hselectonemenu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!