【问题标题】:Update form in xhtml file by id from another xhtml file [duplicate]通过来自另一个 xhtml 文件的 id 更新 xhtml 文件中的表单 [重复]
【发布时间】:2017-02-17 11:59:45
【问题描述】:

这是我的代码

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 - 找不到从“studentListForm:j_idt10”引用的表达式“:studentAdding”的组件。) 为什么它无法识别来自另一个 xhtml 文件的 id 表单?

【问题讨论】:

    标签: jsf primefaces jsf-2 popup xhtml


    【解决方案1】:

    在这里您尝试打开位于另一个页面中的对话框,因此当您尝试更新studentAdding 时,它找不到解释错误Cannot find component for expression ":studentAdding" referenced from "studentListForm:j_idt10" 的表单,我建议您包含页面@987654323 @在主页上是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 "/>
    

    【讨论】:

      【解决方案2】:

      要使用包含的 xhtml 文件中的任何 xhtml 元素,您可以将其传递为 &lt;ui:param name="yourElementAlias" value=":pathTo:Element"/&gt;。它允许您使用包含的源文件中的这个特定元素:

      <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}"/>
      

      【讨论】:

        猜你喜欢
        • 2011-11-22
        • 1970-01-01
        • 2023-03-05
        • 1970-01-01
        • 2013-05-29
        • 2012-04-21
        • 2021-03-27
        • 2014-07-05
        • 2011-12-23
        相关资源
        最近更新 更多