0

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?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Tea
  • 873
  • 2
  • 7
  • 25
  • Effectively you state that using an `h:commandButton` instead of a PrimeFaces one makes it work (since the `p:commandButton` is the only 'PrimeFaces' related code in your post – Kukeltje Mar 11 '19 at 15:52
  • Maybe no, mate. I thought h:commandButton don't have update element or partial update by selector – Tea Mar 11 '19 at 16:42
  • But is that 'selector update' relevant to the problem? (effectively it is as you most likely already know, but you could have investigated that and added that to the question) – Kukeltje Mar 11 '19 at 16:54
  • Just confuse cause primefaces component has an Id, Html tag is not. I thought they are the same mechanism – Tea Mar 11 '19 at 23:46

1 Answers1

3

Assuming you are asking why there is no ID attribute on your <span> element rendered by <h:outputText value="#{bean.text1}" styleClass="myClass" />:

By default, the h:outputText component rendered by com.sun.faces.renderkit.html_basic.TextRenderer (in case of Mojarra) does not render an ID. Whether or not an ID is rendered is determined by 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())));
}

All of this is plain JSF and does not have any relation to primefaces.

Selaron
  • 6,105
  • 4
  • 31
  • 39