7 Replies Latest reply on Jul 12, 2007 6:06 AM by pmuir

    selectOneMenu and selectItems problem

      Hi I can't see value of selected item in my backed bean which is Stateful bean.
      I can see values of selectOneMenu many component but there is no selected
      values passed into backed bean after submit.

      seam 1.2.1
      jboss 4.0.5


      <rich:panel rendered="#{CandidateAction.newShortListStep == 1}">
       <f:facet name="header">
       <h:outputText value="#{messages['shortlist.add_new']}" />
       </f:facet>
      
       <h:selectOneMenu id="selectedJobType" value="#{CandidateAction.selectedCompany}" converter="CompanyConverter"
       onchange="submit()">
       <s:selectItems value="#{CandidateAction.companies}" var="cmp" label="#{cmp.name}"
       oSelectionLabel="#{messages['select.company']}" />
       <!--
       <a4j:support event="onchange" reRender="cpos" ajaxSingle="true" />
       -->
       </h:selectOneMenu>
      
      


      and bean


      
      @Stateful
      @Scope(ScopeType.CONVERSATION)
      @Name("CandidateAction")
      public class CandidateAction extends BaseAction implements ICandidate {
      
       ...
      
       private Company selectedCompany;
      
       ...
      
       public Company getSelectedCompany() {
       return selectedCompany;
       }
      
       public void setSelectedCompany(Company selectedCompany) {
       this.selectedCompany = selectedCompany;
       }
      
      
      }
      
      



      selectedCompany is always NULL (Method setSelectedCompany is not called). Bot methods are defined in ICandidate interface.




      Thanks for help,

      Mttu





        • 1. Re: selectOneMenu and selectItems problem
          pmuir

          Any output from h:messages?

          • 2. Re: selectOneMenu and selectItems problem

             

            "pete.muir@jboss.org" wrote:
            Any output from h:messages?



            Hi,
            message from h:messages is:

            "value is not valid"

            but why ?

            the Converter class:

            @Name("CompanyConverter")
            @Converter
            public class CompanyConverter implements javax.faces.convert.Converter, Serializable {
            
             @Logger
             private Log log;
            
             @In(required = false)
             List<Company> companies;
            
             /*
             * @see javax.faces.convert.Converter#getAsObject(javax.faces.context.FacesContext,
             * javax.faces.component.UIComponent, java.lang.String)
             */
             public Object getAsObject(FacesContext arg0, UIComponent arg1, String string) throws ConverterException {
             if (string == null || string.length() == 0) {
             return null;
             }
            
             long id = Long.valueOf(string).longValue();
             log.info("[getAsObject] string -> " + string);
             log.info("[getAsObject] id -> " + id);
             log.info("[getAsObject] companies -> " + companies);
             if (companies != null) {
             for (Company referenceData : companies) {
             if (referenceData.getId() == id) {
             log.info("[getAsObject] returning -> " + referenceData);
             return referenceData;
             }
             }
             }
             log.info("[getAsObject] returning -> null" );
             return null;
             }
            
             /*
             * @see javax.faces.convert.Converter#getAsString(javax.faces.context.FacesContext,
             * javax.faces.component.UIComponent, java.lang.Object)
             */
             public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) throws ConverterException {
             log.info("[getAsString] obj -> " + obj);
             if (obj == null) {
             return null;
             }
             Company company = (Company) obj;
             log.info("[getAsString] company -> " + company);
             return String.valueOf(company.getId());
             }
            
            }
            


            Both methods return not null values.

            Any idea ?

            Thanks,
            Mttu


            • 3. Re: selectOneMenu and selectItems problem
              milli

              If you are converting ID from long to string and vice versa, why don't you try <s:convertEntity />. But you would still get the same error unless you override equals in your Company class to equate id's.

              • 4. Re: selectOneMenu and selectItems problem

                OK, it started to worked after i had changed:

                @Out
                private Company selectedCompany;
                


                and implemented equals in Company.

                It's still not clear to me why selectedCompany should be outjected in this case.

                • 5. Re: selectOneMenu and selectItems problem
                  trickyvail

                  If your Company object is an entity you need to be aware of:

                  http://hibernate.org/109.html

                  • 6. Re: selectOneMenu and selectItems problem

                    But why selected company has to be outjected ?

                    • 7. Re: selectOneMenu and selectItems problem
                      pmuir

                      It probably subtley altered the way in which your variables were scoped. The way you have written your code doesn't require it. milli is correct - you need to make sure the selected object appears in the original list, otherwise validation fails.