How can I write a custom converter when working with PrimeFaces components that use a list of POJO? My particular problem is with
<
This problem is not primefaces related, just general JSF related.
Why should you hit the database again? Your bean already contains the list of Objects you want to display in a component, or is it request scoped?
If you access the final list via a get property in your bean or the nested one in the converter is of your choice.
public class SuperPojo
{
protected Integer id;
//constructor & getter
}
public class PojoTest extends SuperPojo
{
private String label = null;
//constructor & getter
}
public class SuperPojoConverter implements Converter
{
private Collection superPojos;
public IdEntityConverter(Collection superPojos)
{
this.superPojos = superPojos;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
//catch exceptions and empty or null value!
final int intValue = Integer.parseInt(value);
for(SuperPojo superPojo : this.superPojos)
if(superPojo.getId().intValue() == intValue)
return superPojo;
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value)
{
//catch null and instanceof
return String.valueOf(((SuperPojo)value).getId().intValue());
}
public Collection getSuperPojos()
{
return this.superPojos;
}
}
public class Bean
{
private SuperPojoConverter pojoTestConverter = null;
public Bean()
{
final List pojoTests = //get list from hibernate
this.pojoTestConverter = new SuperPojoConverter(pojoTests);
}
public SuperPojoConverter getPojoTestConverter()
{
return this.pojoTestConverter;
}
}