0

有人可以帮我处理以下 JSF 数据表吗?在这里,我从数据库表中获取数据并使用了 dataTable 绑定,但我不知道为什么它在屏幕上显示行 3 次,但如果我删除绑定,那么它只显示一次。


<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();   

}

输出:

            myText   myText   myText
4

2 回答 2

2

将此组件绑定到 bean value="#{threadController.queryCategories}" 的绑定表达式。因此 value 属性足以使用 dataTable 标记检索数据。

于 2009-01-12T04:57:39.550 回答
0

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.

于 2009-01-19T01:00:51.417 回答