首页 > 解决方案 > Will Primefaces not automatically generate ID for html tag JSF?

问题描述

Today I found out an interesting thing with tag in JSF. I got this point from a BalusC's comment:

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

But the following example will work (note that assigning the form an ID is not necessary):

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

It seems Primefaces will not generate an ID for plain HTML tag. I tried with several components, but still not sure. So, Is my conclusion correct? If so, why is this behaviour?

标签: jsfprimefaces

解决方案


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

默认情况下,h:outputTextcom.sun.faces.renderkit.html_basic.TextRenderer(在 Mojarra 的情况下)渲染的组件不渲染 ID。是否呈现 ID 由com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.shouldWriteIdAttribute(UIComponent) 此处确定:

/**
     * @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 没有任何关系。


推荐阅读