通过另一个xhtml文件中的id更新xhtml文件中的表单

时间:2016-10-08 16:58:51

标签: jsf primefaces jsf-2 popup xhtml

这是我的代码

的index.xhtml

<h1>Student List</h1>
    <h:form id="studentListForm">
        <h:dataTable id="studentsList"
                     style="border:2px solid;"
                     var="student"
                     value="#{tableItems.students}">
            <h:column>
                <f:facet name="First Name"/>
                <h:outputText value="#{student.firstName}"/>
            </h:column>
            <h:column>
                <f:facet name="Last Name"/>
                <h:outputText value="#{student.lastName}"/>
            </h:column>
            <h:column>
                <f:facet name="Faculty Name"/>
                <h:outputText value="#{student.faculty.name}"/>
            </h:column>
        </h:dataTable>
        <br/>
        <p:commandButton value="Add Student"
                         update=":studentAdding"
                         oncomplete="PF('studentAddingDialog').show();"/>
    </h:form>

addStudent.xhtml

<p:dialog id="studentAddingDialogId" modal="true"
              header="Add Student"
              widgetVar="studentAddingDialog"
              closeOnEscape="true">

        <h:form id="studentAdding">
            <h:outputLabel value="Student first name"/>
            <h:inputText id="firstName"
                         value="#{tableItems.studentFirstName}"
                         required="true"/>
            <br/>
            <h:outputLabel value="Student last name"/>
            <h:inputText id="lastName"
                         value="#{tableItems.studentLastName}"
                         required="true"/>
            <br/>
            <h:outputLabel value="Choose student faculty"/>
            <h:selectOneMenu id="selectFaculty"
                             value="#{tableItems.studentFaculty}"
                             required="true">

                <f:selectItem itemLabel="--Select--" itemValue=""/>
                <f:selectItems value="#{faculties.faculties}"
                               var="itam"
                               itemValue="#{itam.name}"
                               itemLabel="#{itam.name}"/>
            </h:selectOneMenu>
            <p:commandButton value="Submit"
                             actionListener="#{tableItems.addStudent}"
                             oncomplete="PF('studentAddingDialog').hide();"
                             update="studentListForm"/>
        </h:form>
    </p:dialog>

运行程序后,我看到一个错误(HTTP状态500 - 无法找到表达式组件&#34;:studentAdding&#34;引自&#34; studentListForm:j_idt10&#34;。) 为什么它无法从另一个xhtml文件中识别出它的id?

2 个答案:

答案 0 :(得分:0)

在这里,您尝试打开位于其他页面中的对话框,因此当您尝试更新studentAdding时,它找不到表单,这解释了错误Cannot find component for expression ":studentAdding" referenced from "studentListForm:j_idt10",我建议您添加主页addStudent.xhtml中的页面index.xhtml

<ui:include src="/../studentAdding.xhtml">
    <ui:param name="updateForm" value=":studentListForm"/>
</ui:include>

您必须确保要从studentAdding.xhtml更新并且位于index.xhtml的任何内容,您必须将其作为参数传递给studentAdding.xhtml,作为示例您正在更新提交按钮的studentListForm,因此在您更新时使用您传递的参数的名称,即updateForm,如下所示:

<p:commandButton value="Submit"
                 actionListener="#{tableItems.addStudent}"
                 oncomplete="PF('studentAddingDialog').hide();"
                 update="updateForm "/>

答案 1 :(得分:0)

要使用包含的xhtml文件中的任何xhtml元素,您可以将其作为<ui:param name="yourElementAlias" value=":pathTo:Element"/>进行传递。它允许您使用包含的源文件中的此特定元素:

<ui:include src="/../studentAdding.xhtml">
    <ui:param name="yourElementAlias" value=":pathTo:Element"/>
</ui:include>

然后,要在studentAdding.xhtml中使用此元素(例如更新它),请按照以下步骤操作:

<p:commandButton value="Submit"
             actionListener="#{tableItems.addStudent}"
             oncomplete="PF('studentAddingDialog').hide();"
             update="#{yourElementAlias}"/>