1

The selectOneMenu calls the setter method but always passes a null value. The event listener will eventually update a session variable with the selected plant id, but just logs the value for now. I tried to add a converter to manage the object, but when I set it in the selectOneMenu, the page fails with:

Class com.sun.faces.application.ApplicationImpl can not access a member of class com.conagra.data.PlantsConverter with modifiers ""

Using Primefaes 6.2 on Tomcat 9.

xhtml

...
<p:selectOneMenu id="plantList"
    value="#{userPlantsBean.selectedPlant}" height="250" effect="fade"
    style="padding:0;" converter="plantsConverter"
    rendered="#{plants != 1 and view.viewId != '/Login.xhtml'}">
    <f:selectItem itemLabel="#{plantName}" itemValue="#{userPlantsBean.selectedPlant}"
        style="padding:0"  />
    <f:selectItems value="#{userPlantsBean.userPlantsList}"
        var="plant" itemLabel="#{plant.plantDesc}"
        itemValue="#{plant}" />
    <p:ajax event="change" listener="#{userPlantsBean.onPlantChange}"  />
</p:selectOneMenu>
...

bean

...
public List<Plants> getUserPlantsList() {
    ...
}

public Plants getSelectedPlant() {
    return selectedPlant;
}

public void setSelectedPlant(Plants selectedPlant) {
    this.selectedPlant = selectedPlant;
}

public void onPlantChange(AjaxBehaviorEvent e) {
    logger.debug(selectedPlant);
}
...

converter

...
@FacesConverter(value="plantsConverter")
public class PlantsConverter implements Converter {
    private List<Plants> plantsList;

    PlantsConverter(){
        plantsList = new UserPlantsBean().getUserPlantsList();
    }

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String submittedValue) {
        if (submittedValue.trim().equals("")) {
            return null;
        } else {
            try {
                for (Plants p : plantsList) {
                    if (p.getPlantID() == submittedValue) {
                        return p;
                    }
                }
            } 
            catch (NumberFormatException exception) {
                throw new ConverterException(
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, "Conversion Error", "Not a valid plant"));
            }
        }
        return null;
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent componet, Object value) {
        if (value == null || value.equals("")) {
            return "";
        } else {
            return String.valueOf(((Plants) value).getPlantID());
        }
    }
}

Generated select html

<select id="plantList_input" name="plantList_input" tabindex="-1" aria-hidden="true" onchange="PrimeFaces.ab({s:&quot;plantList&quot;,e:&quot;change&quot;,p:&quot;plantList&quot;});">
    <option value="com.conagra.data.Plants@6a98fdfb">RENSSELAER PLANT</option>
    <option value="com.conagra.data.Plants@7785b2d0">ARCHBOLD PLANT</option>
    <option value="com.conagra.data.Plants@3e184b2b">LAKEVILLE PLANT</option>
    <option value="com.conagra.data.Plants@2279b29e">MAPLE GROVE PLANT</option>
    <option value="com.conagra.data.Plants@3adb3875">MILTON PLANT &amp; RYDER LOGISTICS</option>
    <option value="com.conagra.data.Plants@60921ecc">OAKDALE PLANT</option>
    <option value="com.conagra.data.Plants@1caf40e6">ONTARIO IMC</option>
    <option value="com.conagra.data.Plants@73adecaf">RUSSELLVILLE PLANT</option>
</select>
Ben Rivera
  • 31
  • 6
  • The type of 'value' of selectOneMenu must be the type of 'itemValue' of selectItems. There's no need for a converter. – Holger Aug 17 '18 at 06:51
  • I set the itemValue to be plant which is a Plants object, the same that is returned from selectedPlant, but no difference... – Ben Rivera Aug 17 '18 at 20:58

1 Answers1

1

First of all, selectedPlant should be a Plant object, not a DataTableColumn object:

public Plant getSelectedPlant() {
   return selectedPlant;
}
public void setSelectedPlant(Plant selectedPlant) {
   this.selectedPlant = selectedPlant;
}

After that you must set the object, not a property of the object.

<p:selectOneMenu id="plantList"
   value="#{userPlantsBean.selectedPlant}" height="250" effect="fade"
   style="padding:0;" converter="plantConverter"
   rendered="#{plants != 1 and view.viewId != '/Login.xhtml'}">
   <f:selectItems value="#{userPlantsBean.userPlantsList}"
       var="plant" itemLabel="#{plant}"
       itemValue="#{plant}" />
   <p:ajax event="change" listener="#{userPlantsBean.onPlantChange}"  />
</p:selectOneMenu>

And at last you must implement a PlantConverter that allow convert the value into a Plant object.

  • The DataTableColumn object is just a generic object with 2 properties (header and property). My label is the name of the plant and the value is the code/id for the plant. I guess the converter is the key. I will have to figure out how those work. – Ben Rivera Aug 17 '18 at 00:16
  • I tried to add a PlantsConverter, but the page fails with object not found. Is there something missing in my converter? – Ben Rivera Aug 17 '18 at 21:03
  • Did you put the annotation @FacesConverter(value = "plantConverter") to you converter class? – Diego de la Riva Aug 18 '18 at 11:14
  • I add the annotation and got: Class com.sun.faces.application.ApplicationImpl can not access a member of class com.conagra.data.PlantsConverter with modifiers "" – Ben Rivera Aug 19 '18 at 13:27
  • Diego, any idea what is missing or wrong. I have updated xhtml, the selectedPlant returns a Plants object and the userPlantsList is a list of Plants objects. – Ben Rivera Aug 21 '18 at 21:45