2

I get a duplicate ID exception when using composite components conditionally with <c:if> I'm aware of this compile/render time issue but i have really no clue why the example below does not work. Please have a look at the following three simple snippets

A session scoped bean named TestBean which holds a boolean value and two ajax listeners which change this value to true or false:

@Named
@SessionScoped
public class TestBean implements Serializable {

    private boolean isVisible = false;

    public void onSetItemVisible(AjaxBehaviorEvent e) {
        this.isVisible = true;
    }

    public void onSetItemInvisible(AjaxBehaviorEvent e) {
        this.isVisible = false;
    }

    public boolean isItemVisible()  {
        return this.isVisible;
    }
}

A really simple composite component named testCmp:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:cc="http://java.sun.com/jsf/composite">

    <cc:interface/>
    <cc:implementation>
        <h:outputText id="text" value="text"/>
     </cc:implementation>
</html>

And a view page which allows switching between hide/show this composite component

<h:body>
    <h:form id="testForm">

        <c:if test="#{testBean.itemVisible}">
            <test:testCmp id="test1"/>
        </c:if>
        <p/>

        <test:testCmp id="test2"/>
        <p/>

        <!-- show/hide dynamic item -->
        <h:commandLink value="Show Item">
            <f:ajax execute="@this" listener="#{testBean.onSetItemVisible}" render="@form"/> 
        </h:commandLink>
        <br/>
        <h:commandLink value="Hide Item">
            <f:ajax execute="@this" listener="#{testBean.onSetItemInvisible}" render="@form"/>
        </h:commandLink>
    </h:form>
</h:body>

The thing is: i get a duplicate ID Exception when i switch between show/hide. Exception says: "Component ID testForm:test2:text has already been found in the view"

It complains about 'test2' ... the component which is not conditionally added. And when i don't use a composite component and replace it with a standard component like <h:outputText> then anything works well. To reproduce the error it is important, that the composite component is used two times at the same page, one times with and the other times without the condition.

After further searching i found another guy which i think has the same problem like me. But his example looks a bit more complex and is harder to reproduce. Duplicate component ID in JSF using composite component twice in view

Anybody has a clue whats going on here? My setup is JBoss EAP7 with Mojarra JSF 2.2.14 (but i tested with JSF 2.3.0-m11 too) Can someone can confirm this problem? If so i'll create a bug for the mojarray guys.

Community
  • 1
  • 1
fmueller19
  • 21
  • 3
  • Nobody who can confirm this behavior? If it's a Bug in current jsf implementation i can live with it but i haven't found anything in their jira bugtracker and at the moment i have no clue whether i'm doing something wrong or have misunderstood some JSF basics. – fmueller19 Mar 31 '17 at 12:30
  • I've created a Bug here: https://java.net/jira/browse/JAVASERVERFACES-4240 – fmueller19 Apr 12 '17 at 06:00
  • 1
    It has been moved to github: https://github.com/javaserverfaces/mojarra/issues/4244 – Michele Mariotti Jun 12 '17 at 11:18

2 Answers2

2

This is definitely a bug in Mojarra and is related to handling of dynamic component tree modifications in certain situations.

I have created a reproducer, filed an issue and written about my findings to mojarra dev mailing list. Please read more at https://github.com/tuner/mojarra-dynamic-include-reproducer

tuner
  • 326
  • 3
  • 9
1

I got the same issue when using <c:if> inside <cc:implementation>. The only way bypassing this bug I found was using <h:panelGroup rendered="#{condition}"> instead of <c:if test="#{condition}">.

Nik
  • 26
  • 3