【问题标题】:How to render different ui tags in a ui:repeat如何在 ui:repeat 中呈现不同的 ui 标签
【发布时间】:2011-11-29 10:27:52
【问题描述】:

我正在用 JSF 2.0 开发一个应用程序。 在应用程序中必须有一个页面,用户可以在其中创建文档模板。它可与 Google 文档表单功能相媲美。例如,用户应该能够确定他们想要 inputText、textArea 或 selectBooleanCheckbox 在模板中的哪个位置。我设计了一个超类 UiDocumentElement 和子类 UiTextarea, UiInputText, ... .

现在我想知道如何在我的 XHTML 页面上显示这样的文档模板。我的支持 bean 将有一个带有 UiDocumentElement 对象的 DataModel。但是如何使用 ui:repeat 来显示不同类型的 UI 标签?还是我应该尝试另一种设计来实现这一点?

其实是为了解决这个问题:

 <h1>#{backingBean.templateTitle}</h1>

<ui:repeat value="#{backingBean.uiDocumentElements}" var="uiElement">        
    <label>
        <span>#{uiElement.label}</span>
        <!-- here the application should know whether to render an inputText, an inputTextarea or a selectBooleanCheckbox with the attribute value="#{uiElement.value}" -->
    </label>        
</ui:repeat>

任何帮助将不胜感激。

编辑:查看 BalusC 的评论以及相关问题的链接。

【问题讨论】:

标签: jsf jsf-2


【解决方案1】:

最简单的方法是通过rendered 属性控制一个 3 组件块:

<h:inputText value="#{uiElement.value}" rendered="#{uiElement.type == 'input'}"/>
<h:inputTextarea value="#{uiElement.value}" rendered="#{uiElement.type == 'textArea'}"/>
<h:selectBooleanCheckbox value="#{uiElement.value}" rendered="#{uiElement.type == 'checkbox'}"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-01
    • 2017-01-07
    • 2019-09-28
    • 2011-10-23
    • 2012-11-22
    • 1970-01-01
    相关资源
    最近更新 更多