8 Replies Latest reply on Oct 14, 2007 5:45 AM by pmuir

    Binding HtmlDataTable to h:dataTable in Seam

    asookazian

      Is it possible to do the following in Seam1.2.1.GA? I need a solution for a dataTable that can handle a dynamic column resultset from stored proc.

      snippet of .xhtml:

      <h:dataTable value="#{userRoleSearch.myList}" var="myItem" binding="#{userRoleSearch.dynamicDataTable}"/>


      snippet of userRoleSearch SFSB:
      public HtmlDataTable getDynamicDataTable() {
       if (dynamicDataTable == null) {
       viewRolesData(selectedChoices);
       populateDynamicDataTable();
       }
      
       return dynamicDataTable;
       }



      The @DataModel Seam annotation exposes an attribute of type java.util.List to a JSF page as an instance of javax.faces.model.DataModel. The <h:dataTable> supports data binding to a collection of data objects represented by a DataModel instance.


      http://weblogs.java.net/blog/caroljmcdonald/archive/2007/07/sample_applicat_1.html

      In this case, I am not using @DataModel and the return type is HtmlDataTable which dynamically replaces the <h:dataTable> rendering based on the # of columns in the resultset.

      The solution above works fine with JSF + managed bean, but it doesn't work with Seam (I get JSF h:dataTable tag formatting error).

      Will I need to use a hybrid Seam SFSB + managed bean approach?? thx.

        • 1. Re: Binding HtmlDataTable to h:dataTable in Seam
          asookazian

          Ok, now there is no exception (using only the Seam SFSB, not the manage bean anymore), but the dataTable does not render on the screen.

          Using the <s:debug> tag, I see the following in the component tree:

          <HtmlDataTable border="-2147483648" first="0" id="_id22" rendered="true" rowIndex="-1" rows="0" transient="false" var="myItem" binding="#{userRoleSearch.dynamicDataTable}"/>


          • 2. Re: Binding HtmlDataTable to h:dataTable in Seam
            asookazian

            Here is the dynamic populator method. Looks like the value attribute is not being bound (it wasn't null in the Eclipse debugger)...

            public void populateDynamicDataTable() {
            
             // Any columns?
             if (myList != null && myList.size() > 0) {
             dynamicDataTable = new HtmlDataTable();
            
             // Get amount of columns.
             int columns = ((List) myList.get(0)).size();
            
             // Set columns.
             for (int i = 0; i < columns; i++) {
            
             // Set header (optional).
             UIOutput header = new UIOutput();
             header.setValue(headers);
            
             // Set output.
             UIOutput output = new UIOutput();
             ValueBinding myItem =
             FacesContext
             .getCurrentInstance()
             .getApplication()
             .createValueBinding("#{myItem[" + i + "]}");
             output.setValueBinding("value", myItem);
            
             // Set column.
             UIColumn column = new UIColumn();
             column.setHeader(header);
             column.getChildren().add(output);
            
             // Add column.
             dynamicDataTable.getChildren().add(column);
             }
             }
             }


            • 3. Re: Binding HtmlDataTable to h:dataTable in Seam
              asookazian

              I noticed that the binding action only gets exec'd once per session (the SFSB has session scope).

              • 4. Re: Binding HtmlDataTable to h:dataTable in Seam
                asookazian

                As far as order of events, how do you know which actions/methods will be exec'd in what order?

                For example:

                <s:div rendered="#{userRoleSearch.displayViewRolesDataTable}">
                 <h:dataTable value="#{userRoleSearch.myList}" var="myItem" binding="#{userRoleSearch.dynamicDataTable}"/>
                 </s:div>


                • 5. Re: Binding HtmlDataTable to h:dataTable in Seam
                  asookazian

                  Ok, so it seems to be working perfectly AFTER I pulled the dataTable out into a separate .xhtml due to the hiding/showing (use of <s:div rendered...>) becoming a problem. I am redirecting as follows:

                  pages.xml

                  <!-- added for the dynamic column support -->
                   <page view-id="/filterRoles.xhtml">
                   <navigation from-action="#{userRoleSearch.viewRoles}">
                   <redirect view-id="/dynamic.xhtml"/>
                   </navigation>
                   </page>


                  The order of method executions in a JSF file is very tricky and illogical to me. The fact that sometimes a method called from a JSF gets called several times (seen in the debugger) is very illogical as well.



                  • 6. Re: Binding HtmlDataTable to h:dataTable in Seam
                    pmuir

                    Binding back into components, is, in my opinion, a very bad idea. You can try http://docs.jboss.org/seam/2.0.0.CR1/reference/en/html/conversations.html#d0e5034 the uiComponent technique described here.

                    As for JSF calling methods multiple times, this is perfectly normal for JSF - you should get yourself a good JSF reference.

                    • 7. Re: Binding HtmlDataTable to h:dataTable in Seam
                      asookazian

                       

                      "pete.muir@jboss.org" wrote:
                      Binding back into components, is, in my opinion, a very bad idea. You can try http://docs.jboss.org/seam/2.0.0.CR1/reference/en/html/conversations.html#d0e5034 the uiComponent technique described here.

                      As for JSF calling methods multiple times, this is perfectly normal for JSF - you should get yourself a good JSF reference.


                      I have the Java Server Faces book by Bergsten. What specifically is the problem with using binding back into components? I didn't see any negatives on this technique. Is it something that has to do with JSF and Seam regarding this technique?? thx.

                      • 8. Re: Binding HtmlDataTable to h:dataTable in Seam
                        pmuir

                         

                        "asookazian" wrote:
                        I didn't see any negatives on this technique. Is it something that has to do with JSF and Seam regarding this technique?? thx.


                        IMO it tends to indicate bad design and a lack of proper separation of the UI and business layer (unless of course you bind your components in UI layer java). Also, with facelets and seam there are better ways to do the same e.g. injection via uiComponent or a facelets handler.