9 Replies Latest reply on Dec 3, 2007 8:54 AM by Sheng Huang

    Converter Issue

    Kishore R Newbie

      I have two entities, Sheet and Category. A Sheet can belong to multiple Categories.

      So, I have in my Sheet.java

      
       @ManyToMany(mappedBy = "sheets")
       private List<Category> categories = new ArrayList<Category>();
      
      


      In my create Sheet page
       <h:outputText value="Category : " />
       <h:selectManyListbox id="categoryList" value="#{sheet.categories}" required="true" converter="#{entityConverter}">
       <s:selectItems value="#{categoryAction.rootCategories}" id="cats" var="cat" label="#{cat.name}(#{cat.id})" noSelectionLabel="Please Select..."/>
       <!-- <f:selectItems value="#{categoryAction.rootCategories}" />-->
       <s:convertEntity/>
       </h:selectManyListbox>
      


      I am using the selectManyListBox to show the categories from which the user can choose while creating a new Sheet. I am getting the dreaded
      "value is not valid" exception.

      The s:selectItems does not display the id value correctly in the list. It has a positional index as the value for the list item which is not the id for the category object. So, I guess that is the reason why the converter is not able to get the Category object when submitted. So, I changed my xhtml page to the following

      
       <h:outputText value="Category : " />
       <h:selectManyListbox id="categoryList" value="#{sheet.categories}" required="true" converter="#{entityConverter}">
       <f:selectItems value="#{categoryAction.rootCategories}" />
       <s:convertEntity/>
       </h:selectManyListbox>


      Now, I am getting
      Argument Error: An option for component categoryList was not an instance of javax.faces.model.SelectItem. Type found: java.util.ArrayList.

      My components.xml has the following for the converter
       <component name="entityConverter" scope="CONVERSATION" precedence="20"
       class="org.jboss.seam.ui.converter.EntityConverter">
       <property name="entityManager">#{entityManager}</property>
       </component>
      

      Any ideas as to what I am doing wrong.

      Thanks in advance

        • 1. Re: Converter Issue
          Kishore R Newbie

          Going through some other postings I believe that s:selectitems is going to have a "itemValue" attribute soon. Any idea when that is going to be out? In the meanwhile is there any workaround for this issue

          • 2. Re: Converter Issue
            Kishore R Newbie

            It works fine after I added a categoryConverter.

            • 3. Re: Converter Issue
              Pete Muir Master

              In 2.0.1, out very soon now.

              • 4. Re: Converter Issue
                Matthew Lieder Apprentice

                "very soon" likely being ~4 weeks, based on the rate of bugfixing since 2.0.0.GA a month ago :)

                • 5. Re: Converter Issue
                  Sheng Huang Newbie

                  Good afternoon rvkishore,

                  Could you post your categoryConverter so that I can have a temporary workaround also?

                  Thanks a lot,
                  Sheng

                  • 6. Re: Converter Issue
                    Pete Muir Master

                    Probably much less than 4 weeks :) Yes, we are deliberately vague about release schedules so that we can release when we feel ready rather than look stupid when the date slips.

                    But very soon means within a week or two.

                    • 7. Re: Converter Issue
                      Matthew Lieder Apprentice

                      Good to hear! Can't wait to be able to use itemValue, along with all the other changes that have been/will be done :)

                      • 8. Re: Converter Issue
                        Kishore R Newbie

                        xshuang,
                        below is the code for my CategoryConverter.

                        import javax.faces.component.UIComponent;
                        import javax.faces.context.FacesContext;
                        import javax.faces.convert.ConverterException;
                        
                        import org.jboss.seam.Component;
                        import org.jboss.seam.annotations.Name;
                        import org.jboss.seam.annotations.faces.Converter;
                        
                        import com.datalette.actions.CategoryIntf;
                        import com.datalette.model.Category;
                        
                        @Name("categoryConverter")
                        @Converter
                        public class CategoryConverter implements javax.faces.convert.Converter
                        {
                        
                         public Object getAsObject(FacesContext context, UIComponent component,
                         String value) throws ConverterException
                         {
                         Integer i = new Integer(value);
                         CategoryIntf categoryAction = (CategoryIntf)Component.getInstance("categoryAction");
                         return categoryAction.getCategory(i);
                         }
                        
                         public String getAsString(FacesContext context, UIComponent component,
                         Object value) throws ConverterException
                         {
                         return ((Category)value).getId().toString();
                         }
                        }


                        • 9. Re: Converter Issue
                          Sheng Huang Newbie

                          Good morning rvkishore,

                          Thanks a lot for your reply. It seems that reference objects retrieved using <framework:entity-query> and using entityManager.createQuery() are different, possibly due to different memory location.

                          An equals() method of the reference class also helps to solve the "value is invalid" error. Basically it check the Id and returns true if ids of two reference objects are the same.

                          Have a nice day!

                          Best regards,
                          Sheng