Primefaces不会自动为html标签JSF生成ID吗?

问题描述 投票:0回答:1

今天我在JSF中发现了一个有趣的标签。我从BalusC's comment得到了这一点:

<h:form>
    <h:outputText value="#{bean.text1}" styleClass="myClass" />
    <p:commandButton value="Update" update="@(.myClass)" /> 
</h:form>

但是以下示例将起作用(请注意,不需要为表单分配ID):

<h:form>
    <h:outputText id="myText" value="#{bean.text1}" styleClass="myClass" />
    <p:commandButton value="Update" update="@(.myClass)" /> 
</h:form>

似乎Primefaces不会为纯HTML标记生成ID。我尝试了几个组件,但仍然不确定。那么,我的结论是否正确?如果是这样,为什么会这样呢?

jsf primefaces
1个回答
3
投票

假设你问为什么<span>渲染的<h:outputText value="#{bean.text1}" styleClass="myClass" />元素上没有ID属性:

默认情况下,h:outputText呈现的com.sun.faces.renderkit.html_basic.TextRenderer组件(如果是Mojarra)不会呈现ID。是否呈现ID由com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.shouldWriteIdAttribute(UIComponent) right here确定:

/**
     * @param component
     *            the component of interest
     *
     * @return true if this renderer should render an id attribute.
     */
    protected boolean shouldWriteIdAttribute(UIComponent component) {

        // By default we only write the id attribute if:
        //
        // - We have a non-auto-generated id, or...
        // - We have client behaviors.
        //
        // We assume that if client behaviors are present, they
        // may need access to the id (AjaxBehavior certainly does).

        String id;
        return (null != (id = component.getId()) && (!id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX)
                || ((component instanceof ClientBehaviorHolder) && !((ClientBehaviorHolder) component).getClientBehaviors().isEmpty())));
}

所有这些都是普通的JSF,与primefaces没有任何关系。

© www.soinside.com 2019 - 2024. All rights reserved.