5 Replies Latest reply on Apr 27, 2009 5:19 PM by pbravo

    DataModelSelection injects always the first element of a list instead of the selected one

    jf

      Hi, I have the following problem: when I'm using the DataModelSelection annotation to inject a selected element of a list (for example the selected element in a combobox) into an attribute of my Session Bean, the attribute is always injected with the first element of the list instead of the selected one.


      Here my Session Bean:


      @Stateful
      @Name("portalTaxaCreator")
      public class CreatePortalTaxonActionBean implements CreatePortalTaxonAction {
      
        ...
      
        @EJB
        PortalsDAO portalsDAO;
      
        @DataModel
        private List<Portals> portalList;
      
        @DataModelSelection("portalList")
        private Portals selectedPortal;
      
        @Factory("portalList")
        public void findPortals() {
          portalList = portalsDAO.findAll();
        }
      
        /*
         * Action method.
         */
        public boolean create() {
          // Doing something with selectedPortal
      
          // Problem: selectedPortal is always the first element
          // in portalList and not the selected one!
          ...
        }
      
        @Remove
        public void destroy() {}
      
      }
      



      Here the portal entity:


      @Entity
      @Table(name = "PORTALS")
      public class Portals extends EntityBase {
      
        private Integer id;
      
        private Integer customersId;
      
        private String name;
      
        private String site;
      
        // Other properties + getters and setters
        ...
      
        @Override
        public boolean equals(Object obj) {
          if ((getId() == null) || (obj == null) || !(obj instanceof Portals)) {
            return false;
          }
      
          Portals entity = (Portals) obj;
      
          if (entity.getId() == null) {
            return false;
          }
          return (this.getId().intValue() == entity.getId().intValue());
        }
      
        @Override
        public int hashCode() {
          if (getId() == null) {
            return -1;
          }
          return getId().intValue();
        }
      



      Here my xhtml page:


      ...
      <h:selectOneMenu value="#{selectedPortal}"
                       required="true">                             
        <s:selectItems value="#{portalList}"
                       var="port"
                       label="#{port.name}"
                       noSelectionLabel="#{messages['cmbbx.please.select']}" />
        <s:convertEntity />
      </h:selectOneMenu>
      <h:commandButton value="Create" action="#{portalTaxaCreator.create}" />
      ...
      



      Do you have any ideas why I always get the first element an not the selected element of my list of portals? (I'm using seam 2.0.1.GA, JBoss 4.2.2.GA on Windows XP)


      I have found a solution where it works without the DataModelSelection annotation, but I would prefer to use the annotation since it's a really nice seam-feature (and since it should work)!


      Thanks.


        • 1. Re: DataModelSelection injects always the first element of a list instead of the selected one
          endyamon

          Hi,
          I have the same problem and i'm curious to know the answer.


          • 2. Re: DataModelSelection injects always the first element of a list instead of the selected one
            lucaspereira

            I had this same problem...


            The problem was solved when I updated the version of jsf-impl.jar e jsf-api.jar.


            Now I am using this version:



             1.2_08-b06-FCS
            jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer\jsf-libs
            




            It looks like a rendering problem... the isSelected method was not being called.


            I hope this helps...


            Lucas Pereira

            • 3. Re: DataModelSelection injects always the first element of a list instead of the selected one
              ali1983

              I tried doing your way but I was not successful in that procedure.
              I am using two action classes both with Event Scope. I am sending you the code of the classes please take a look at it and see if u can save the problem


              LocationFactory.java


              This is the class in which I have DataModelSelection


              @Name("locationFactory")
              @Scope(ScopeType.EVENT)
              public class LocationFactory {
              
                   @In(create = true) SalesPersonActionBean salesPersonAction;
                   @In(create = true) LocationService locationService;
                   @In(create = true, required = false) CustomerActionFactory customerActionFactory;
                   
                   @DataModel
                   private List<Locations> lstLocations;
                   @DataModelSelection("lstLocations")
                   @Out(required = false, scope=ScopeType.SESSION)
                   Locations location;
                   
                   UIDatascroller locationDatascroller;
                   
                   @SuppressWarnings("unchecked")
                   @Factory(value = "lstLocations")
                   public void listLocations() {
                        int locationsPageFirstIndex;
                        List listLocationSubSetRecords;          
                        Long locationCount;
                        java.util.Vector locationsVector = new Vector();
              
                        locationCount = locationService
                                  .getLocationRowCount(customerActionFactory.getCustomerWrapper()
                                            .getCustomers().getCustomerNumber());
              
                        locationsVector.setSize(locationCount.intValue());
                        
                        // This statement fetches the current pages first row index
                        
                        locationsPageFirstIndex = (this.getLocationDatascroller().getPage() * 
                                  this.getLocationDatascroller().getDataTable().getRows()) - 
                                  this.getLocationDatascroller().getDataTable().getRows();
                        
                        
                        listLocationSubSetRecords = locationService.getLocations(
                                  salesPersonAction.getLocationOrderField(), salesPersonAction
                                            .isAscendingOrder(), locationsPageFirstIndex,
                                  customerActionFactory.getCustomerWrapper().getCustomers()
                                            .getCustomerNumber());
              
                        for (int index = 0; index < listLocationSubSetRecords.size(); index++) {
                             locationsVector.set(locationsPageFirstIndex + index,
                                       listLocationSubSetRecords.get(index));
                        }
              
                        lstLocations = locationsVector;
                   }
                   
                   public void nullLocationList(){
                        this.lstLocations = null;
                   }
              
                   public List<Locations> getLstLocations() {
                        return lstLocations;
                   }
              
                   public void setLstLocations(List<Locations> lstLocations) {
                        this.lstLocations = lstLocations;
                   }
              
                   public UIDatascroller getLocationDatascroller() {
                        return locationDatascroller;
                   }
              
                   public void setLocationDatascroller(UIDatascroller locationDatascroller) {
                        this.locationDatascroller = locationDatascroller;
                   }
              
                   public Locations getLocation() {
                        return location;
                   }
              
                   public void setLocation(Locations location) {
                        this.location = location;
                   }
              }



              LocationPrinterFactory.java


              @Name("locationPrinterFactory")
              @Scope(ScopeType.EVENT)
              
              public class LocationPrinterFactory {
                   
                   @In(create = true)LocationPrinterService locationPrinterService;
                   @In(create = true, required = false)LocationFactory locationFactory;
                   @DataModel private List<Locationprinters> lstLocationPrinters;
                   
                   UIDatascroller locationPrinterDatascroller;
                   
                   @SuppressWarnings("unchecked")
                   @Factory("lstLocationPrinters")
                   public void listLocationPrinters() {
                        Long locationPrinterCount;
                        Vector locationPrintersVector = new Vector();
                        
                        int locationPrinterCurrentPageIndex;
                        int locationPrintersPageFirstIndex;
                        List listLocationPrintersSubSetRecords;
              
                        
              *System.out.println("this.this.locationFactory.getLocation().getLocationName(): " + this.locationFactory.getLocation().getLocationName());
              System.out.println("this.locationFactory.getLocation.getLocationNumber: " + this.locationFactory.getLocation().getLocationNumber());*
              
                        
                        locationPrinterCount = locationPrinterService
                                  .getlocationPrinterRowCount(this.locationFactory.getLocation().getLocationNumber());
              
                        locationPrintersVector.setSize(locationPrinterCount.intValue());
              
                        
                        // This statement fetches the current pages first row index
                        locationPrintersPageFirstIndex = (this.locationPrinterDatascroller.getPage() *
                                  this.locationPrinterDatascroller.getDataTable().getRows()) - 
                                  this.locationPrinterDatascroller.getDataTable().getRows();
                        
                        listLocationPrintersSubSetRecords = locationPrinterService
                                  .getLocationPrinter(locationPrintersPageFirstIndex,
                                            this.locationFactory.getLocation().getLocationNumber());
              
                        for (int index = 0; index < listLocationPrintersSubSetRecords.size(); index++) {
                             locationPrintersVector.set(locationPrintersPageFirstIndex + index,
                                       listLocationPrintersSubSetRecords.get(index));
                        }
              
                        lstLocationPrinters = locationPrintersVector;
                   }
              
                   public List<Locationprinters> getLstLocationPrinters() {
                        return lstLocationPrinters;
                   }
              
                   public void setLstLocationPrinters(List<Locationprinters> lstLocationPrinters) {
                        this.lstLocationPrinters = lstLocationPrinters;
                   }
              
                   public UIDatascroller getLocationPrinterDatascroller() {
                        return locationPrinterDatascroller;
                   }
               
                   public void setLocationPrinterDatascroller(
                             UIDatascroller locationPrinterDatascroller) {
                        this.locationPrinterDatascroller = locationPrinterDatascroller;
                   }
                        
                   public void nullLocationPrinter(){
                        this.lstLocationPrinters = null;
                   }     
              }



              I am trying to access the DataModelSelection in LocationPrinterFactory class but it always print me the very first value.

              • 4. Re: DataModelSelection injects always the first element of a list instead of the selected one
                ali1983

                Lucas Pereira wrote on Jun 10, 2008 21:05:


                I had this same problem...

                The problem was solved when I updated the version of jsf-impl.jar e jsf-api.jar.

                Now I am using this version:


                 1.2_08-b06-FCS
                jboss-4.2.2.GA\server\default\deploy\jboss-web.deployer\jsf-libs
                




                It looks like a rendering problem... the isSelected method was not being called.

                I hope this helps...

                Lucas Pereira



                I tried doing your way but I was not successful in that procedure.
                I am using two action classes both with Event Scope. I am sending you the code of the classes please take a look at it and see if u can save the problem


                LocationFactory.java


                This is the class in which I have DataModelSelection


                @Name("locationFactory")
                @Scope(ScopeType.EVENT)
                public class LocationFactory {
                
                     @In(create = true) SalesPersonActionBean salesPersonAction;
                     @In(create = true) LocationService locationService;
                     @In(create = true, required = false) CustomerActionFactory customerActionFactory;
                     
                     @DataModel
                     private List<Locations> lstLocations;
                     @DataModelSelection("lstLocations")
                     @Out(required = false, scope=ScopeType.SESSION)
                     Locations location;
                     
                     UIDatascroller locationDatascroller;
                     
                     @SuppressWarnings("unchecked")
                     @Factory(value = "lstLocations")
                     public void listLocations() {
                          int locationsPageFirstIndex;
                          List listLocationSubSetRecords;          
                          Long locationCount;
                          java.util.Vector locationsVector = new Vector();
                
                          locationCount = locationService
                                    .getLocationRowCount(customerActionFactory.getCustomerWrapper()
                                              .getCustomers().getCustomerNumber());
                
                          locationsVector.setSize(locationCount.intValue());
                          
                          // This statement fetches the current pages first row index
                          
                          locationsPageFirstIndex = (this.getLocationDatascroller().getPage() * 
                                    this.getLocationDatascroller().getDataTable().getRows()) - 
                                    this.getLocationDatascroller().getDataTable().getRows();
                          
                          
                          listLocationSubSetRecords = locationService.getLocations(
                                    salesPersonAction.getLocationOrderField(), salesPersonAction
                                              .isAscendingOrder(), locationsPageFirstIndex,
                                    customerActionFactory.getCustomerWrapper().getCustomers()
                                              .getCustomerNumber());
                
                          for (int index = 0; index < listLocationSubSetRecords.size(); index++) {
                               locationsVector.set(locationsPageFirstIndex + index,
                                         listLocationSubSetRecords.get(index));
                          }
                
                          lstLocations = locationsVector;
                     }
                     
                     public void nullLocationList(){
                          this.lstLocations = null;
                     }
                
                     public List<Locations> getLstLocations() {
                          return lstLocations;
                     }
                
                     public void setLstLocations(List<Locations> lstLocations) {
                          this.lstLocations = lstLocations;
                     }
                
                     public UIDatascroller getLocationDatascroller() {
                          return locationDatascroller;
                     }
                
                     public void setLocationDatascroller(UIDatascroller locationDatascroller) {
                          this.locationDatascroller = locationDatascroller;
                     }
                
                     public Locations getLocation() {
                          return location;
                     }
                
                     public void setLocation(Locations location) {
                          this.location = location;
                     }
                }



                LocationPrinterFactory.java


                @Name("locationPrinterFactory")
                @Scope(ScopeType.EVENT)
                
                public class LocationPrinterFactory {
                     
                     @In(create = true)LocationPrinterService locationPrinterService;
                     @In(create = true, required = false)LocationFactory locationFactory;
                     @DataModel private List<Locationprinters> lstLocationPrinters;
                     
                     UIDatascroller locationPrinterDatascroller;
                     
                     @SuppressWarnings("unchecked")
                     @Factory("lstLocationPrinters")
                     public void listLocationPrinters() {
                          Long locationPrinterCount;
                          Vector locationPrintersVector = new Vector();
                          
                          int locationPrinterCurrentPageIndex;
                          int locationPrintersPageFirstIndex;
                          List listLocationPrintersSubSetRecords;
                
                          
                *System.out.println("this.this.locationFactory.getLocation().getLocationName(): " + this.locationFactory.getLocation().getLocationName());
                System.out.println("this.locationFactory.getLocation.getLocationNumber: " + this.locationFactory.getLocation().getLocationNumber());*
                
                          
                          locationPrinterCount = locationPrinterService
                                    .getlocationPrinterRowCount(this.locationFactory.getLocation().getLocationNumber());
                
                          locationPrintersVector.setSize(locationPrinterCount.intValue());
                
                          
                          // This statement fetches the current pages first row index
                          locationPrintersPageFirstIndex = (this.locationPrinterDatascroller.getPage() *
                                    this.locationPrinterDatascroller.getDataTable().getRows()) - 
                                    this.locationPrinterDatascroller.getDataTable().getRows();
                          
                          listLocationPrintersSubSetRecords = locationPrinterService
                                    .getLocationPrinter(locationPrintersPageFirstIndex,
                                              this.locationFactory.getLocation().getLocationNumber());
                
                          for (int index = 0; index < listLocationPrintersSubSetRecords.size(); index++) {
                               locationPrintersVector.set(locationPrintersPageFirstIndex + index,
                                         listLocationPrintersSubSetRecords.get(index));
                          }
                
                          lstLocationPrinters = locationPrintersVector;
                     }
                
                     public List<Locationprinters> getLstLocationPrinters() {
                          return lstLocationPrinters;
                     }
                
                     public void setLstLocationPrinters(List<Locationprinters> lstLocationPrinters) {
                          this.lstLocationPrinters = lstLocationPrinters;
                     }
                
                     public UIDatascroller getLocationPrinterDatascroller() {
                          return locationPrinterDatascroller;
                     }
                 
                     public void setLocationPrinterDatascroller(
                               UIDatascroller locationPrinterDatascroller) {
                          this.locationPrinterDatascroller = locationPrinterDatascroller;
                     }
                          
                     public void nullLocationPrinter(){
                          this.lstLocationPrinters = null;
                     }     
                }



                I am trying to access the DataModelSelection in LocationPrinterFactory class but it always print me the very first value.

                • 5. Re: DataModelSelection injects always the first element of a list instead of the selected one
                  pbravo
                  Don't use @DataModelSelection with selectOneMenu, use @In(required=false) instead.

                  See http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4082996#4082996