tags:

views:

1831

answers:

2

Hi, can some one help me with the following JSF dataTable? here I am getting data from database table and I used dataTable binding, but I don't know why it displays the rows 3 times in the screen, but if I remove the binding then it displays only one time.


<h:dataTable binding="#{threadController.dataTable}" var="category" value="#{threadController.queryCategories}" border="1" cellpadding="2" cellspacing="0">
  <h:column>
  <img src="../../images/directory.jpg" alt="Forum Icon" />
  </h:column>
  <h:column>
  <h:form>
  <h:commandLink value="#{category.cname}" action="#{threadController.categoryDateItem}" />
  </h:form>
  </h:column>

// defined globally
private HtmlDataTable dataTable;
private HtmlInputHidden dataItemId = new HtmlInputHidden();


public String categoryDateItem() {
         category = (Category) dataTable.getRowData();
            System.out.println("category action by select: "+category.getCname());
            dataItemId.setValue(category.getId());
            return "editItem"; // Navigation case.
 }

@SuppressWarnings("unchecked")
public ArrayList<Category> getQueryCategories(){    

 return (ArrayList<Category>)HibernateUtil.getSession().createCriteria(Category.class).list(); 

}


output:

            myText   myText   myText
+2  A: 

The binding expression to bind this component to the bean value="#{threadController.queryCategories}".So value attribute is sufficient to retrieve data using dataTable tag.

Warrior
A: 

Binding = component backing bean

Value= data model backing bean

So, you have the Value and Binding set correctly (at least, as far as I can see). Your problem may result from the fact that you're not caching the list you're getting back from the database in getQueryCategories().

You really can't have any idea how often getQueryCategories() will be called in the process of rendering that dataTable, so it's a good idea to do something like this:

// Somewhere near the top of the handler class.. create a cache variable:
private ArrayList<Category> qCategories = null;

// now for getQueryCategories
public ArrayList<Category> getQueryCategories(){    
      if ( qCategories == null ) {  // qCategories should be a member of the handler
           qCategories = (ArrayList<Category>)HibernateUtil.getSession().createCriteria(Category.class).list();   
      }

      return qCategories
}

This kind of cache-ing is very helpful in JSF apps with handlers that are session of even request scoped, as again you can't really know how often JSF will evaluate your "value" expression in the dataTable.

Bill James