You need to implement Converter#getAsString()
so that the desired Java Object is been represented in an unique string representation which can be used as HTTP request parameter. Using the database technical ID (the primary key) is very useful here.
public String getAsString(FacesContext context, UIComponent component, Object value) {
// Convert the Project object to its unique String representation.
return String.valueOf(((Project) value).getId());
}
Then you need to implement Converter#getAsObject()
so that the HTTP request parameter (which is per definition String
) can be converted back to the desired Java Object (Project
in your case)`.
public Object getAsObject(FacesContext context, UIComponent component, String value) {
// Convert the unique String representation of Project to the actual Project object.
return projectDAO.find(Long.valueOf(value));
}
Finally just associate this converter with the object type in question, JSF will then take care about conversion when Project
comes into the picture no need to specify a converterId
or a f:converter
:
<converter>
<converter-for-class>com.example.Project</converter-for-class>
<converter-class>com.example.ProjectConverter</converter-class>
</converter>
This way you can just create SelectItem
with Project
as value.
You can get some background info and more ideas out of this blog article: http://balusc.blogspot.com/2007/09/objects-in-hselectonemenu.html