Hi,
I'm having trouble getting select items to work in a way that I'm not
even sure they are supposed to work.
I would like to make a list of select items that are created around
the instances of a some class, lets say DataItem. I would like to then
use this with tomahawk's selectManyCheckbox tag so that I can select
these instances.
So my approach was to create a converter for the DataItem class:
public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2)
throws ConverterException {
System.out.println("Converting from string to object");
DataItem d = new DataItem(Integer.getInteger(arg2));
return d;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2)
throws ConverterException {
System.out.println("Converting from object to string");
System.out.println(arg2);
try {
DataItem d = (DataItem) arg2;
return "" + d.getValue();
} catch (Exception e) {
System.out.println("Fug");
System.out.println(e.getMessage());
e.printStackTrace();
throw new ConverterException();
}
}
then populate a list with select items constructed like this:
public List<SelectItem> getDataItems() {
LinkedList<SelectItem> items = new LinkedList<SelectItem>();
System.out.println("Getting data items");
for (DataItem i : dataItems) {
items.add(new SelectItem(i, "Item Value: " + i.getValue()));
}
return items;
}
and a I set up the checkboxes with
<t:selectManyCheckbox value="#{dataItemBean.selectedItems}">
<f:selectItems value="#{dataItemBean.dataItems }" />
</t:selectManyCheckbox>
The dataItemsBean has getters and setters for the property selectedItems
private List<DataItem> selectedItems;
public List<DataItem> getSelectedItems() {
return selectedItems;
}
public void setSelectedItems(List<DataItem> selectedItems) {
this.selectedItems = selectedItems;
}
My faces config is:
<faces-config>
<managed-bean>
<managed-bean-name>dataItemBean</managed-bean-name>
<managed-bean-class>stuff.DataItemBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
<converter>
<converter-for-class>stuff.DataItem</converter-for-class>
<converter-class>stuff.DataItemConverter</converter-class>
</converter>
</faces-config>
I've gotten this as far as showing validation errors when i press
submit. I am not sure if I can just write a validator or something
that allows it to work. Also I'm not sure this test case works exactly
like my actual implementation as I'm pulling the objects i'm putting
in the selectitems out of a database. So my converter is talking to
the database and doing more work.
I think the issue stems from the use of List<DataItem>. I've done the
same thing using string lists and manually converting.
I'd like to have it so I didn't have to use two lists or manually
convert the data.
If you can offer any suggestions, links, documentation, or anything I
would be grateful.
Thanks,
Eugene