JSF: SelectItem referencing an object -> “Conversion error occurred”

Maybe I’m hitting another JSF quirk… but I have yet to see an example using this method:

Lets say I have a drop down list:

<h :selectOneMenu id="hardwareTypeItem" value="#{Model.hardwareTypeItem}">
<f :selectItems value="#{Item.hardwareTypeItems}"></f>
</h>

this is my Item.hardwareTypeItems:

List items = mgr.getAllHardwareTypeItems();
for (int i = 0; i < items.size(); i++) {
h = (HardwareTypeItem) items.get(i);
hardwareTypeItems.add(new SelectItem(h,h.getDescription(),h.getDescription()))
}

Here is my Model.hardwareTypeItem:
/**
* @return Returns the hardwareTypeItem.
* @hibernate.many-to-one column="type_id"
*/
public HardwareTypeItem getHardwareTypeItem() {
return hardwareTypeItem;
}

Being realistic, I doubt the assumption of JSF to just know hey that’s my object and update the Model with the proper reference, but that’s not the case and shoots back a “Conversion error occurred” for the message output.

— UPDATE —

After a little digging around, I downloaded the JSF source code and happened to stumble upon additional examples under the ”standard” directory. Now I feel a bit dumb as its straight forward process using the javax.faces.convert.Converter interface.

I created a converter and then just register your custom converter in the faces-config.xml file like so:

<converter>
<converter -for-class>model.item.BaseItem</converter>
<converter -class>converters.BaseItemConverter</converter>
</converter>

This entry was posted in Tech and tagged , . Bookmark the permalink.

Comments are closed.