动态生成并绑定p:dataTable是不好的做法吗?

时间:2020-07-29 18:58:51

标签: dynamic jsf-2

根据该帖子How does the 'binding' attribute work in JSF? When and how should it be used,通常认为它是不良做法。我可能读错了。

话虽如此,我正在做一个primefaces项目,主要要求之一是允许公司定义自定义数据表,然后允许用户创建该数据表的自定义视图。

我当前的设计是使用xml文件来定义列,该列的内容基于像这样的后备bean属性。

        <columns>
        <column>
            <headerText>Column 1</headerText>
            <width>22</width>
            <toggleable>false</toggleable>
            <selectionMode></selectionMode>
            <style>text-align:center</style>
            <styleClass></styleClass>
            <content>
                <componentType>HtmlPanelGroup</componentType>
                <renderedEl>#{!item.readOnly}</renderedEl>
                <valueEl></valueEl>
                <content>
                    <id></id>
                    <componentType>HtmlPanelGroup</componentType>
                    <valueEl></valueEl>
                    <styleClass>ui-icon pi pi-ellipsis-v customDragIcon</styleClass>
                </content>
            </content>
        </column>

在Java中,将读取xml并根据componentType动态创建组件。

    private HtmlPanelGroup createPanelGroup(ContentType g) {
    FacesContext fc = FacesContext.getCurrentInstance();
    Application application = fc.getApplication();

    ValueExpression valueExp = createValueExpression(g.getValueEl());
    HtmlPanelGroup comp = (HtmlPanelGroup) application.createComponent(HtmlPanelGroup.COMPONENT_TYPE);
    comp.setValueExpression("value", valueExp);
    
    if (g.getStyle() != null)
        comp.setStyle(g.getStyle());
    
    if (g.getStyleClass() != null)
        comp.setStyleClass(g.getStyleClass());
    
    if (g.getRenderedEl() != null && g.getRenderedEl().length() > 0) {
        comp.setValueExpression("rendered", createValueExpressionBoolean(g.getRenderedEl()));
    }

    if (g.getDisabledEl() != null && g.getDisabledEl().length() > 0) {
        comp.setValueExpression("disabled", createValueExpressionBoolean(g.getDisabledEl()));
    }

    if (g.getContent() != null && g.getContent().size() > 0) {
        for (ContentType childContent : (List<ContentType>) g.getContent()) {
            Object child = generateChildContent(childContent);
            
            comp.getChildren().add((UIComponent) child);
        }
    }       
    
    return comp;
}   

这将允许我为每个客户端和用户存储自定义xml文件,并动态生成表。是否有更好/不同的方法来满足此要求?

0 个答案:

没有答案