9 Replies Latest reply on Jan 15, 2008 3:10 AM by nickarls

    No selected element from <h:selectOneListbox>

    haefti

      Hi!
      I have a problem to get the selected element from a select box.
      I would like to have a list of items and a button which leads to a page where I can edit the values of the selected element.

      <h:selectOneListbox size="10">
       <s:selectItems value="#{videos}" var="video" label="#{video.name}" />
       <s:convertEntity />
      </h:selectOneListbox>
      
      <h:commandButton type="submit" action="#{manager.editVideo()}" value="Change"></h:commandButton>
      


      If I do not choose anything and press the button the first element is chosen (I can see the id of the selected item in the log) but if I choose an element the method manager.editVideo() is not called and the page is rendered again.
      (I know someone reported this kind of problem here in the forum but I can't find the posting any more.)


      My second problem is how to go on with it to come to the edit page.

      I have an edit.xhtml where I can edit the values of each item (video) which works pretty good if I use a data table with a link and the id as parameter instead of the select box above..
      How do I do this from the select box? Is it right that I need the editVideo() method as action for the command button even though the method does nothing but logging the id of the selected element at the moment?

      Is this code from my pages.xml already wrong?
      <page view-id="/edit.xhtml" action="#{manager.editVideo}"/>
      


      Thanks for your help!

      Haefti

        • 1. Re: No selected element from <h:selectOneListbox>
          jmodadams

          I believe the problem here is that you have not specified a value attribute in the h:selectOneListbox tag, which is how you bind the selected value to your backing bean.

          If you were to add a video property to your manager bean with a public setter method, you could then do the following:

          <h:selectOneListbox size="10" value="#{manager.video}">
           <s:selectItems value="#{videos}" var="video" label="#{video.name}" />
           <s:convertEntity />
          </h:selectOneListbox>
          
          <h:commandButton type="submit" action="#{manager.editVideo()}" value="Change"></h:commandButton>
          


          Also, make sure that the listbox and commandButton components are wrapped inside of an <h:form> tag or the values will not be posted to the server upon submission.

          • 2. Re: No selected element from <h:selectOneListbox>
            haefti

            Hi!
            First of all thanks for the answer!

            "jmodadams" wrote:
            I believe the problem here is that you have not specified a value attribute in the h:selectOneListbox tag, which is how you bind the selected value to your backing bean.

            If you were to add a video property to your manager bean with a public setter method, you could then do the following:

            Okay, I thought I could inject it via @In or @DataModelSelection or something similar.

            My managerAction now looks like this:
            @Stateless
            @Name("manager")
            public class ManagerAction implements Manager{
            
             @Logger
             Log log;
            
             private Video selVideo;
            
             public Video getselVideo() {
             return selVideo;
             }
            
             public void setSelVideo(Video selVideo) {
             this.selVideo = selVideo;
             }
            
             public String editVideo () {
             log.info("selected video Id: " + selVideo.getVideoId());
             return "edit";
             }
            .
            .
            .
            }
            

            the hello.xhtml contains this code:
            <h:form>
            
            <h:selectOneListbox size="10" value="#{manager.selVideo}">
             <s:selectItems value="#{videos}" var="video" disabled="#{video.videoId < 25000 ? true : false}"
             label="(#{video.datum}) #{video.videoId} - #{video.thema}: #{video.headline}" />
             <s:convertEntity />
            </h:selectOneListbox>
            
            <h:commandButton type="submit" action="#{manager.editVideo()}" value="Ändern"></h:commandButton>
            
            </h:form>
            

            and I added this to my pages.xml:
             <page view-id="/hello.xhtml">
            
             <navigation from-action="#{manager.editVideo}">
             <rule if-outcome="edit">
             <redirect view-id="/edit.xhtml"/>
             </rule>
             </navigation>
            
             </page>
            

            but if I choose an item from the list and press the button I still get back to the hello.xhtml and do not get the log output of the editVideo() method.
            If I don't choose anything and press the button I get an (expected) NullPointerException from the method editVideo() because selVideo is null can not be logged.

            Any idea?

            Thanks!

            Haefti

            • 3. Re: No selected element from <h:selectOneListbox>
              nickarls

              drop the () from the editVideo() action in the button

              • 4. Re: No selected element from <h:selectOneListbox>
                haefti

                 

                "nickarls" wrote:
                drop the () from the editVideo() action in the button

                Thanks but that does not change anything.
                The log output after clicking the button is:
                13:19:40,096 INFO [STDOUT] Hibernate: select video0_.videoId as videoId123_, video0_.picturesId as picturesId123_, vide
                o0_.datum as datum123_, video0_.status as status123_, video0_.videocatId as videocatId123_, video0_.name as name123_, vi
                deo0_.thema as thema123_, video0_.headline as headline123_, video0_.teaser as teaser123_, video0_.modificationDate as mo
                dific10_123_, video0_.mediaId as mediaId123_, video0_.typ as typ123_, video0_.width as width123_, video0_.height as heig
                ht123_, video0_.credit as credit123_, video0_.flags as flags123_, video0_.videoDisplayCatId as videoDi17_123_, video0_.e
                xpireDate as expireDate123_ from SPC_VIDEO video0_ where video0_.datum>?
                
                13:19:40,268 INFO [ManagerAction] showVideoList1
                
                13:19:40,284 INFO [STDOUT] Hibernate: select video0_.videoId as videoId123_0_, video0_.picturesId as picturesId123_0_,
                video0_.datum as datum123_0_, video0_.status as status123_0_, video0_.videocatId as videocatId123_0_, video0_.name as na
                me123_0_, video0_.thema as thema123_0_, video0_.headline as headline123_0_, video0_.teaser as teaser123_0_, video0_.modi
                ficationDate as modific10_123_0_, video0_.mediaId as mediaId123_0_, video0_.typ as typ123_0_, video0_.width as width123_
                0_, video0_.height as height123_0_, video0_.credit as credit123_0_, video0_.flags as flags123_0_, video0_.videoDisplayCa
                tId as videoDi17_123_0_, video0_.expireDate as expireDate123_0_ from SPC_VIDEO video0_ where video0_.videoId=?
                
                13:19:40,393 INFO [[/simple_video]] resource bundle messages could not be found


                The first entry is the SQL which fills the select box.
                The second one is a debug output from the corresponding method but the third entry could be the SQL I need as you can see in the end ("where video0_.videoId=?").
                Unfortunately no further indication for the method call editVideo() is here.
                Can the last entry with the missing resource bundle be my solution?

                • 5. Re: No selected element from <h:selectOneListbox>
                  nickarls

                  Do you have a messages tag to see validation errors? (although you should see them in the log, too).

                  Tried putting a change listener on the select box?

                  The last sql is probably from the entityConverter...

                  • 6. Re: No selected element from <h:selectOneListbox>
                    haefti

                     

                    "nickarls" wrote:
                    Do you have a messages tag to see validation errors? (although you should see them in the log, too).

                    I inserted a <h:messages /> and get a "validation failed" but I have no idea what kind of validation Seam is doing here.

                    Do I get it right that I can expect an object of the same type as the the collection for the select box contains of as selected element?
                    In this case I don't know why I get an validation error.

                    Tried putting a change listener on the select box?

                    Is it necessary? I hope I can get the selected element without it

                    The last sql is probably from the entityConverter...

                    Okay.

                    • 7. Re: No selected element from <h:selectOneListbox>
                      nickarls

                      Nope, the listener would just have been from debug purposes.
                      Try overriding equals() and hashCode() on your entity. Seam gets upset when the entity is not in the options list (as determined by the said functions)

                      • 8. Re: No selected element from <h:selectOneListbox>
                        haefti

                        You made my morning! ;)

                        Thanks a lot!
                        Overriding the two methods solved the problem. I couldn't figure that out.

                        Be prepared for more newbie questions! ;)

                        • 9. Re: No selected element from <h:selectOneListbox>
                          nickarls

                          Heh, it's a case of "The blind leading the blind" here but glad I could help.

                          I've run into the same trap myself. It's really a JSF issue more than a Seam issue but it comes up often in the convertEntity case so it could be worth a note in the documentation under 29.1...