tags:

views:

46

answers:

2

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.

+1  A: 

You need to define a converter to convert object Sensor to String and vice-versa.

This converter will be used by JSF to convert the Sensor to String when the page will be rendered, and it will be also used to convert from String to Sensor when the value will be set in the measurementController bean and in the JSF component tree structure.


First, define a converter that will implements the javax.faces.convert.Converter interface. In this class, define the two methods getAsObject() and getAsString(). They are used to make the conversion.

Then, register this converter in the faces-config.xml file:

<converter>
  <converter-id>SensorConverter</converter-id>
  <converter-class>my.application.SensorConverter</converter-class>      
</converter>

Eventually, you can also define an automatic convertor for a specific class (in this case, the last step will not be needed):

<converter>
  <converter-for-class>foo.bar.Sensor</converter-for-class>
  <converter-class>my.application.SensorConverter</converter-class>
</converter>

Finally, use the <f:converter id="SensorConverter"/> or converterId="SensorConverter" in your <h:selectOneMenu/> component.

More information about conversion and validation can be found here.

romaintaz
Thanks for the quick response. Though, I have just one problem with this. Digging trough generated code I've found it that there already exists a Converter class for Sensor, but it is not working. I'll edit the question with the code....
skajfes
The JSF 2.0 way of registering a converter is using `@FacesConverter` annotation. By the way, if there was no suitable converter, the OP would rather have got a conversion error like `Conversion Error setting value 'com.example.Sensor@1234567' for 'null Converter'` before validation could take place.
BalusC
+2  A: 

Validation Error: Value is not valid

In case of a <h:selectOneMenu>, you can get this validation error whenever the selected item doesn't match any of the items available in the list as per the Object#equals() contract. Basically, it boils down that sensor.equals(selectItem.getValue()) has never returned true for any of the SelectItem items available in the list. This can happen if the equals() method of the Sensor class is not or poorly implemented.

BalusC
I can't believe that I have wasted three days on this! How can I be so stupid. I didn't implement the equals method. What is even worse - it came to me that the validator has to compare two Sensor instances, and I implemented the equals method - comparing Integer by reference instead of by value. Thanks a bunch for the tip.
skajfes
You're welcome. You can find some hints in [this answer](http://stackoverflow.com/questions/3181339/right-way-to-implement-equals-contract/3181374#3181374).
BalusC