only shows toString() of the model as item label

前端 未结 1 1433
逝去的感伤
逝去的感伤 2020-12-20 06:03

I am facing a problem to populate SelectOneMenu correctly using converter. I followed several google references and lastly http://balusc.blogspot.in/2007/09/objects-in-hsele

相关标签:
1条回答
  • 2020-12-20 06:41

    Your problem is two-fold.

    Firstly, in the output the select item label is being presented, not the select item value. The converter only affects the select item value, not the select item label. Unless otherwise specified via itemLabel attribute, the select item label defaults to toString() outcome of the select item value.

    Assuming that you want to display the gender property as select item label, then this should do:

    <f:selectItems value="#{registrationBean.genderList}" var="gender"
        itemValue="#{gender}" itemLabel="#{gender.gender}" />
    

    (it should be said that having a property with the same name as the class itself is quite awkward)

    That should fix the presentation fail.

    Secondly, you're for the output converting from Gender instance to its gender property. However, you're for the input attempting to interpret the outputted gender property as an id in order to find the associated Gender instance. This doesn't make any sense. Fix the converter accordingly to give the id back in getAsString(), exactly as expected by getAsObject().

    return ((Gender) value).getId().toString();
    

    That should fix the form submit fail.

    See also:

    • How to populate options of h:selectOneMenu from database?
    0 讨论(0)
提交回复
热议问题