4 Replies Latest reply on Mar 31, 2008 11:45 PM by Marcell

    select elements in s:selectManyListBox

    Tim Morgan Newbie

      Hi!
      I know there are some postings concerning <s:selectItems> but I don't think they cover my problem.


      I need to have a select box with multiple choice options but I don't know how to preselect items.


      This is the code:



      <h:selectManyListbox size="10" value="#{pictureManager.selectedPicture.pictureRightList}">
           <s:selectItems value="#{pictureRightList}"
           var="pictureRight" label="#{pictureRight.name}" />
           <s:convertEntity />
      </h:selectManyListbox>



      The select box fills right with the values from pictureRightList but I don't know to get the values from pictureManager.selectedPicture.pictureRightList as selected elements.


      (Sorry for using the same name for different properties but pictureRightList contains a list with all possible choices and pictureManager.selectedPicture.pictureRightList should contain the chosen ones.)


      In a <h:selectOneListBox> I can use the itemValue property of <s:selectItems> but what do I have to do to get it work with a list of selected items?


      Thanks!

        • 1. Re: select elements in s:selectManyListBox
          Pete Muir Master

          Make sure the #{pictureManager.selectedPicture.pictureRightList} contains the values that should already be selected (this is no different to a <h:inputText /> for example

          • 2. Re: select elements in s:selectManyListBox
            Tim Morgan Newbie

            Hi!


            Hm, it does not work.


            I try to paste only the relevant parts of the code:


            @Stateless
            @Name("pictureManager")
            public class PictureManager implements PictureManagerLocal {
            
            /* Logger and EntityManager injected */
                 
                 private Picture selectedPicture;
                 
                 @DataModel
                 private List<PictureRight> pictureRightList;
                      
                 @Factory(value = "pictureRightList")
                 public void initPictureRightList() {
                        Query query = em.createQuery("select p from PictureRight p order by name");
                      pictureRightList =  query.getResultList();
                        log.info("Number of picture rights: " + pictureRightList.size());
                 }
                 
                 public String editPicture() {
                      if (selectedPicture != null) {
                           log.info("Chosen picture: " + selectedPicture.getPictureId());
                           if (selectedPicture.getPictureRightList() != null) {
                                log.info("Number of picture rights: " + selectedPicture.getPictureRightList().size());
                                log.info("First picture right: " + selectedPicture.getPictureRightList().get(0).getName());
                           return "edit";
                           }
                      }
                 }
                 
                 public Picture getSelectedPicture() {
                      return selectedPicture;
                 }
            
                 public void setSelectedPicture(Picture selectedPicture) {
                      this.selectedPicture = selectedPicture;
                 }
                 
            }



            The editPicture() method is called when selecting an option from a select box in a view before.


            After invoking the method I can see in the log, that there is a value injected in selectedPicture, the object has a pictureRightList with the size of 1 and the name of the first associated pictureRight is displayed so I think pictureManager.selectedPicture.pictureRightList should contain a value but it is not selected in the select box of available rights in the following view.


            <h:selectManyListbox size="10" value="#{pictureManager.selectedPicture.pictureRightList}">
                 <s:selectItems value="#{pictureRightList}"
                 var="pictureRight" label="#{pictureRight.name}" />
                 <s:convertEntity />
            </h:selectManyListbox>



            (I also checked, that the associated value is one of the values of the available rights in pictureRightList.)


            Could it be a problem like object identity so that the already associated object(s) do(es) not compare successfully to the items of the list of avaliable items? (Of course they use the same entity bean)


            Maybe someone has another idea.


            Thanks!

            • 3. Re: select elements in s:selectManyListBox
              Pete Muir Master

              For a start you can't have fields with mutators on a @Stateless bean. Make it stateful. You probably want to be in a conversation too.

              • 4. Re: select elements in s:selectManyListBox
                Marcell Newbie

                As Pete said, use a statefull bean or bijection to keep your selectedPicture.


                After that, you are going to run into a @Factory update problem. You can solve this by nullifying the pictureRightList variable at the setSelectedPicture.


                If none of this solve your problem, plz post your java code as is (or a full example) and facelets page.