0

I have an issue with one of my SelectOneMenu in JSF.

I have a SelectOneMenu component bound with list of Enums, and the value given to the selectOneMenu is an array element like this :

<h:selectOneMenu id="menuOperatorOne" value="#{operandExpression.operators[0]}">
                 <f:selectItem itemValue="#{null}" itemLabel=" -- Select "></f:selectItem>
                 <f:selectItems value="#{currentBean.listComparaisonOperators}"/>
                  <f:ajax render="expressionForm"/>
 </h:selectOneMenu>

The problem is that return only a String (the name of the Enum as String, not the object itself).

I know the code works well if I set the value of the selectOneMenu to a simple variable of my Enum Type in my managedBean like :

<h:selectOneMenu id="menuOperatorOne" value="#{currentBean.myEnumObject}">

So in my meaning my enums are all good and not the source of the problem.

How can I retrieve object enum in an array element ?

Thanks for your help !

EDIT after duplicate proposition :

Saying we have two bean variables like ;

private EnumType singleEnumObject;
private List<EnumType> listEnumObjects;

and we have two selectOneMenu in the JSf like (listComparaisonOperators contains my Enum values);

<h:selectOneMenu id="menuOperatorOne" value="#{currentBean.singleEnumObject}">
     <f:selectItems value="#{currentBean.listComparaisonOperators}"/>
 </h:selectOneMenu>
 <h:selectOneMenu id="menuOperatorTwo" value="#{currentBean.listEnumObjects[0]}">
     <f:selectItems value="#{currentBean.listComparaisonOperators}"/>
</h:selectOneMenu>

As you can see, they are pretty same identicals, just the second is linked to element array as value. But each of them have Enums in selectItems, and the very same treatment.

The first selectOneMenu will return Enum Object in "singleEnumObject", which is good.

The second selectOneMenu will return String in "listEnumObjects[0]" (enum name as string). Shouldn't I get my Enum Object, juste like the first SelectOneMenu ?

Does anybody have an idea of what am I doing wrong here ? Can someone provide me example where he retrieves Enum Object using array element in the SelectOneMenu value (and not single variable) ?

Thanks.

Tim-Tac
  • 1
  • 3
  • Did you read https://stackoverflow.com/questions/8229638/how-to-use-enum-values-in-fselectitems? – Kukeltje Dec 06 '18 at 10:55
  • Yes, I don't have issue to get object Enum returned if the value assigned to SelectOneMenu is a "simple" variable. My issue is only when the value is a element of an array. – Tim-Tac Dec 06 '18 at 12:38
  • Possible duplicate of [How to use enum values in f:selectItem(s)](https://stackoverflow.com/questions/8229638/how-to-use-enum-values-in-fselectitems) – Jasper de Vries Dec 06 '18 at 14:34

1 Answers1

0

in your controller class(CurrentBean) add a Enum object with getter & setter and create a get method which will return Enum objects. like follows

private EnumObject enumObject;

// generate getter and setter for enumObject

public EnumObject[] getMyEnums {
  return EnumObject.values();
}

In view page use the following

<h:selectOneMenu id="menuOperatorOne" value="#{operandExpression.enumObject}">
             <f:selectItems value="#{currentBean.myEnums}"/>
</h:selectOneMenu>    

Hope this will solve your problem. :)

Shahriar Miraj
  • 307
  • 3
  • 14
  • Hi, thanks for your reply. I correctly retrieve my enums in my SelectOneMenu so this is not my poblem. The fact is, if I set the value to a simple variable like you did, I works fine. If I set the value to an element of array of EnumObject types, it doesn't work :( – Tim-Tac Dec 06 '18 at 10:42
  • Isn't this the same as mentioned in the link posted in the comment below the question – Kukeltje Dec 06 '18 at 13:15