• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSF drop down Converter issues  RSS feed

 
Bansilal Haudakari
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having problems with JSF 1.1_01 (MyFaces 1.1.1).

I have created page called page.jsp as follows:

. . . .

<h:selectOneMenu value="#{test.selectedDevice}" id="deviceTypeList" styleClass="dropdown">
<f:selectItems value="#{test.deviceTypes}" />

<ajax:support action="#{test.loadDevice}" event="onchange" reRender="t2,t3,t4,t5"/>
</h:selectOneMenu>
. . . .

It is uses managed bean called TestBean.java:

public class TestBean {

public List getDeviceTypes(){
logger.info(" *** In getDeviceTypes Backing Bean*** ");
List<SelectItem> models = new ArrayList<SelectItem>();
List<SelectItem> deviceTypes = new ArrayList<SelectItem>();
// Gets Data from Hibernate Query . It returns List of Device Types
models = deviceManager.getDeviceTypes();
logger.info(" *** DeviceType List Size=*** "+models.size());

for (Iterator it = models.iterator(); it.hasNext();) {
System.out.println("Inside For Loop Iterator size="+models.size());
Object[] row = (Object[]) it.next();
System.out.println("ID: " + row[0]); // prints data
System.out.println("Name: " + row[1]); //prints data
// Below line results in error : does not have a Converter
deviceTypes.add(new SelectItem(row[0],row[1]+""));
}



return deviceTypes;

}
}



During page rendering, page.jsp throws following exception:
javax.servlet.ServletException: Value is no String and component _id0:deviceTypeList does not have a Converter


Is this bug with MyFaces 1.1.1 or I have made something wrong?

I am a newbie & didnt know much about Converters hence read some examples from the web stating to have getObject() & getString methods . So i added below Class but not sure how to align with page.jsp page.
public class DeviceTypeConverter implements Converter{
protected final Log logger = LogFactory.getLog(getClass());
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String s) throws ConverterException {
DeviceDao dao = new DeviceDao();
return dao.getdeviceTypes().(Long.decode(s));
}

public String getAsString(FacesContext facesContext, UIComponent
uiComponent, Object o) throws ConverterException {

if(o!= null)
logger.debug(o.toString());

try {
if((o!= null) && (o instanceof DeviceType)){
DeviceType dt = (DeviceType) o;
return ""+dt.getId();
}
else
return "0";
} catch (Exception e) {
logger.error(e);
throw new ConverterException();
}
}

}

Any pointers/suggestions on how to write a Converter & help in resolving the error will be highly appreciated

Regards
Bansi
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!