在JSF中,在h:dataTable中有一个ui:repeater是有效的

时间:2012-06-25 12:28:10

标签: java jsf-2

在h:dataTable中有一个JSF ui:repeater来动态定义列是否有效?即使从html引用了getFieldNames(...)方法,也永远不会调用它。方法getEntities被调用。有什么想法吗?

@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>

1 个答案:

答案 0 :(得分:1)

不,不可能使用ui:repeat来在数据表中包含动态列。您可以尝试使用Primefaces或Richfaces等组件库,这样可以轻松获得动态列。在this链接中,您可以看到Primefaces的动态列功能。 此外,您可以尝试在辅助bean中动态创建表模型。此BalusC的tutorial解释了如何执行此操作。