无法定位动态生成的panelGroup

时间:2014-12-18 17:46:53

标签: jsf jsf-2 primefaces

我的申请中有以下结构:

<h:outputLabel value="Regiões :" />
<p:autoComplete multiple="true" value="#{investimento.regioesSelecionadas}"
                completeMethod="#{investimento.completaRegiao}"
                var="reg" itemLabel="#{reg.label}" itemValue="#{reg}" 
                converter="entityConverter" forceSelection="true">

    <p:column>
        <h:outputText value="#{reg.label}" />
    </p:column>
    <p:ajax event="itemSelect" update="regiaoCol" />
    <p:ajax event="itemUnselect" update="regiaoCol" />
    <p:ajax event="itemUnselect" listener="#{investimento.tiraUmaRegiao}"/>
</p:autoComplete>

<h:outputText />
<h:panelGroup id="regiaoCol">
    <c:forEach items="#{investimento.regioesSelecionadas}" var="regSel" rendered="#{!investimento.regioesSelecionadas.isEmpty()}">
        <h:panelGrid columns="2">
            <h:outputLabel value="Região #{regSel.label} - Estados:" />
            <p:autoComplete multiple="true" value="#{investimento.estadosSelecionados}"
                            var="est" itemLabel="#{est.label}" itemValue="#{est}"
                            completeMethod="#{investimento.completaEstado}" 
                            converter="entityConverter" forceSelection="true" cash="true">
                <f:attribute name="regPar" value="#{regSel}" />
                <p:column>
                    <h:outputText value="#{est.label}" />
                </p:column>
                <p:ajax event="itemSelect" update="#{investimento.idComposta(est, regSel)}" />
                <p:ajax event="itemUnselect" update="regiaoCol" />
                <p:ajax event="itemUnselect" update="#{investimento.idComposta(est, regSel)}"/>
                <p:ajax event="itemUnselect" update="#{investimento.idComposta(est, regSel)}" listener="#{investimento.tiraUmEstado}"/>
            </p:autoComplete>

            <h:outputLabel />
            <h:panelGroup id="#{investimento.idComposta(est, regSel)}">
                <c:forEach items="#{investimento.estadosSelecionados}" var="estSel">
                    <h:panelGrid columns="2">
                        <h:outputLabel value="Estado #{estSel.label} - Municipios:               #{investimento.idComposta(est, regSel)}" />
                        <p:autoComplete multiple="true" value="#{investimento.municipiosSelecionados}"
                                        var="mun" itemLabel="#{mun.label}" itemValue="#{mun}"
                                        completeMethod="#{investimento.completaMunicipio}" maxResults="8"
                                        converter="entityConverter" forceSelection="true" cash="true">
                            <f:attribute name="estPar" value="#{estSel}" />
                            <p:column>
                                <h:outputText value="#{mun.label}" />
                            </p:column>
                        </p:autoComplete>
                    </h:panelGrid>
                </c:forEach>
            </h:panelGroup>
        </h:panelGrid>
    </c:forEach>
</h:panelGroup>

所以,每当我选择“Região”时,都会在“regiaoCol”中为用户选择新的选项。但是有一个问题:当我删除“Região”时,再次添加它并尝试再次删除它会导致以下错误:

> Grave: Error Rendering View[/InsI.xhtml]
javax.faces.FacesException: Cannot find component with expression "munEst2Reg3" referenced from "form:j_idt190".
    at org.primefaces.expression.SearchExpressionFacade.resolveComponentInternal(SearchExpressionFacade.java:422)
    at org.primefaces.expression.SearchExpressionFacade.resolveComponentForClient(SearchExpressionFacade.java:200)
    at org.primefaces.expression.SearchExpressionFacade.resolveComponentsForClient(SearchExpressionFacade.java:147)
    at org.primefaces.util.AjaxRequestBuilder.addExpressions(AjaxRequestBuilder.java:92)
    at org.primefaces.util.AjaxRequestBuilder.update(AjaxRequestBuilder.java:85)
    at org.primefaces.behavior.ajax.AjaxBehaviorRenderer.getScript(AjaxBehaviorRenderer.java:80)
    at javax.faces.component.behavior.ClientBehaviorBase.getScript(ClientBehaviorBase.java:103)
    at org.primefaces.renderkit.CoreRenderer.encodeClientBehaviors(CoreRenderer.java:458)

我认为,这是由浏览器查找该引用引起的,在本例中为“munEst2Reg3”并且它不在那里。但是:为什么它会抛出错误?与第一次删除“Região”有什么不同?我该如何解决这个问题?每次取消选举后我应该提交页面吗? (这不是最友好的用户选择)

1 个答案:

答案 0 :(得分:0)

Tiny的回应让我走上了正确的轨道,经过一些调整后我发现了一些事情:

  1. 由于所有实例都在改变相同的列表,所以它们都有一个事件或一个将删除另一个的输入是最重要的;
  2. 您还必须注意dataTable的行为,并保证他只获取您感兴趣的值;
  3. 我的代码最终如何:

    <h:outputLabel value="Regiões :" />
                    <p:autoComplete multiple="true" value="#{investimento.regioesSelecionadas}"
                                    completeMethod="#{investimento.completaRegiao}"
                                    var="reg" itemLabel="#{reg.label}" itemValue="#{reg}" 
                                    converter="entityConverter" forceSelection="true">
    
                        <p:column>
                            <h:outputText value="#{reg.label}" />
                        </p:column>
                        <p:ajax event="itemSelect" update="regiaoCol" />
                        <p:ajax event="itemUnselect" update="regiaoCol" listener="#{investimento.tiraUmaRegiao}"/>
                    </p:autoComplete>
    
                    <h:outputText />
                    <h:panelGroup id="regiaoCol">
                        <p:dataTable value="#{investimento.regioesSelecionadas}" var="regSel" rendered="#{investimento.regioesSelecionadas.isEmpty() == false}">
                            <p:column>
                                <h:outputLabel value="Região #{regSel.label} - Estados:" />
                            </p:column>
    
                            <p:column>
                                <p:autoComplete multiple="true" value="#{investimento.estadosSelecionados}"
                                                var="est" itemLabel="#{est.label}" itemValue="#{est}"
                                                completeMethod="#{investimento.completaEstado}" 
                                                converter="entityConverter" forceSelection="true">
                                    <f:attribute name="regPar" value="#{regSel}" />
    
                                    <p:column>
                                        <h:outputText value="#{est.label}" />
                                    </p:column>
    
                                    <p:ajax event="itemSelect" update="#{investimento.idComposta()}" >
                                        <f:attribute name="regiaoPar" value="#{regSel}" />
                                    </p:ajax>
    
                                    <p:ajax event="itemUnselect" update="#{investimento.idComposta()}" listener="#{investimento.tiraUmEstado}">
                                        <f:attribute name="regiaoPar" value="#{regSel}" />
                                    </p:ajax> 
                                </p:autoComplete>
                            </p:column>
    
                            <p:column>
                                <h:panelGroup id="#{investimento.idComposta()}">
                                    <f:attribute name="regiaoPar" value="#{regSel}" />
    
                                    <p:dataTable value="#{investimento.meusEstSel()}" var="estSel" rendered="#{investimento.meusEstSel().isEmpty() == false}">
                                        <f:attribute name="regiaoPar" value="#{regSel}" />
                                        <p:column>
                                            <h:outputLabel value="Estado #{estSel.label} - Municipios:" />
                                        </p:column>
                                        <p:column>
                                            <p:autoComplete multiple="true" value="#{investimento.municipiosSelecionados}"
                                                            var="mun" itemLabel="#{mun.label}" itemValue="#{mun}"
                                                            completeMethod="#{investimento.completaMunicipio}" maxResults="8"
                                                            converter="entityConverter" forceSelection="true">
                                                <f:attribute name="estPar" value="#{estSel}" />
    
                                                <p:column>
                                                    <h:outputText value="#{mun.label}" />
                                                </p:column>
                                                <p:ajax event="itemSelect" />
                                                <p:ajax event="itemUnselect" />
                                            </p:autoComplete>
                                        </p:column>
                                    </p:dataTable>
                                </h:panelGroup>
                            </p:column>
                        </p:dataTable>
                    </h:panelGroup>