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

Will Primefaces not automatically generate ID for html tag JSF?

今天我在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。我尝试了几个组件,但仍然不确定。那么,我的结论正确吗?如果是这样,为什么会出现这种行为?

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

默认情况下,com.sun.faces.renderkit.html_basic.TextRenderer 渲染的 h:outputText 组件(在 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 没有任何关系。