繁体   English   中英

使用ajax时如何识别Java Server Faces 2.0复合组件?

[英]How to recognize Java Server Faces 2.0 composite components when using ajax?

我具有以下Java Server Faces 2.0复合组件。 请注意,我使用逐字记录

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>

为了使用ajax,我已经做了(Notice render属性)

<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>

我的CustomerSearchController如下所示

private Customer customer;

// getter's and setter's

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

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

    customer.setAge(30);
}

CustomerSearchController和Customer都是托管Bean。 但是当我打电话给ajax请求时,它抱怨: 搜索:未找到结果

我该怎么解决这个问题?

<f:ajax render>应该指向HTML DOM树中的现有客户端ID。 但是,由于search:result元素在HTML DOM树中不可用,因为它不是由服务器端呈现的,因此JS / ajax在HTML DOM树中找不到要更新/呈现的任何内容。

将其包装在HTML DOM树中始终可用的另一个元素中,以便Ajax可以找到它。

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

与实际问题无关,请注意f:verbatim应该只包含逐字记录(纯HTML),而不包含JSF组件。 将其替换为h:panelGroup

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM