0
votes

I have a problem when i use the following drop down list in my page. In detail , when i use the selectOneMenu and press on the button listed below the method save is never called. It just goes through the all the methods i have defined with an f:event only. Once i remove the selectOneMenu from my page and press the save button again, my method is called without any problem.

<p:selectOneMenu id="unitList"
    value="#{myController.userDTO.selectedUnitDTO}">
    <f:selectItems value="#{myController.unitList}" var="unitList"
        itemValue="#{unitList}" itemLabel="#{unitList.name}" />
</p:selectOneMenu>

The drop down list data is loaded by the following method (Note. unitList is initialiazed)

public List<UnitDTO> getUnitList() {
    return unitList;
}

My bean has following annotations @ManagedBean @ViewScoped

and this is my button:

<p:commandButton id="save" action="#{myController.save}" value="SAVE"  />

Further info, i have noticed that when i press the save button the setter of selectedUnitDTO is never called.

Primefaces Version: 6.0

1
Do you have a Converter for your UnitDTO? Seems like validation and/or conversion fail, so your setter never gets called. - Tomek
No, i do not use a converter. The object type that is retrieved from unitList is the same as the selectedUnitDTO i want to set. - Stephan
Put an h:messages in your page and you'll most likely see an error (most likely in the log as well). You need a converter in this case. The itemValue is a String when it is posted back to the server, not an object. Try with a plain jsf selectOneMenu, same problem! - Kukeltje

1 Answers

2
votes

JSF require converters when you use objects, in this case, for p:selectonemenu

You can add the next java class:

import java.util.List;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter("converterUnitDTO")
public class ConverterUnitDTO implements Converter {

    @Override
    public UnitDTO getAsObject(FacesContext context, UIComponent component, String value) {
        List<UnitDTO> unitsDTO = (List<UnitDTO>) context.getApplication().evaluateExpressionGet(context, "#{myController.unitList}", List.class);
        for (UnitDTO unitDTO : unitsDTO)
            if (unitDTO.toString().equals(value))
                return unitDTO;
        return null;
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        try {
            return value.toString();
        } catch (Exception e) {
            return "";
        }
    }

}

In your xhtml change to this:

<p:selectOneMenu id="unitList"
    value="#{myController.userDTO.selectedUnitDTO}" converter="converterUnitDTO">
    <f:selectItems value="#{myController.unitList}" var="unitList"
        itemValue="#{unitList}" itemLabel="#{unitList.name}" />
</p:selectOneMenu>