6 Replies Latest reply on Mar 25, 2011 7:41 AM by angela

    issues getting the selected value from a selectOneMenu

    angela

      good morning!!!


      I am quite new in this community and excuse me if my english is  little incorrect
      I'm facing some issues for a while know concerning a selected value from the selectOneMenu.
      Actually, what I'm trying to do is to create a new user for my application and each user must have a specific profil. so ,for a simple use, I made a drop down list from the profil datatable.
      In the user datatable, I have idprofil which is a foreign key that references the idprofil in the profil datatable. I hope that was a little clear...
      The problem is that i can't find out how to get the value of the profil selected and save it.


      here is some parts of the code:


      UserAdd.xhtml




      <s:decorate id="emailField" template="layout/edit.xhtml">
                      <ui:define name="label">Email</ui:define>
                      <h:inputText id="email"
                             required="true"
                                 size="45"
                            maxlength="45"
                                value="#{utilisateurHome.instance.email}">
                          <a:support event="onblur" reRender="emailField" bypassUpdates="true" ajaxSingle="true"/>
                      </h:inputText>
                  </s:decorate>
                  
                  
                  <s:decorate id="profilField" template="layout/edit.xhtml">
                  <ui:define name="label">Profil</ui:define>
                          <h:selectOneMenu value="#{selectedProfil}" id="profil" required="true">
                              <s:selectItems value="#{profilToTrack}" 
                                 var="_profil" 
                                 label="#{_profil.profil}"
                               
                                 noSelectionLabel="Please Select ..." />
                                 <s:convertEntity />
                                 <a:support event="onchange" action="#{profilAction.test()}" reRender="profilField" />
                      
                              </h:selectOneMenu> 
                  </s:decorate>






      ProfilActionBean.java




      @Scope(ScopeType.CONVERSATION)
      @Name("profilAction")
      public class ProfilActionBean implements ProfilAction, Serializable {
      
              /**
               * 
               */
              private static final long serialVersionUID = -3853383063011157886L;
              @In(create = true)
              private ProfilList profilList;
              @Logger private Log log;
      
              @DataModel
              private List<Profil> profilToTrack = new ArrayList<Profil>();
      
              @Out(required = false)
              @In(required = false)
              private Profil selectedProfil = new Profil();
      
              @Factory("profilToTrack")
              public List<Profil> initProfilToTrack() {
                      profilToTrack = profilList.getResultList();
                      System.out.println("initProfilToTrack");
                      return profilToTrack;
              }
      
              
              public Profil getSelectedProfil() {
                      log.info(">>>>> get selectedPerson");
      
                      if (this.selectedProfil != null) {
                              log.info(">>>>> get getSelectedProfil selectedProfil = "
                                              + this.selectedProfil.getProfil());
                      }
                      return this.selectedProfil;
              }
              
      
              public void setSelectedProfil(Profil profil) {
                      this.selectedProfil = profil;
              }
      
      
              public void test() {
                      System.out.println("test");
                      System.out.println(selectedProfil == null ? "null" : selectedProfil.getIdprofil());
              }
      }
      
      




      PS: the test method works fine, when I choose a value from the dropdown list i get the correct Idprofil


      I'm ready for any further explanation
      please if someone can help I will be very thankful :)

        • 1. Re: issues getting the selected value from a selectOneMenu
          antibrumm.mfrey0.bluewin.ch

          Hi
          I've not used in / outjection alot for these cases. Can you try just this?




           <h:selectOneMenu value="#{profilAction.selectedProfil}" id="profil" required="true">




          • 2. Re: issues getting the selected value from a selectOneMenu
            angela

            Thank you for ur answer!
            I tried it but didn't work!when I actually use profilAction.selectedProfil I get an empty drop down list.


            could the problem be related to the userAdd.page.xml???

            • 3. Re: issues getting the selected value from a selectOneMenu
              angela

              my be this will be useful...


              when I first used the seam generated entities, as the idprofil is a foreign key in the utilisateur Entity, to create the user, seam proposed a rich tab wich redirect to the profil list, choose the profil and returned the profilid as a parameter...


              here is the code if the original userAdd.xhtml




              <ui:define name="body">
              
                  <h:form id="utilisateur" styleClass="edit">
              
                      <rich:panel>
                          <f:facet name="header">Add User</f:facet>
              
              
              
              
                          <s:decorate id="nomField" template="layout/edit.xhtml">
                              <ui:define name="label">Nom</ui:define>
                              <h:inputText id="nom"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.nom}">
                                  <a:support event="onblur" reRender="nomField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
              
              
                          <s:decorate id="prenomField" template="layout/edit.xhtml">
                              <ui:define name="label">Prenom</ui:define>
                              <h:inputText id="prenom"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.prenom}">
                                  <a:support event="onblur" reRender="prenomField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
              
              
                          <s:decorate id="fonctionField" template="layout/edit.xhtml">
                              <ui:define name="label">Fonction</ui:define>
                              <h:inputText id="fonction"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.fonction}">
                                  <a:support event="onblur" reRender="fonctionField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
              
              
                          <s:decorate id="loginField" template="layout/edit.xhtml" >
                              <ui:define name="label">Login</ui:define>
                              <h:inputText id="login"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.login}">
                                  <a:support event="onblur" reRender="loginField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
              
              
                          <s:decorate id="passwordField" template="layout/edit.xhtml">
                              <ui:define name="label">Password</ui:define>
                              <h:inputText id="password"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.password}" >
                                  <a:support event="onblur" reRender="passwordField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
              
              
                          <s:decorate id="emailField" template="layout/edit.xhtml">
                              <ui:define name="label">Email</ui:define>
                              <h:inputText id="email"
                                     required="true"
                                         size="45"
                                    maxlength="45"
                                        value="#{utilisateurHome.instance.email}">
                                  <a:support event="onblur" reRender="emailField" bypassUpdates="true" ajaxSingle="true"/>
                              </h:inputText>
                          </s:decorate>
                          <div style="clear:both">
                              <span class="required">*</span>
                              required fields
                          </div>
              
                      </rich:panel>
              
                      <div class="actionButtons">
              
                          <h:commandButton id="save"
                                        value="Save"
                                       action="#{utilisateurHome.persist}"
                                    
                                     rendered="#{!utilisateurHome.managed}"/>
              
                          <h:commandButton id="update"
                                        value="Save"
                                       action="#{utilisateurHome.update}"
                                     rendered="#{utilisateurHome.managed}"/>
              
                          <h:commandButton id="delete"
                                        value="Delete"
                                       action="#{utilisateurHome.remove}"
                                    immediate="true"
                                     rendered="#{utilisateurHome.managed} "/>
              
                          <s:button id="cancelEdit"
                                 value="Cancel"
                           propagation="end"
                                  view="/Utilisateur.xhtml"
                              rendered="#{utilisateurHome.managed}"/>
              
                          <s:button id="cancelAdd"
                                 value="Cancel"
                           propagation="end"
                                  view="/#{empty utilisateurFrom ? 'UtilisateurList' : utilisateurFrom}.xhtml"
                              rendered="#{!utilisateurHome.managed}"/>
              
                      </div>
                  </h:form>
              
              <rich:tabPanel switchType="ajax">
              
                  <rich:tab label="Profil *" labelClass="required">
                  <div class="association" id="profilParent">
              
                      <h:outputText value="There is no profil associated with this utilisateur."
                                 rendered="#{utilisateurHome.instance.profil == null}"/>
              
                      <rich:dataTable var="_profil"
                                 value="#{utilisateurHome.instance.profil}"
                              rendered="#{utilisateurHome.instance.profil != null}"
                            rowClasses="rvgRowOne,rvgRowTwo"
                                    id="profilTable">
                          <h:column>
                              <f:facet name="header">Profil</f:facet>
                              <h:outputText value="#{_profil.profil}"/>
                          </h:column>
                      </rich:dataTable>
              
                      <div class="actionButtons">
                          <s:button id="#{utilisateurHome.instance.profil != null ? 'changeParent' : 'selectParent'}" value="#{utilisateurHome.instance.profil != null ? 'Change' : 'Select'} profil"
                                     view="/ProfilList.xhtml">
                              <f:param name="from" value="UtilisateurEdit"/>
                          </s:button>
                      </div>
              
                  </div>
                  </rich:tab> 
              </rich:tabPanel>
              </ui:define>
              
              </ui:composition>



              and a part of the userAdd.page.xml




                <begin-conversation join="true" flush-mode="MANUAL"/>
              
                 <action execute="#{utilisateurHome.wire}"/>
              
                 <param name="utilisateurFrom"/>
                 <param name="utilisateurIdutilisateur" value="#{utilisateurHome.utilisateurIdutilisateur}"/>
                 <param name="profilFrom"/>
                 <param name="profilIdprofil" value="#{profilHome.profilIdprofil}"/>
              
              
              
                 <navigation from-action="#{utilisateurHome.persist}">
                    <rule if-outcome="persisted">
                       <end-conversation/>
                       <redirect view-id="/Utilisateur.xhtml"/>
                    </rule>
                 </navigation>



              • 4. Re: issues getting the selected value from a selectOneMenu
                antibrumm.mfrey0.bluewin.ch

                Hi again.


                I'm quite busy atm because of a project deadline some my answers are quite short :)


                Did you use the selectitems tag inside the menu? If not it will just not have values.


                One of my main rules in working with seam (or even globally) is that if you have something not working drill the problem down to its pieces.


                In your case create a simple page containing only a basic jsf combobox with h selectitems. Do you get the values in your component? Good, use s selectitems from a factory returning a list of selectitem classes. Check. And so on...


                Hope this helps

                • 5. Re: issues getting the selected value from a selectOneMenu
                  angela

                  Good morning Martin!
                  thanks a lot for your time and help!


                  I think that the problem is not in my jsf page but probably in my UserHome or ProfilHome. May be I need to add a new method or override an existing one but I don't know what exactly.


                  if you have any ideas please...I'm running out of time
                  anyway I really appreciate your help

                  • 6. Re: issues getting the selected value from a selectOneMenu
                    angela

                    heeey Martin I finally solved the problem


                    In my ProfilActionBean, I just needed to add a variable from ProfilHome:



                    @In(create = true)
                    ProfilHome profilHome;





                    and in my test method I added a this line:




                    profilHome.setProfilIdprofil(selectedProfil.getIdprofil());





                    and this is my dropdown list:




                    <h:selectOneMenu value="#{selectedProfil}" id="selectedProfil">
                                            <s:selectItems value="#{profilToTrack}" 
                                               var="_profil" 
                                               label="#{_profil.profil}"
                                               noSelectionLabel="Please Select ..." />
                                            <s:convertEntity/> 
                                               <a:support event="onchange" action="#{profilAction.test()}" reRender="profilField" />
                                     </h:selectOneMenu> 



                    and I have this code in my UserAdd.page.xml:




                    <page xmlns="http://jboss.com/products/seam/pages"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.2.xsd"
                          no-conversation-view-id="/UtilisateurAdd.xhtml"
                          login-required="true">
                    
                       <begin-conversation join="true" flush-mode="MANUAL"/>
                    
                       <action execute="#{utilisateurHome.wire}"/>
                    
                       <param name="utilisateurFrom"/>
                       <param name="utilisateurIdutilisateur" value="#{utilisateurHome.utilisateurIdutilisateur}"/>
                       <param name="profilFrom"/>
                       <param name="profilIdprofil" value="#{profilHome.profilIdprofil}"/>
                    
                    
                    
                       <navigation from-action="#{utilisateurHome.persist}">
                          <rule if-outcome="persisted">
                             <end-conversation/>
                             <redirect view-id="/Utilisateur.xhtml"/>
                          </rule>
                       </navigation>
                    </page>



                    Hope that this will be useful for someone else


                    thanks again Martin