4 Replies Latest reply on Feb 27, 2011 8:12 AM by Benjamin Seyinbour

    Can't get data from entity bean.

    Sam W Expert

      Hello,

       

      With JBOSS 6.0, JDK 1.6  and FreeBSD,

      I have a single selected datatable list. When I click on it, my managed bean gets the entity " role", but the dialog is unable to get the value from the entity bean..

       

       

      Here is my code for illustration:

       

       

      Controller :

       

       

      [quote]@ManagedBean(name="roleController")

      //@SessionScoped

      //@ViewScoped

      public class RoleController //implements Serializable

      {

       

       

          @EJB

          private RoleManager roleManager;

          private Role selectedRole;

          private String roleName;

          private final static Logger logger = Logger.getLogger(RoleController.class.getName());

          private ArrayList<Role> roleList;

       

          public RoleController() {

              logger.info("Invoke roleController contructor.");

                roleList = null;

          }

       

          public void createRole() {

                logger.info("Creating role "+getRoleName());

                roleManager.createRoleFromName(getRoleName());

          }

       

            public Role getSelectedRole() {

              return selectedRole;

          }

       

       

          [b]public void setSelectedRole(Role selectedRole) {

              logger.info(" === selectedRole: " + selectedRole.getRoleId());

              this.selectedRole = selectedRole;[/b]

          }[/quote]

       

       

      In the managed bean(controller) as shown above, whenever I click on a row, the setSelectedRole() method is get called, and the value of getRoleId() is returned correctly.

       

       

      [quote]

       

       

      <html xmlns="http://www.w3.org/1999/xhtml"

            xmlns:ui="http://java.sun.com/jsf/facelets"

            xmlns:f="http://java.sun.com/jsf/core"

            xmlns:h="http://java.sun.com/jsf/html"

            xmlns:p="http://primefaces.prime.com.tr/ui">

       

          <p:panel id="panel-edit" header="Edit Role" style="margin-bottom:10px;"

                   > 

       

              <h:panelGrid columns="5" cellpadding="4"> 

                  <h:outputText value="Role ID: " />

                 [b] <h:outputText value="#{roleController.selectedRole.roleId}" />[/b]

       

              </h:panelGrid> 

          </p:panel>  

       

      </html>

       

       

      [/quote]

       

       

      In the above role_add.xhtml file, the roleId is always empty.

       

      If I changed the line of outputText to the one shown as below:

      <h:outputText value="#{roleController.selectedRole}" />

       

      the role_edit.xhtml prints the address of the Role object:

      au.com.houseware.wsdl.ix.generated.Role@cf2e58

       

      Can anyone tell me what have the code missing or misconfigured?

       

       

      Thanks

      Sam

        • 1. Can't get data from entity bean.
          Mark Paluch Master

          Hi Sam,

          is your roleId set (overwritten) by any other JSF component? You could try to make a custom toString Method where you construct a String for debugging.

           

          Code looks fine, but keep in Mind, that a @ManagedBean without @...Scope is handled as RequestScoped Bean.

           

          Best regards,

          Mark

          • 2. Can't get data from entity bean.
            Benjamin Seyinbour Expert

            Hi Sam W,

             

            I don't use JSF any more, but if I recollect correctly, the outputText 'value' attribute takes Object  and I don't think it would  accepts your Role class unless you have a custom converter for it. It would also accepts String, Integer etc., but then, there are built-in converters for them.

             

            Your <h:outputText value="#{roleController.selectedRole}" /> is simply returning the default toString() inherited from Object. It doesn't seem you have overridden it in Role.

             

            Also make sure you have a public getter (and optionally, public  setter) for the 'roleId' attribute in your Role class.

             

            Cheers!

            • 3. Can't get data from entity bean.
              Sam W Expert

              Hello,

               

              Thank you for the response.

              Here is more precise symtom of the problem:

               

               

              The value of the single selection of datatable is not being updated.

              This is reproducible at least in my enviornment.

              The test case is:

              1. on fresh load of the web page, click on the 2nd row in datatable, noticed that setSelectedRole() method sets the correct value, but the getSelectedRole() doesn't get called, thus the popup dialog from the Edit Button show nothing on the page ( getSelectedRole() doesn't get call).

              2. Now change the page theme, noticed that getSelectedRole() get calls with the value from 2nd row.

              3. click on the third row, the setSelectedRole() method get called, and the value is set correctly (which are the values from the 3rd row). Click on the Edit button, the dialog shows  the value on 2nd row (step 1). But I have just clicked the 3rd row, the value should be from the 3rd row.

              4. Change theme again, getSelectRole() get calls with the value from the 3rd row...

               

               

              Now you can repeat the steps, it is reproduciable.

               

               

               

               

              Here are the sample codes.

               

               

              create the following xhtml:

              [quote]

              <html xmlns="http://www.w3.org/1999/xhtml"

                    xmlns:ui="http://java.sun.com/jsf/facelets"

                    xmlns:f="http://java.sun.com/jsf/core"

                    xmlns:h="http://java.sun.com/jsf/html"

                    xmlns:p="http://primefaces.prime.com.tr/ui">

                 

              <h:form id="f1">

               

               

                  <p:dialog header="Edit Role" modal="true" widgetVar="editDlg"

                            resizable="false" width="400" showEffect="explode" hideEffect="explode"> 

                      <h:panelGrid columns="5" cellpadding="4">   

                          <h:outputText value="#{roleController.selectedRole.roleId}" />

                      </h:panelGrid>

                  </p:dialog>

               

               

                  <p:outputPanel id="tableWrapper">

                      <p:dataTable value="#{roleController.roles}" var="_role"

                              selection="#{roleController.selectedRole}" selectionMode="single"

                              >

                          <p:column sortBy="#{_role.roleId}" filterBy="#{_role.roleId}">

                              <f:facet name="header">

                                  <h:outputText value="Role ID" />

                              </f:facet>

                              <h:outputText value="#{_role.roleId}" />

                          </p:column>

               

               

                          <f:facet name="footer">

               

               

                              <p:commandButton value="Edit" image="ui-icon ui-icon-search" 

                                          oncomplete="editDlg.show()"/>

                

                          </f:facet>

                      </p:dataTable>

                  </p:outputPanel>  

              </h:form>

                 

              </html>

              [/quote]

               

               

               

               

              Managed bean:

              [quote]@ManagedBean(name="roleController")

              //@SessionScoped

              @ViewScoped

              public class RoleController //implements Serializable

              {

               

               

                  @EJB

                  private RoleManager roleManager;

                  private Role selectedRole;

                  private String roleName;

                  private ArrayList<Role> roleList;

               

               

               

               

                  public RoleController() {

                                  roleList = null;

                  }

                 

                  public void createRole() {

                        roleManager.createRoleFromName(getRoleName());

                  }

                 

                    public Role getSelectedRole() {

                        if (selectedRole == null)

                            logger.info(" ======= getSelectedRole is NULL");

                        else

                          logger.info(" === getSelectedRole: " + selectedRole.getRoleId());

                      return selectedRole;

                  }

               

               

                  public void setSelectedRole(Role selectedRole) {

                      logger.info(" === setSelectedRole: " + selectedRole.getRoleId());

                      this.selectedRole = roleManager.findByRoleId(selectedRole.getRoleId());

                      logger.info(" --- setSelectedRole: " + this.selectedRole.getRoleId());

                  }

                 

                  public String getRoleName() {

                      return roleName;

                  }

                 

                  public void setRoleName(String roleName) {

                      logger.info("setting role "+roleName);

                      this.roleName = roleName;

                  }

               

               

                  public ArrayList<Role> getRoles()

                  {

                      if (roleList == null || roleList.size() == 0) {

                          roleList = loadAll();

                      }

                      logger.info("getRoles: "+roleList.size());

                      return roleList;

                     

                  }

                 

                  private ArrayList<Role> loadAll() {

                      List<Role> retList = roleManager.findAll();

                          roleList = new ArrayList<Role>();

                          for (Role role : retList) {

                              roleList.add((Role) role);

                          }

                          return roleList;

                  }

              }[/quote]

               

               

              Thanks alot

              Sam

              • 4. Can't get data from entity bean.
                Benjamin Seyinbour Expert

                Hi Sam W,

                 

                The setSelectedRole() shows the code that initializes selectedRole, but nowhere in the initialization process of your backing bean is setSelectedRole() called nor is it init in its getter method; unless you have a method (not shown) to handle your onclick event, which initializes the selectedRole.

                 

                 

                You want to init the selectedRole in the constructor or preferable in a seperate method [initSelectedRole()] with the @PostConstruct annotation. In both cases, you will have to feed it a "roleId' to use roleManager.findByRoleId.

                 

                The roleId may be a default or may come from the user -- but only after the page is up.

                 

                 

                You may want to add to your getSelectedRole() the following line:

                 

                selectedRole == new Role(); //If selectedRole == null

                 

                And assign it a default roleId (just for testing), and you will have a better idea.

                 

                Cheers