selecting item from primeface's selectOneMenu not working

cascadox picture cascadox · Jul 6, 2011 · Viewed 39.4k times · Source

I'm having problem with getting the selected item from a selectOneMenu.
Here is my JSF code:

<h:form id="mainfrm">   
    <h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">  
        <h:outputText value="Basic Usage: " />  
        <p:selectOneMenu id="domaine" value="#{projet.currentDomaines}">  
            <f:selectItem itemLabel="Select One" itemValue="" />  
            <f:selectItems value="#{projet.initDomaines()}"  var="d" itemValue="#{d}" itemLabel="#{d.libelleDomaine}" /> 
            <p:ajax update="formEquipe" process="mainfrm" event="change" />
        </p:selectOneMenu>
     </h:panelGrid>     

</h:form>  

<h:form id="formEquipe">  
    <h:panelGrid id="display" columns="2" cellpadding="4">  
        <f:facet name="header">  
            <p:graphicImage value="/images/cars/xxxx.jpg"/>  
        </f:facet>  

        <h:outputText value="Domaine name :" />  
        <h:outputText value="#{projet.currentDomaines.libelleDomaine}"/>  

        <h:outputText value="Director :" />  
        <h:outputText value="#{projet.currentDomaines.nomDirecteur}" />  
    </h:panelGrid>  
</h:form>

it seems like everything is right but i must be missing something... so i tested by changing the currentDomaines (object type Domaines) by text (String) and it worked, and here is the code :

<h:form id="mainfrm">   
    <h:panelGrid columns="2" style="margin-bottom:10px" cellpadding="5">  
        <h:outputText value="Basic Usage: " />  
        <p:selectOneMenu id="domaine" value="#{projet.text}">  
            <f:selectItem itemLabel="Select One" itemValue="" />  
            <f:selectItems value="#{projet.initDomaines()}"  var="d" itemValue="#{d.libelleDomaine}" itemLabel="#{d.libelleDomaine}" /> 
            <p:ajax update="formEquipe" process="mainfrm" event="change" />
        </p:selectOneMenu>
    </h:panelGrid>     
</h:form>  

<h:form id="formEquipe">  
    <h:panelGrid id="display" columns="2" cellpadding="4">  
        <f:facet name="header">  
        <p:graphicImage value="/images/cars/xxxx.jpg"/>  
        </f:facet>  

        <h:outputText value="Domaine name :" />  
        <h:outputText value="#{projet.text/>  
    </h:panelGrid>  
</h:form>

and here is my backing bean:

public class ProjetsBean implements Serializable {

   private  DomainesService domainesService;

   private Domaines currentDomaines;
   private String text;


   /////////////// setters & getters \\\\\\\\\\\\\\\\\\\
   public void setCurrentDomaines(Domaines currentDomaines) {
       this.currentDomaines=currentDomaines;
   }
   public Domaines getCurrentDomaines() {
       return currentDomaines;
   }

   public void setText(String text) {
       this.text=text;
   }
   public Integer getText() {
       return text;
   }

   ///////////////// Méthodes  \\\\\\\\\\\\\\\
   @PostConstruct   
   public List<Domaines> initDomaines() {
       return domainesService.getAllDomaines();
   }  
}

Answer

Matt Handy picture Matt Handy · Jul 6, 2011

The selection from a html selectbox will always be returned to the server as string. If you want to use objects in h:selectOneMenu you need a converter.

There is a comprehensive tutorial on that topic: "Objects in h:selectOneMenu".