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

[2349 byte] By [scot_l33t@yahoo.coma] at [2007-11-27 7:09:23]
# 1
Maintain a backing map with the MyObject objects as values and it's identifiers as String keys.
BalusCa at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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.

smurray_eriea at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

smurray: yeah, this will work. But how did you think to get the actual selected values? You'll only get a List of string representations of the selected MyObject items (e.g. MyObject@a1b2c3d).

There are 2 solutions for this problem: write a converter that converts between MyObject and String, or maintain a backing map.

BalusCa at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4

not true you get a list of MyObjects for what is selected, JSF handles this for you perfectly. The actual selected value IS the object not the string.This is one case where you cannot think in terms of HTML, there is absolutely no need for a converter in this example.

While you might see <input name="j_idjsp_tag_ctru2:j_idjsp_tag_ctru3" id="j_idjsp_tag_ctru2:j_idjsp_tag_ctru3:1" value="test.MyBean$MyObject@1bf7701" type="checkbox"> in the page source this is not what you get back in JSF. That value is converted back into an object and the value of the multiple selection list box if you query it (or what you will get in valueChangeListener) is a java.util.List of MyObjects.

smurray_eriea at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

Replace your public String action() {

return null;

}

bypublic String action() {

for (MyObject myObject : selectedItems) {

System.out.println(myObject.getValue());

}

return null;

}

and see it yourself.

BalusCa at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 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

scot_l33t@yahoo.coma at 2007-7-12 19:00:45 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...