如果在 PrimeFaces 的 ui:composition 模板中调用,为什么轮询方法侦听器不起作用?

Why polling method listener does not work if called in ui:composition template in PrimeFaces?

我正在尝试在 PrimeFaces 中进行投票。为什么轮询监听器在<ui:composition template="/pages/layout.xhtml">下调用时没有被调用,而直接调用时它可以工作。

示例如下

我尝试使用 <ui:composition template="/pages/layout.xhtml"> 和不使用它进行轮询。没有 <ui:composition template="/pages/layout.xhtml"> 轮询工作但 <ui:composition template="/pages/layout.xhtml"> 轮询不工作。

layout.xhtml

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html" locale="en" encoding="UTF-8">
    <h:head>
        <title><ui:insert name="pageTitle">SampleApp</ui:insert></title>
        <f:facet name="first">
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta http-equiv="pragma" content="no-cache" />
            <meta http-equiv="cache-control" content="no-cache" />
            <meta http-equiv="expires" content="0" />
        </f:facet>
        <link
            href="${facesContext.externalContext.requestContextPath}/resources/css/styles.css"
            rel="stylesheet" type="text/css" />
        <link
            href="${facesContext.externalContext.requestContextPath}/resources/css/ReportStyle.css"
            rel="stylesheet" type="text/css" />

    </h:head>

    <h:body>
        <h:form id="form" prependId="false" enctype="multipart/form-data">
            <p:layout id="layout" fullPage="true">

                <p:layoutUnit position="north" size="50" resizable="false"
                    closable="false" collapsible="false"
                    style="overflow-x:hidden;overflow-y:hidden">


                    <table>
                        <tr valign="top">
                            <td width='100%'><h:outputText
                                    style="font-weight: bold;font-size: 12px;" immediate="true "
                                    value="SampleApp" /></td>

                            <td><h:outputLink style="font-weight: bold;"
                                    immediate="true"
                                    value="${facesContext.externalContext.requestContextPath}/pages/about.xhtml">About</h:outputLink>
                                <h:outputLink style="font-weight: bold;padding-left:10px"
                                    immediate="true"
                                    value="${facesContext.externalContext.requestContextPath}/pages/contactus.xhtml">Contact</h:outputLink>
</td>
</tr>
<tr>
<td><h:outputText style="font-weight: bold;font-size: 10px;"
                                    immediate="true " value="Welcome, #{loginBean.emailid} " /></td>
                        </tr>

</table>
</p:layoutUnit>
                <p:layoutUnit position="west" resizable="false" closable="false" collapsible="false" size="200">
    <p:menu style="width:95%">

<p:submenu label="Reports">
 <p:menuitem value="Dashboard" onclick="selectComponentLink(this)"                              url="/pages/Report/Dashboard.xhtml" />
</p:submenu>

</p:menu>
</p:layoutUnit>

<p:layoutUnit position="center">
    <p:growl id="growl" life="2000" />
        <ui:insert name="centerContent" />
            </p:layoutUnit>
            </p:layout>
</h:form>
    </h:body>
</f:view>
</html>

Dashboard.xhtml - 轮询不起作用

<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<ui:composition template="/pages/layout.xhtml">

    <ui:define name="pageTitle">
        <h:outputText value="Suites" />
    </ui:define>

    <ui:define name="centerContent">
        <div class="bodycopy parbase">
            <div style="clear: both"></div>
            <div class="content-body">
                <h:form id="test">
                    <h:panelGrid id="reportId1">
                        <h:outputText id="txt_count" value="#{executionBean.number}" />
                        <p:poll interval="3" listener="#{executionBean.increment}"
                            update="txt_count" />
                    </h:panelGrid>
                </h:form>
            </div>
            <div style="clear: both"></div>
        </div>

    </ui:define>
</ui:composition>
</html>

Dashboard2.xhtml - 轮询工作正常

<?xml version="1.0" encoding="UTF-8"?>
<!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:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html" locale="en" encoding="UTF-8">
    <h:head>
        <title><ui:insert name="pageTitle">SampleApp</ui:insert></title>
        <f:facet name="first">
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta http-equiv="pragma" content="no-cache" />
            <meta http-equiv="cache-control" content="no-cache" />
            <meta http-equiv="expires" content="0" />
        </f:facet>
        <link
            href="${facesContext.externalContext.requestContextPath}/resources/css/styles.css"
            rel="stylesheet" type="text/css" />
        <link
            href="${facesContext.externalContext.requestContextPath}/resources/css/ReportStyle.css"
            rel="stylesheet" type="text/css" />

    </h:head>
    <h:body>
        <h:form id="DashboardForm">
            <h:outputText id="txt_count" value="#{executionBean.number}" />
            <p:poll interval="3" listener="#{executionBean.increment}"
                update="txt_count" />

        </h:form>
    </h:body>
</f:view>
</html>

ExecutionBean.java

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name="executionBean")
@SessionScoped
public class ExecutionBean {
        private int number;
        public int getNumber() {
            return number;
        }
        public void increment() {
                System.out.println("number:" + number);
            number++;
        }

}

实际结果:当我 运行 在 Dashboard2.xhtml 中进行轮询时,它正在工作,因为没有定义

并且,

当我 运行 在 Dashboard.xhtml 中进行轮询时,轮询不起作用,因为它在

中定义

在您的 "does not work" 示例中,您的 ui:composition 定义了一个 h:form,它最终位于 layout.xhtml<h:form id="form" prependId="false" enctype="multipart/form-data"> 内部,而在您的第二个示例中你只有一个 <h:form id="DashboardForm"> - nested forms are not allowed!