4 Replies Latest reply on Mar 2, 2007 10:40 AM by Pete Muir

    Using <s:selectItems> with a list of entities

    Joshua Davis Expert

      I'm passing a list of entities to <s:selectItems> and it looks like the createSelectItems() method is using using the toString() method on my entity in order to convert it to a value for the HTML rendering.

      It looks like what is happening is that the NoSelectionConverter is first in the ConverterChain. This converter is returning the toString() value of the entity which causes ConverterChain.getAsString() to stop processing and return the string to
      org.apache.myfaces.shared_impl.renderkit.RenderUtils.getConvertedStringValue(). The renderer then puts the toString() of the entity in the value of the option tag.

      EntityConverter doesn't like this string when it comes back in the post, of course.

      Has anyone seen this before? It looks like UISelectItems just doesn't work right with noSelectionLabel and an EntityConverter.



        • 1. Re: Using <s:selectItems> with a list of entities
          Joshua Davis Expert

          Here's the view code:

           <h:selectOneMenu id="theSelect" value="#{myBean.selectedUser}">
           <s:selectItems value="#{myBean.users}" var="user"
           noSelectionLabel="Select a user..." label="#{user.name}"/>
           <ec:convertEntity/>
           </h:selectOneMenu>
          


          Here's the SLSB:
          @Name("myBean")
          @Stateful
          @Logging
          public class TestBean implements TestLocal
          {
           @PersistenceContext
           private EntityManager em;
          
           private List<User> users;
          
           private User selectedUser;
          
           public List<User> getUsers()
           {
           if (users == null)
           {
           Query q = em.createQuery("select t from User t join t.roles r where r = :role");
           q.setParameter("role", Role.SPECIAL_ROLE);
           //noinspection unchecked
           users = q.getResultList();
           }
           return users;
           }
          
           public User getSelectedUser()
           {
           return selectedUser;
           }
          
           public void setSelectedUser(User selectedUser)
           {
           this.selectedUser = selectedUser;
           }
           @Destroy
           @Remove
           public void destroy()
           {
           }
          }
          


          I'm using Seam 1.1.6 and EntityConverter 0.1.

          The problem happens when rendering the <h:selectOneMenu> for the first time. I can see with the debugger that the ConverterChain has two converters in it: the NoSelectionConverter and the EntityConverter. The EntityConverter never get's called for the list of Users when rendering.


          • 2. Re: Using <s:selectItems> with a list of entities
            Joshua Davis Expert

            So anyway... the end behavior is that I get:

            Invalid selection. Selected item cannot be loaded from persistence context



            When the form is submitted because the value in the option is the toString() of the User entity, which the EntityConverter doesn't understand.

            • 3. Re: Using <s:selectItems> with a list of entities
              Joshua Davis Expert

              Okay, this looks like some sort of incompatibility with <s:selectItems> and <ec:entityConverter>. The NoSelectionConverter is effectively overriding the EntityConverter in the ConverterChain.

              I was able to work around the problem by using my own bean that generates SelectIems with labels and converts the select item values back into the entity objects (similar to http://wiki.jboss.org/wiki/Wiki.jsp?page=SelectItems).

              But... this isn't as nice as the EntityConverter, IMO.

              Should I not be using EntityConverter? Is this a known issue?

              • 4. Re: Using <s:selectItems> with a list of entities
                Pete Muir Master

                This is fixed in 1.2.0