How to recognize Java Server Faces 2.0 composite c

2019-08-15 04:49发布

问题:

I have the following Java Server Faces 2.0 composite component. Notice i am using verbatim

resources/customer/customer.xhtml

<composite:interface>
    <composite:attribute name="id" required="false"/>
    <composite:attribute name="firstName" required="false"/>
    <composite:attribute name="lastName" required="false"/>
    <composite:attribute name="age" required="false"/>
    <composite:attribute name="rendered" required="false"/>
</composite:interface>
<composite:implementation>
    <f:verbatim id="#{cc.attrs.id}" rendered="#{cc.attrs.rendered}">
        <div>
            <div>
                <p>First name</p>
                <h:outputText value="#{cc.attrs.firstName}"/>
            </div>
            <div>
                <p>Last name</p>
                <h:outputText value="#{cc.attrs.lastName}"/>
            </div>
            <div>
                <p>Age</p>
                <h:outputText value="#{cc.attrs.age}"/>
            </div>
        </div>
    </f:verbatim>
</composite:implementation>

In order to use ajax, i have done (Notice render attribute)

<h:form id="search">
<div>
    <h:commandButton value="Search" action="#{customerSearchController.search}">
        <f:ajax execute="@form" render="search:result"/>
    </h:commandButton>
</div>
<customer:customer id="result"
                   rendered="#{customerSearchController.customer != null}"
                   firstName="#{customerSearchController.customer.firstName}"
                   lastName="#{customerSearchController.customer.lastName}"
                   age="#{customerSearchController.customer.age}"/>
</h:form>

My CustomerSearchController is shown as follows

private Customer customer;

// getter's and setter's

public void search() {
    customer = new Customer();

    customer.setFirstName("First");
    customer.setLastName("Last");

    customer.setAge(30);
}

Both CustomerSearchController and Customer are managed beans. But when i call ajax request, it complains: search:result not found

What should do i to solve this issue ???

回答1:

<f:ajax render> should point to an existing client ID in the HTML DOM tree. However, since search:result element is not available in the HTML DOM tree because it's not rendered by the server side, JS/ajax cannot find anything in HTML DOM tree to update/render.

Wrap it in another element which is always available in the HTML DOM tree so that Ajax can locate it.

<h:panelGroup id="result">
    <customer:customer rendered="..." />
</h:panelGroup>

Unrelated to the actual problem, note that f:verbatim is supposed to contain only verbatim (plain HTML), not JSF components. Replace it by h:panelGroup.