How to use si:selectItems with an object that has a String i
lawrieg Jan 31, 2007 7:45 AMHi,
I'm a Seam newbie and I'm trying to get my head around si:selectItems and the Seam application framework.
My CustomerType object has a String id so when I initially tried to use si:selectItems without creating and specifying a converter I got an exception:
<h:selectOneMenu value="#{ customerHome.instance.customerType}" id="customerType"> <si:selectItems value="#{customerTypes.resultList}" var="customerType" label="#{customerType.name}" noSelectionLabel="Please Select..." /> </h:selectOneMenu>
javax.faces.convert.ConverterException: javax.faces.application.FacesMessage@154fe9c com.mycomp.myapp.CustomerType at org.jboss.seam.selectitems.ui.BasicEntityConverter.getIdAsString(BasicEntityConverter.java:61) at org.jboss.seam.selectitems.jsf.EntityConverter.convertToString(EntityConverter.java:36) at org.jboss.seam.selectitems.jsf.SelectItemsConverter.getAsString(SelectItemsConverter.java:131) at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(RendererUtils.java:544) ... Caused by: java.lang.NullPointerException at org.jboss.seam.selectitems.ui.BasicEntityConverter.getIdAsString(BasicEntityConverter.java:59) ... 43 more
So I then created a CustomerTypeSelectItemsConverter which extends SelectItemsConverter:
@Name("customerTypeSelectItemsConverter") public class CustomerTypeSelectItemsConverter extends org.jboss.seam.selectitems.jsf.SelectItemsConverter { @Override protected Object convertToObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException { return arg2; } @Override protected String convertToString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException { return ((CustomerType)arg2).getId(); } }
and changed my facelets code:
<h:selectOneMenu value="#{customerHome.instance.customerType}" converter="#{customerTypeSelectItemsConverter}" id="customerType"> <si:selectItems value="#{customerTypes.resultList}" var="customerType" label="#{customerType.name}" noSelectionLabel="Please Select..." /> </h:selectOneMenu>
This gets my drop down list displaying correctly but gives me a validation error "value is not valid" when I try to save. I presume this means that I should be returning a CustomerType object (rather than the CustomerType object's id) from CustomerTypeSelectItemsConverter's convertToObject() method?
Am I on the right lines now?
If so, what is the correct way for me to obtain the CustomerType object? Should I somehow be getting the List from the Seam Application Framework EntityQuery object ("customerTypes") that was used to populate the si:selectItems tag and then iterating the List to find find the CustomerType with the specified id? Or is there a better way?
Any help would be greatly appreciated as I can't find any examples which match my problem and I'm struggling to find any answers...
Thanks,
Lawrie