picklist conversion problem
sparkettin May 19, 2009 5:45 PMhello,
I am new in jsf. for the last 2 weeks, I have been trying to solve a problem (which is I guess an easy one for most experienced developers) with rich:pickList (I am using it for the first time). I made a search on internet and found the sample code below. I exactly applied it to my object class. I can get my objects listed their names on the left side of picklist. but when I select an item, rich:messages reports "conversion error" and does not save my values. I can not find any solution to my problem and I almost give up. Please help me.... thanks...
<rich :p ickList value="#{yourBean.pickListResult}"
copyAllControlLabel="add all"
removeAllControlLabel="remove all"
copyControlLabel="add"
removeControlLabel="remove"
converter="#{yourBean.converter}">
<f:selectItems value="#{yourBean.leftPickList}" />
<a4j:support event="onlistchanged"
action="#{yourBean.updateSelection}" reRender="whatever"/>
</rich :p ickList>
public class YourBean {
private boolean firstFill = true;
//the two PickLists
private List leftPickList = new ArrayList();
private List<Your_Database_Object> pickListResult = new ArrayList<Your_Database_Object>();
//Converting from SelectItems to your Object:
private YourConverter converter;
//rendering the dataTable depends on pickListResult.size()>0
public boolean isResultListFilled() {
if(firstFill)
preload();
if(this.pickListResult.size()>0)
return true;
else
return false;
}
private void preload() {
for(Your_Database_Object a: getPreselectionListFromDB()){
pickListResult.add(a);
}
}
public List getLeftPickList() {
this.leftPickList = new ArrayList();
//this list collects all items for the converter
List<Your_Database_Object> tmpList = new ArrayList<Your_Database_Object>();
//adding result items to the list to be added to the converter
for(Your_Database_Object x: this.pickListResult)
tmpList.add(x);
for(Your_Database_Object a: getAvailableSelectionListFromDatabase()){
tmpList.add(a);
}
//creates the converter on first load with all possible items including result items
if(firstFill)
converter = new YourConverter(tmpList);
firstFill = false;
//adding all items to the SelectItem-list
for(Your_Database_Object a: tmpList){
SelectItem s = new SelectItem(a,a.getYourObjectName());
this.leftPickList.add(s);
}
return leftPickList;
}
//other important Getters and Setters
public YourConverter getConverter() {
return converter;
}
public void setConverter(YourConverter con) {
this.converter = con;
}
public void setPickListResult(List<Your_Database_Object> pickListResult) {
this.pickListResult = pickListResult;
}
public List<Your_Database_Object> getPickListResult() {
return pickListResult;
}
... other methods
}
class YourConverter implements Converter{
//converter uses a HashMap for switching from selectItems to your objects
private HashMap<String, Your_Database_Object> map;
public YourConverter(List<Your_Database_Object> objekts) {
map=new HashMap<String, Your_Database_Object>();
for(Your_Database_Object o :o bjekts){
map.put(Integer.toString(o.getYourObjectID()), o);
}
}
public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) {
return map.get(string);
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
if(obj instanceof Your_Database_Object)
return Integer.toString(((Your_Database_Object)obj).getYourObjectID());
return null;
}
}