【问题标题】:Best approach to render UI elements based on dataType基于 dataType 呈现 UI 元素的最佳方法
【发布时间】:2011-12-09 14:31:34
【问题描述】:

我正在尝试根据 dataType 值动态呈现 Richfaces 和 jsf UI 元素。

例如:我有一个如下的枚举

public enum DataType {
    DT_LONGLONG(1), DT_STRING(2), DT_LONG(3), DT_DATE(4), DS_EXTERNALREFERENCE(5), 
    DT_BOOLEAN(6), DT_FLOAT(7), DT_SHORT(8);
}

然后在 xhtml 页面中遍历我的自定义对象列表时,我检查 dataType 并相应地呈现 UI 元素,如下所示:

<c:if test="#{meaCompPartAttr.dataType.dataType == 2}">
    <h:inputText />
</c:if>
<c:if test="#{(meaCompPartAttr.dataType.dataType == 1) or
        (meaCompPartAttr.dataType.dataType == 3) or
        (meaCompPartAttr.dataType.dataType == 8)}">
    <h:inputText onkeyup="javascript:validateField(this,             '#{tpMsgs.longRegularExpression}');">
    <f:validateLongRange/>
    </h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 7}">
<h:inputText onkeyup="javascript:validateField(this, '#{tpMsgs.doubleRegularExpression}');">
    <f:validateDoubleRange/>
    </h:inputText>
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 6}">
    <h:selectBooleanCheckbox />
</c:if>
<c:if test="#{meaCompPartAttr.dataType.dataType == 4}">
    <rich:calendar />
</c:if>

因此,我通常会遇到类转换异常,例如 String to Boolean 或 Long to String 等。我认为这是因为 jstl 和 jsf 代码不同步运行。

是否有任何其他方法可以按照上述示例中的建议动态呈现 UI 元素?

【问题讨论】:

    标签: jsf enums richfaces jstl facelets


    【解决方案1】:

    所以您使用&lt;ui:repeat&gt;&lt;h:dataTable&gt; 或任何其他JSF 迭代组件而不是JSTL &lt;c:forEach&gt; 进行迭代?要么改用&lt;c:forEach&gt;,要么改用rendered 属性而不是&lt;c:if&gt;

    另见:

    【讨论】:

    • 我使用 c:forEach 来遍历组件。我想现在试试渲染属性。
    • 但是想知道在我看来检查枚举值的正确方法是什么?或者我不能直接做点什么:
    • 嗯,那么你的问题是由其他原因引起的。关于这个问题的信息很少,很难给出有针对性的答案。编辑:好吧,你可以使用#{meaCompPartAttr.dataType == 'DT_BOOLEAN'}。另请参阅第二个“另请参阅”链接。
    • 终于有时间浏览这些链接,它们真的很有帮助,帮助我解决了问题。非常感谢 BalusC
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-15
    • 2021-12-06
    • 2011-11-01
    • 2017-10-25
    • 2011-11-01
    • 2015-10-28
    相关资源
    最近更新 更多