jsf有条件地设置id

时间:2012-07-13 15:53:43

标签: jsf parameters custom-tag

我有一个自定义标签,并希望允许设置内部元素的ID

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:rich="http://richfaces.org/rich"
    xmlns:a4j="http://richfaces.org/a4j">

<f:subview rendered="#{not empty id}">
    <h:message styleClass="message" id="#{id}" errorClass="message error"
        warnClass="message warn" for="#{element}" />
</f:subview>
<f:subview rendered="#{not id}">
    <h:message styleClass="message" errorClass="message error"
        warnClass="message warn" for="#{element}" />
</f:subview>
</html>

但我总是得到

Empty id attribute is not allowed

我如何能够在用户设置id时使用它,而不是jsf应该自己生成它

2 个答案:

答案 0 :(得分:1)

使用<c:if>有条件地添加<f:attribute> id

<h:message styleClass="message" errorClass="message error" warnClass="message warn" for="#{element}">
    <c:if test="#{not empty id}">
        <f:attribute name="id" value="#{id}" />
    </c:if>
</h:message>

顺便说一句,在自定义标记中使用<html>而不是<ui:composition>,甚至没有它也很奇怪。您确定最终使用语法上有效的HTML吗?此外,您的第二个<f:subview rendered>表达式不正确,但这与您获得的特定错误消息无关。

答案 1 :(得分:0)

使用BalusC的答案使我在IllegalArgumentException类中的UIComponent上专门为id抛出了这个问题:

else if ("id".equals(name) || "parent".equals(name)) {
    throw new IllegalArgumentException();
}

我最终使用了Omnifaces的o:tagAttribute