2012-06-25 30 views
0

在h:dataTable裏面有一個JSF ui:repeater用於動態定義列是否有效? getFieldNames(...)方法從不被調用,即使它是從html引用的。方法getEntities被調用。有任何想法嗎?在JSF中是有效的在h:dataTable裏面有一個ui:repeater

@Named(value = "data") 
@RequestScoped 
public class MyBean { 

    List<Map<String, String>> data = new ArrayList<Map<String, String>>(); 

    public CustomBean() { 
     final AtomicInteger i = new AtomicInteger(0); 
     for (final String fieldName : new String[] 
      {"ColumnOne", "ColumnTwo", "ColumnThree"}) { 
      data.add(new HashMap<String, String>() { 

       { 
        put(fieldName, fieldName + "_" + i.getAndDecrement()); 
       } 
      }); 
     } 
    } 

    public List<String> getFieldNames() { 
     // THIS METHOD DOES NOT GET CALLED 
     return new ArrayList<String>(data.get(0).keySet()); 
    } 

    public List<Map<String, String>> getEntities() { 
     return data; 
    } 
} 



<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" 
     xmlns:h="http://java.sun.com/jsf/html" 
     xmlns:f="http://java.sun.com/jsf/core" 
     xmlns:ui="http://java.sun.com/jsf/facelets"> 
    <h:head> 
     <title>Title</title> 
    </h:head> 
    <h:body> 
     <h:dataTable value="#{data.entities}" var="entity"> 
      <ui:repeat var="fieldName" value="#{data.fieldNames}"> 
       <h:column> 
        <f:facet name="header">#{fieldName}</f:facet> 
        #{entity.get(fieldName)} 
       </h:column> 
      </ui:repeat> 
     </h:dataTable> 
    </h:body> 
</html> 
+0

以下Q/As可能會在你的主題上閃亮一些: http://stackoverflow.com/questions/8945544/uirepeat-and-hpanelgrid,http://stackoverflow.com/questions/3342984/jstl-in- JSF2,Facelets的 - 品牌 - 感 – Yura

回答

1

不,不可以使用ui:repeat在數據表中使用動態列。您可以嘗試一個組件庫,例如Primefaces或Richfaces,它可以讓您輕鬆地創建動態列。在this鏈接中,您可以查看Primefaces的動態列功能。 此外,您可以嘗試在後臺bean中動態創建表模型。此BalusC的tutorial解釋瞭如何執行此操作。

相關問題