开发者

JSF generated pages and entities, selectOneMenu

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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜