开发者

selecting item from primeface's selectOneMenu not working

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<Dom开发者_如何学Caines> initDomaines() {
       return domainesService.getAllDomaines();
   }  
}


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".


For the most cases you can do it without an converter, but this is not wrong.

Here is an example:

<h:selectOneMenu value="#{bean.selectedObject.id}">
    <f:selectItems value="#{bean.listOfObjects}" var="item" itemLabel="#{item.title}" itemValue="#{item.id}" />
</h:selectOneMenu>

Another idea is to use the hashcode instead of the ID (but in both: itemValue and value).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜