7 Replies Latest reply on Nov 18, 2009 6:18 PM by notify

    h:selectOneMenu - value is not valid

    notify

      After my problems with EntityConverter see previous post, with no replies. I wrote my own Converter. Now I'm getting value is not valid.


      Have tried every suggestion I can find (Googled for days!), over riding equals and returning true. Still I get the error.


      I put a debug statement in my entity's equals method, but it doesn't appear to get called. Why?


      Also investigated persistence context, have searched my files and only appear to have one declared, should I be looking any config files.


      Thanks.

        • 1. Re: h:selectOneMenu - value is not valid
          kragoth

          Please post your relevant code.

          • 2. Re: h:selectOneMenu - value is not valid
            notify

            Tim Evers wrote on Nov 18, 2009 00:17:


            Please post your relevant code.


            Been there, done that. However;


            <h:outputLabel for="title">Title</h:outputLabel>
                <h:selectOneMenu value="#{userTitles.title}" id="title" converter="titleConverter" required="true" >
                    <s:selectItems value="#{userTitlesList}" var="userTitles" label="#{userTitles.title}" />
                </h:selectOneMenu> 
                <rich:message for="title">
                </rich:message>
            



            Relevent code in EJB where userTitlesList is obtained from;


            @SuppressWarnings("unchecked")
            @Begin
            @Factory("userTitlesList")  
            public final List <UserTitles> getUserTitles() {
                    
                final Query query = entityManager.createQuery(GET_USER_TITLES);
                userTitlesList = query.getResultList();
                            
                log.info(">>>>> getUserTitles userTitlesList size    = " + userTitlesList.size());
                return userTitlesList;
            }
            



            Entity Bean with equals override;


            @Entity
            @Table(name = "user_titles", catalog = "notifypinpoint")
            @Name("userTitles")
            @Scope(ScopeType.CONVERSATION)
            public class UserTitles implements Serializable {
            
                    /**
                     * 
                     */
                    private static final long serialVersionUID = 1L;
                    
                    /**
                     * Attribute titleOrder.
                     */
                    private Integer titleOrder;
                    
                    /**
                     * Attribute title.
                     */
                    private String title;
                    
                    
                    /**
                     * <p> 
                     * </p>
                     * @return titleOrder
                     */
                    @Basic
                    @Id
                    @Column(name = "TITLE_ORDER")
                            public Integer getTitleOrder() {
                            return titleOrder;
                    }
            
                    /**
                     * @param titleOrder new value for titleOrder 
                     */
                    public void setTitleOrder(Integer titleOrder) {
                            this.titleOrder = titleOrder;
                    }
                    
                    /**
                     * <p> 
                     * </p>
                     * @return title
                     */
                    @Basic
                    @Column(name = "TITLE", length = 5)
                    public String getTitle() {
                            return title;
                    }
            
                    /**
                     * @param title new value for title 
                     */
                    public void setTitle(String title) {
                            this.title = title;
                    }
                    
                    /**
                     * calculate hashcode
                     */
                    @Override
                    public int hashCode() {
                            return super.hashCode();
                    }
            
                    /**
                     * equals method
                     * 
                     * @param object
                     */
                    @Override
                    public boolean equals(Object object) {
                            System.out.println(">>>>> UserTitles object = " + object.toString());
                            //return super.equals(object);
                            return true;
                    }
            }
            



            I enter the user information and add user by calling EJB;


            <div class="actionButtons">
                <h:commandButton id="save"
                    value="Save"
                    action="#{usersBean.addUser}"/>
            </div>
            



            addUser method in EJB;


            @Begin
            public final boolean addUser() {
                // Persistence code. 
            }
            

            • 3. Re: h:selectOneMenu - value is not valid
              notify

              Title converter;


              @Name("titleConverter")
              @Converter(forClass = UserTitles.class)
              @Scope(ScopeType.CONVERSATION)
              @BypassInterceptors
              public class TitleConverter implements javax.faces.convert.Converter {
              
                   public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
                        //System.out.println(">>>>> getAsObject value = " + value);
              
                        // EntityManager entityManager = (EntityManager)
                        // Component.getInstance("entityManager");
              
                        if (value != null) {
                             // try {
                             // Integer titleOrder = Integer.parseInt(value);
                             // if (titleOrder != null) {
                             // return entityManager.find(UserTitles.class, titleOrder);
                             // }
                             // } catch (NumberFormatException e) {
                             // }
                             System.out.println("***** getAsObject return = " + value);
                             Object o = (Object) value;
                             return o;
                        }
                        return null;
              
                   }
              
                   public String getAsString(FacesContext context, UIComponent component, Object value) throws ConverterException {
                        //System.out.println(">>>>> getAsString value = " + value);
                        // System.out.println(">>>>> getAsString value instanceof UserTitles= "
                        // + (value instanceof UserTitles));
              
                        if (value instanceof UserTitles) {
                             UserTitles userTitles = (UserTitles) value;
                             
                             System.out.println(">>>>> getAsString return = " + userTitles.getTitle());
                             
                             return userTitles.getTitle().trim();
                        } else {
                             return null;
                        }
                   }
              }
              

              • 4. Re: h:selectOneMenu - value is not valid
                natemccallum

                Hi Roger, I've very new here and am having an issue with selectonemenus also, so your post caught my eye.  I was having this problem to and I think they @override is what solved it for me.  I see you have done that though so I looked a little closer.  The only difference I see from my stuff is that I use the @Out annotation on my variable for the selectOneMenu's value attribute.  In my reading it appears the @Out annotation provides the variable to the webpage and the @In annotation provides from the webpage to the bean.  If my understanding is wrong let me know where.

                • 5. Re: h:selectOneMenu - value is not valid
                  notify

                  Nate,


                  In the EJB where I populate list for the dropdown I have an @Out annotation;


                  @SuppressWarnings("unused")
                  @Out (required = false)
                  private List <UserTitles> userTitlesList;
                  



                  That contains the entity UserTitles, which maps to the table where I get my titles from.


                  Where are you suggesting I need the @Out?


                  Thanks.


                  • 6. Re: h:selectOneMenu - value is not valid
                    natemccallum

                    I probably have some Non-Best practices code here, but here is my xhtml for the selectOneMenu and snippets from my Action EJB also.


                    I use the @DataModel annotation for the list.
                    Then use the @Out and @In annotation for the selected value.


                    Both my value for the selectOneMenu (selectedEnvironment) and my value for the selectItems (environList) are defined in my Action EJB.  It looks to me like your value for selectOneMenu (userTitles.title) is referencing the @entity class userTitles directly and not an instanceof the class.




                    XHTML
                    <h:selectOneMenu id="environList" value="#{selectedEnvironment}" >
                        <s:selectItems value="#{environList}" var="environment"
                                       label="#{environment.environ_name}"
                                       noSelectionLabel="Please Select..."/>
                        <s:convertEntity />
                        <a4j:support event="onchange" action="#{selectionmanagerimpl.getServerList}" reRender="serverList"/>
                        <rich:message for="environList" />
                    </h:selectOneMenu>
                    



                    EJB
                    @DataModel
                    private List<Environment> environList;
                    
                    @Out(required = false)
                    @In(required = false)
                    private Environment selectedEnvironment;
                    
                    



                    I suggest removing the @Out from the List and replacing with @DataModel.


                    Then define a selectedUserTitle variable in the Action EJB of type UserTitles decorated with the @Out and @In annotations and set the value attribute of the selectOneMenu to the selectedUserTitle.


                    Perhaps I followed a bad example but this seems to be working for me.


                    • 7. Re: h:selectOneMenu - value is not valid
                      notify

                      Many thanks for that it solved it!


                      Had used the @DataModel else where for iterating through a list and displaying as table.


                      Regards,


                      Roger