Bean属性未在动作上更新

时间:2011-12-22 15:59:58

标签: jsf properties managed-bean

我有以下Facelet页面:

        <ui:define name="content">

            <h:form id="selectReport">

                <div>
                    <h:selectOneMenu value="#{reportsController.selectedReport}">
                        <f:selectItems value="#{reportsController.reportsList}"/>
                    </h:selectOneMenu>
                </div>
                <div>

                    <h:commandButton value="Ir al reporte"
                                     action="#{reportsController.goToReport}"/>

                </div>
            </h:form>

            <h:form id="dateRangeSetupForm"
                    rendered="#{reportsController.showDateRangeSetupForm}">
                <div>
                    <h:outputLabel value="Desde:"/>
                    <h:inputText id="fromDate"
                                 value="#{reportsController.fromDate}"/>
                </div>
                <div>
                    <h:outputLabel value="Hasta:"/>
                    <h:inputText id="toDate"
                                 value="#{reportsController.toDate}"/>
                </div>
            </h:form>

            <h:form id="billOfLadingSetupForm"
                    rendered="#{reportsController.showBillOfLadingSetupForm}">
                <div>
                    <h:outputLabel value="# Factura:"/>
                    <h:inputText id="invoiceNumber"
                                 value="#{reportsController.invoiceNumber}"/>
                </div>
            </h:form>


            <h:form id="submitForm">
                <h:commandButton id="processReport" value="Generar Reporte"
                                 action="#{reportsController.processReport}"/>
                <h:commandButton id="goBack" value="Regresar"
                                 action="mainMenu"/>
            </h:form>

        </ui:define>

我让用户选择一个报告,并根据报告呈现表单以设置参数。问题是当我渲染billOfLadingSetupForm并填充inputText时,然后单击processReport按钮,我的bean的invoiceNumber属性未更新,它是设为null

我注意到,如果在填写Return后输入inputText,然后点击processReport按钮,该属性会更新。这很奇怪我之前没有遇到过这个问题。你知道为什么会这样吗?

1 个答案:

答案 0 :(得分:1)

每当提交表单时,只会发送该表单中的数据。其他表格的数据将不会被发送。这是HTML规范。您需要将属于彼此的所有输入和按钮放在相同 <h:form>中。将所有拆分的dateRangeSetupFormbillOfLadingSetupFormsubmitForm表单替换为<h:panelGroup>,并将它们全部放在同一个<h:form>内。

这样的事情:

<ui:define name="content">
    <h:form>
        <div>
            <h:selectOneMenu value="#{reportsController.selectedReport}">
                <f:selectItems value="#{reportsController.reportsList}"/>
            </h:selectOneMenu>
        </div>
        <div>
            <h:commandButton value="Ir al reporte" action="#{reportsController.goToReport}"/>
        </div>
    </h:form>

    <h:form>
        <h:panelGroup id="dateRangeSetupForm" rendered="#{reportsController.showDateRangeSetupForm}">
            <div>
                <h:outputLabel value="Desde:"/>
                <h:inputText id="fromDate" value="#{reportsController.fromDate}"/>
            </div>
            <div>
                <h:outputLabel value="Hasta:"/>
                <h:inputText id="toDate" value="#{reportsController.toDate}"/>
            </div>
        </h:panelGroup>

        <h:panelGroup id="billOfLadingSetupForm" rendered="#{reportsController.showBillOfLadingSetupForm}">
            <div>
                <h:outputLabel value="# Factura:"/>
                <h:inputText id="invoiceNumber" value="#{reportsController.invoiceNumber}"/>
            </div>
        </h:panelGroup>

        <h:commandButton id="processReport" value="Generar Reporte" action="#{reportsController.processReport}"/>
        <h:commandButton id="goBack" value="Regresar" action="mainMenu"/>
    </h:form>
</ui:define>

确保将bean放入视图范围。否则,将不保留rendered属性的条件。另请参阅commandButton/commandLink/ajax action/listener method not invoked or input value not updated