doubt regarding SelectItem class in <f:selectItem>
hi
i wanted to clarify if any converter is needed in my case. i feel its not needed in case of checkboxes/listbox but just in case i am wrong somwhere
w.r.t code below
publicclass MyBean{
private List<MyObject> selectedItems;
private List<SelectItem> selectItems;
.....
public List<SelectItem> getSelectItems(){
if (selectItems ==null){
........
selectItems =new ArrayList<SelectItem>();
selectItems.add(new SelectItem(myob1,"label1"));
selectItems.add(new SelectItem(myob2,"label2"));
selectItems.add(new SelectItem(myob3,"label3"));
.........
}
return selectItems;
}
<h:form>
<h:selectManyCheckbox value="#{myBean.selectedItems}">
<f:selectItems value="#{myBean.selectItems}" />
</h:selectManyCheckbox>
<h:commandButton value="submit" action="#{myBean.action}" />
</h:form>
where myob1,2,3 are of type MyObject
the checkboxes appear properly in my page , but aftre i select few checkboxes and click next and i get a conversion error saying:
Coversion Error setting value 'mypackage...@1233' for 'null Converter'
actaually when i select few checkboxes i expect the object to be passed as the itemValue is myObj1,etc and so #{myBean.selectedItems} shud have a list of all my selected objects but it doesnt work.
so i have kept the type of selectedItems as String array and am converting my object to String and getting it back from String.
but i do not want this way
any help appreciated
# 2
Your assumption is correct, if the value in SelectItem is of type MyObject then that should be the type as well.
So I did your example and it works fine, here is my code it might trigger your issue if we are running on different assumptions.
public class MyBean {
public MyBean() {
}
private List<MyObject> selectedItems;
private List<SelectItem> selectItems;
public List<SelectItem> getSelectItems() {
if (selectItems == null) {
selectItems = new ArrayList<SelectItem>();
selectItems.add(new SelectItem(new MyObject("one"), "label1"));
selectItems.add(new SelectItem(new MyObject("two"), "label2"));
selectItems.add(new SelectItem(new MyObject("three"), "label3"));
}
return selectItems;
}
public String action() {
return null;
}
public void setSelectedItems(List<MyBean.MyObject> param) {
this.selectedItems = param;
}
public List<MyBean.MyObject> getSelectedItems() {
return selectedItems;
}
public static final class MyObject {
String _value = null;
public MyObject() {
}
public MyObject(String value) {
_value = value;
}
public void setValue(String param) {
_value = param;
}
public String getValue() {
return _value;
}
}
}
My web application looks as you posted. Note that my selections come in via the method setSelectedItems which is a List of MyObjects. Works fine for me.
# 6
yes balusC is right.
thats exactly my problem and so
i had to convert my obj to string( using my method )and use it in SelectItem
and then constructing my obj from String.
this works for me but it seems not a good idea.
what i am trying to achieve is to eliminate all these string passing from my code and have actual object to be collected in the selected item rather than a string array.
i want to make it possible to somehow create my own SelectItem class that can achieve this i.e entire object is passed and collected.
any ideas on how i can implement it.
regards
scot