Using NetBeans, I have generated Hibernate mapping files and set of POJOs. I have also generated a set of JSF pages from entity classes (those generated POJOs).
Now, I am trying to add a dropdown menu that would enable me to select one of the enitites.
<h:selectOneMenu value="#{measurementController.sensor}">
<f:selectItems value="#{sensorController.itemsAvailableSelectOne}" />
</h:selectOneMenu>
getItemsAvailableSelectOne() calls this method:
public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {
int size = selectOne ? entities.size() + 1 : entities.size();
SelectItem[] items = new SelectItem[size];
int i = 0;
if (selectOne) {
items[0] = new SelectItem("", "---");
i++;
}
for (Object x : entities) {
items[i++] = new SelectItem(x, x.toString());
}
return items;
}
In measurementController class I have this:
private Sensor sensor;
public Sensor getSensor() {
return this.sensor;
}
public void setSensor(Sensor sensor) {
this.sensor = sensor;
}
Whatever I do, I get Validation Error: Value is not valid
error when I select any entry in the dropdown menu. Why?
I have a feeling that I am missing something very obvious, but I just can't see it.
EDIT:
Digging trough generated code, I've found an existing converter class:
@FacesConverter(forClass=Sensor.class)
public static class SensorControllerConverter implements Converter {
public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
if (value == null || value.length() == 0) {
return null;
}
SensorController controller = (SensorController)facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null, "sensorController");
return controller.ejbFacade.find(getKey(value));
}
java.lang.Integer getKey(String value) {
java.lang.Integer key;
key = Integer.valueOf(value);
return key;
}
String getStringKey(java.lang.Integer value) {
StringBuffer sb = new StringBuffer();
sb.append(value);
return sb.toString();
}
public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Sensor) {
Sensor o = (Sensor) object;
return getStringKey(o.getIdSensor());
} else {
throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: "+SensorController.class.getName());
}
}
}
When I step trough the code with the debugger it all seems to work fine. First the getAsObject method is called with the selected item as argument, and the return value is the Sensor object.
Then the getSensor() method is called that returns null (the current value stored in the measurementController class).
And last, the getAsString() method gets called for every item in the dropdown menu. I think this one is a part of Render response phase and has nothing to do with validation.