Dynamic “for” property of p:tooltip does not work

2019-05-21 02:24发布

问题:

I am trying to attach tooptips to commandButtons in a ui:repeat loop. I create ids of commandButtons dynamically and "for" properties of tooltips too. This gives following error:

    HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Cannot find component "a_test" in view.
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:325)
    org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
root cause

javax.faces.FacesException: Cannot find component "a_test" in view.
    org.primefaces.component.tooltip.TooltipRenderer.getTarget(TooltipRenderer.java:93)
    org.primefaces.component.tooltip.TooltipRenderer.encodeScript(TooltipRenderer.java:66)
    org.primefaces.component.tooltip.TooltipRenderer.encodeEnd(TooltipRenderer.java:35)
    javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:883)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1659)
    com.sun.faces.facelets.component.RepeatRenderer.encodeChildren(RepeatRenderer.java:104)
    com.sun.faces.facelets.component.UIRepeat.process(UIRepeat.java:504)
    com.sun.faces.facelets.component.UIRepeat.encodeChildren(UIRepeat.java:958)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
    javax.faces.render.Renderer.encodeChildren(Renderer.java:168)
    javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:853)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1652)
    javax.faces.component.UIComponent.encodeAll(UIComponent.java:1655)
    com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:399)
    com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:121)
    com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:313)
    org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.12 logs.

Apache Tomcat/7.0.12

My jsf follows:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets">

<h:head></h:head>
<h:form id="MyForm">
    <ui:repeat var="rs"
        value="#{rezervasyonBean.rezervasyonSaatleriListesi}">
        <p:commandButton action="#" value="#{rs.saatAraligi}"
            update="@form" 
            id="#{rs.saatAraligi}_test" 
        />

        <p:tooltip for="#{rs.saatAraligi}_test" value="test text"/>
    </ui:repeat>

</h:form>
</html>

rezervasyonBean.rezervasyonSaatleriListesi is as list and elements of the rs.saatAraligi are a,b,c.

My question is: how can I identify ui components dynamically and "for" attribute of p:tooltip in the same way?

Regards.

回答1:

The ID attribute of JSF UI components is evaluated during view build time, however the <ui:repeat> runs during view render time and thus #{rs.saatAraligi} would evaluate to null and hence the ID of the command button would always be _test.

Just remove the #{rs.saatAraligi} expression from the id and for. You don't need it here. JSF will automatically prefix the iteration index of <ui:repeat> and thus ensure uniqueness of the ID.

<p:commandButton action="#" value="#{rs.saatAraligi}"
    update="@form" 
    id="test" 
/>
<p:tooltip for="test" value="test text"/>

See also:

  • JSTL in JSF2 Facelets... makes sense? - the same story applies to id attribute of JSF UI components