4 Replies Latest reply on Dec 23, 2011 3:58 AM by vata2999

    display selected item on selectonelistbox using s:convertEntity

    jseanjensen

      I'm trying to solve this problem before I try to fit it into my app.  I've got an object (axe) and I've got an owner (axeOwner) I want the object to be associated with an owner.  All is well until I try to render the selectonelistbox that correctly displays the current owner for the axe.  I can populate the list using the following snippet.




      <s:decorate id="ownerField" template="layout/edit.xhtml">
          <ui:define name="label">Owner</ui:define>
          <h:selectOneListbox 
                id="owner" 
                value="#{axeHome.instance.owner}"
                size="1" >
                                     
           <s:selectItems  
                      value="#{axeOwnerList.resultList}" 
                      noSelectionLabel="Select an Owner"
                itemValue="#{axeOwner}" 
                      var="axeOwner" 
                      label="#{axeOwner.name}" 
                      hideNoSelectionLabel="false"/>
      
           <s:convertEntity/>
          </h:selectOneListbox>



      However I cannot get it to display the already selected item as a default in the list.  The convert entity tag helps get the correct display and allows the user to select an owner just fine.  Is there a technique that I can use to display the selected value on render?




        • 1. Re: display selected item on selectonelistbox using s:convertEntity
          jseanjensen

          I've come up with a solution to my problem. . . sort of.  I set the noSelectionLabel to be the instance owner name which gives me the display output that I need but seems to be a hack.  Then my problem turns into how to render the same page to create a new axe but not be forced to select an owner.  I ended up creating two listboxes and conditionally rendering them so that I can render a selection when there is one but allow the user to create a new axe without picking an owner.  This is how I did it.





          <s:decorate id="ownerField" template="layout/edit.xhtml">
               <ui:define name="label">Owner</ui:define>
               <h:selectOneListbox rendered="#{axeHome.instance.owner ne null}"
                    id="editOwner" 
                    value="#{axeHome.instance.owner}"
                    size="1">
                    <s:selectItems  
                         noSelectionLabel="#{axeHome.instance.owner.name}"
                         hideNoSelectionLabel="false"
                         var="axeOwner" 
                         value="#{axeOwnerList.resultList}" 
                         itemValue="#{axeOwner}"
                         label="#{axeOwner.name}" 
                         />
                         <s:convertEntity/>
                    
               </h:selectOneListbox>
               
               <h:selectOneListbox rendered="#{axeHome.instance.owner eq null}"
                    id="addOwner" 
                    value="#{axeHome.instance.owner}"
                    size="1">
                    <s:selectItems  
                         noSelectionLabel="Select Owner"
                         hideNoSelectionLabel="false"
                         var="axeOwner" 
                         value="#{axeOwnerList.resultList}" 
                         itemValue="#{axeOwner}"
                         label="#{axeOwner.name}" 
                         />
                         <s:convertEntity/>
                    
               </h:selectOneListbox>
          </s:decorate>
          



          The axe and axeOwner classes are simple entities:



          @Entity
          public class Axe implements Serializable
          {
              private Long id;
              private Integer version;
              private String name;
              private String history;
              private AxeOwner owner;
              ... getters and setters ...
          }
          
          @Entity
          public class AxeOwner implements Serializable
          {
              private Long id;
              private Integer version;
              private String name;
              ... getters and setters ...
          }



          This solution may work in my application but it seems like a hack.  It seems strange that the select attribute of the list items isn't triggered.  I was hoping someone would explain this to me but I had to figure it out for myself.  Is there anywhere that is more active for this sort of thing than this forum?

          • 2. Re: display selected item on selectonelistbox using s:convertEntity
            mikkus70

            Don't use the itemValue property of the s:selectItems tag, s:convertEntity should handle that for you (if you put #{axeOwner} there, it should evaluate it to the class toString() method, which is not what s:convertEntity expects.


            See the documentation section 33.1.2.2 which gives an example of how s:convertEntity should be used. Note the usage example given:


            <h:selectOneMenu value="#{person.continent}" required="true">
              <s:selectItems value="#{continents.resultList}" var="continent" 
                              label="#{continent.name}" 
                              noSelectionLabel="Please Select..."/>
               <s:convertEntity />
            </h:selectOneMenu>

            • 3. Re: display selected item on selectonelistbox using s:convertEntity
              ahmeric

              Emir is right. but this is not the solution for this problem.
              I have the same trouble. and I couldn't find any solution except Sean did.


              @Sean , have you found any new solution for this stituation.


              thnx.

              • 4. Re: display selected item on selectonelistbox using s:convertEntity
                vata2999

                have u tried to begin conversation in order to set axOwner it perfectly works for me



                AxOwnerList.java


                @Override 
                @Begin(join=true)
                public List getResultList(){
                
                //whatever
                
                }