This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF Converter and h:selectOneMenu  RSS feed

 
Vindia Hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having trouble using the JSF Converter with <h:selectOneMenu>.
I have a PersonEnum class that contains a list of PersonType classes.
I would like to use the <h:selectOneMenu> to allow a user to select the type of person from the listbox and directly bind it to the PersonEnum object. This works fine when I use the Converter with an input box eg:
<h:column>
<f:facet name="header"> <f:verbatim>Ethnicity</f:verbatim>
</f:facet>
<h:inputText id="ethnicity"
value="#{personJaxBObjTable2.ethnicity}" converter="EnumConverter" />
<h utputText value="#{msgs.required}" styleClass="smallText" />
<h:message for="ethnicity" styleClass="errorMessage"/>
</h:column>
 
Vindia Hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry.....I din't complete my question....

Okay, so I can't use my Converter class with the <h:selectOneMenu> . I want to directly bind the user selected value to an object.

My converter works fine for input boxes eg:
<h:inputText id="ethnicity" value="#{personTable2.ethnicity}"converter="EnumConverter" />

...but when I use the same for a select box it doesn't work..

My code for using the converter for the select box is as follows:
<h:selectOneMenu id="ethnicity" value="#{personTable2.ethnicity}">
<f:selectItems id="personTypeList" value="#{PersonService.personEnumItemsList}"/>
</h:selectOneMenu>
Where personTypeList contains the the different ethnicity classes.

Please, if anyone has run into a similar problem can you post a clip of your code.

Thank You,

Vindia
 
Vindia Hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I found the solution.

I was trying to use a custom Converter that would validate the users choice when an item on the dropdown list box <h:selectOneMenu> was checked.

These were the steps that I did to implement the converter.
First, I populated my list boxes with the objects in my backing bean eg:
STEP 1:
for (Iterator i = EnumUtil.getEnumerationValues(PersonEnum.class).iterator(); i.hasNext()
tempList.add(new SelectItem((PersonEnum)i.next()));
STEP 2:
On my jsf page <h:selectOneMenu> box was coded as :
<h:selectOneMenu id="personType" value="#{personJaxBObjTable2.personType}" converter="EnumConverter"><f:selectItems id="personTypeList"value="#PersonService.personTypeList}"/>
</h:selectOneMenu>
STEP 2:
 
Vindia Hari
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I found the solution.

I was trying to use a custom Converter that would validate the users choice when an item on the dropdown list box <h:selectOneMenu> was checked, and once checked I wanted the object to be bound to the selected object.

I had difficulty and couldn't find a solution out there, so I would like to share my solution with those who have the same problem.

These were the steps that I did to implement the converter.
First, I populated my list boxes with the objects in my backing bean eg:
STEP 1:
for (Iterator i = EnumUtil.getEnumerationValues(PersonEnum.class).iterator(); i.hasNext()
tempList.add(new SelectItem((PersonEnum)i.next()));
STEP 2:
On my jsf page <h:selectOneMenu> box was coded as :
<h:selectOneMenu id="personType" value="#{personJaxBObjTable2.personType}" converter="EnumConverter"><f:selectItems id="personTypeList"value="#PersonService.personTypeList}"/>
</h:selectOneMenu>

STEP 3: I created my "EnumConverter" class that implements Converter and teh two methods and put my logic in there.

e.g.
public Object getAsObject(FacesContext context, UIComponent component, String newValue)
throws ConverterException{

if (checkIfEthnicityEnum(newValue)) {
return EthnicityEnum.fromString(newValue);
}
public String getAsString(FacesContext context, UIComponent component, Object objValue)
throws ConverterException{

return objValue.toString();
}

STEP 4: Include the Converter in teh faces-config.xml file
<converter>
<converter-id>EnumConverter</converter-id>
<converter-class>your package name .EnumConverter</converter-class>
</converter>
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!