【问题标题】:PrimeFaces button in Tab Doesn't Navigate选项卡中的 PrimeFaces 按钮无法导航
【发布时间】:2011-06-16 04:44:58
【问题描述】:

我正在使用 JSF 2.0、NetBeans 6.9.1、GlassFish 3.1、Mojarra 2.0.3 和 PrimeFaces 2.2-RC2 开发这个项目。

我的问题类似于this question,但即使我能够使我的一个按钮工作,另一个却不能。这是我的情况:我有一个包含两个链接的列的 dataTable,一个用于编辑行数据,一个用于查看行数据。单击“编辑”或“查看”链接将转到包含<h:panelGrid> 组件的详细信息页面,该组件位于<p:tabView> 组件中。我的详细信息页面有两个<p:tabView> 组件,一个用于编辑,一个用于查看数据。您看到哪一个取决于<c:if> 语句。页面上第一个<p:tabView> 中的按钮(我的意思是首先从顶部向下工作)按我的意愿工作,这恰好是“编辑”组件。另一个<p:tabView> 中的按钮不起作用。我设置了断点并通过了调试器,无论我尝试什么,第二个按钮似乎都不会触发 ActionEvent。这是我的页面代码:

            <ui:define name="metadata">
                <f:metadata>
                    <f:viewParam name="id" value="#{userDetailBean.selectedUserId}" />
                    <f:viewParam name="action" value="#{userDetailBean.view}" />
                </f:metadata>


            </ui:define>


            <ui:define name="main_base_content_top">
                <h:form>
                    <p:toolbar>
                        <p:toolbarGroup align="left">
                            <h:commandButton value="Return to User List" action="user_grid" />
                        </p:toolbarGroup>
                    </p:toolbar>
                </h:form>
            </ui:define>

            <ui:define name="main_base_content">

                    <c:if test="#{empty userDetailBean.view}">
                        <h:form>
                        <table align="center">
                            <tr>
                                <td>
                                    <p:tabView>
                                        <p:tab title="#{msgs.UserDetailTabTitle}">
                                            <h:panelGrid columns="4">
                                                <h:outputLabel value="#{msgs.UserDetailFirstName}" for="fname" />
                                                <h:inputText id="fname" value="#{userDetailBean.firstName}" />
                                                <h:outputLabel value="#{msgs.UserDetailJobTitle}" for="jtitle" />
                                                <h:inputText id="jtitle" value="#{userDetailBean.jobTitle}" />
                                                <h:outputLabel value="#{msgs.UserDetailLastName}" for="lname" />
                                                <h:inputText id="lname" value="#{userDetailBean.lastName}" />
                                                <h:outputLabel value="#{msgs.UserDetailEmployeeId}" for="empid" />
                                                <h:inputText id="empid" value="#{userDetailBean.id}" />
                                                <h:outputLabel value="#{msgs.UserDetailDateOfHire}" for="doh" />
                                                <h:inputText id="doh" value="#{userDetailBean.DOH}" />
                                                <h:outputLabel value="#{msgs.UserDetailLocation}" for="location" />
                                                <h:inputText id="location" value="#{userDetailBean.location}" />
                                                <h:outputLabel value="#{msgs.UserDetailStatus}" for="status" />
                                                <h:inputText id="status" value="#{userDetailBean.status}" />
                                                <h:inputHidden /> <h:inputHidden /> <h:inputHidden /> <h:inputHidden /> <h:inputHidden />
                                                &nbsp;
                                                <h:outputLabel value="#{msgs.UserDetailComments}" for="comments" />
                                                <h:inputTextarea id="comments" value="#{userDetailBean.comments}" />
                                                <h:inputHidden />
                                                <p:commandButton ajax="false" action="#{userDetailBean.submitChanges()}" value="Submit Changes" />
                                            </h:panelGrid>
                                        </p:tab>
                                    </p:tabView>
                                </td>
                            </tr>

                        </table>
                        </h:form>
                    </c:if>
                    <c:if test="#{! empty userDetailBean.view}">
                        <h:form>
                        <table align="center">
                            <tr>
                                <td>
                                    <p:tabView>
                                        <p:tab title="#{msgs.UserDetailViewTabTitle}">
                                            <h:panelGrid columns="4">
                                                <h:outputLabel value="#{msgs.UserDetailFirstName}" for="fname" />
                                                <h:outputText id="fname" value="#{userGridBean.selectedUser.firstName}" />
                                                <h:outputLabel value="#{msgs.UserDetailJobTitle}" for="jtitle" />
                                                <h:outputText id="jtitle" value="#{userGridBean.selectedUser.jobTitle}" />
                                                <h:outputLabel value="#{msgs.UserDetailLastName}" for="lname" />
                                                <h:outputText id="lname" value="#{userGridBean.selectedUser.lastName}" />
                                                <h:outputLabel value="#{msgs.UserDetailEmployeeId}" for="empid" />
                                                <h:outputText id="empid" value="#{userGridBean.selectedUser.id}" />
                                                <h:outputLabel value="#{msgs.UserDetailDateOfHire}" for="doh" />
                                                <h:outputText id="doh" value="#{userGridBean.selectedUser.DOH}" />
                                                <h:outputLabel value="#{msgs.UserDetailLocation}" for="location" />
                                                <h:outputText id="location" value="#{userGridBean.selectedUser.location}" />
                                                <h:outputLabel value="#{msgs.UserDetailStatus}" for="status" />
                                                <h:outputText id="status" value="#{userGridBean.selectedUser.status}" />
                                                <h:inputHidden /> <h:inputHidden /> <h:inputHidden /> <h:inputHidden /> <h:inputHidden />
                                                &nbsp;
                                                <h:outputLabel value="#{msgs.UserDetailComments}" for="comments" />
                                                <h:outputText id="comments" value="#{userGridBean.selectedUser.comments}" />
                                                <h:inputHidden /> <h:inputHidden /> <h:inputHidden />
                                                <p:commandButton onclick="submit()" ajax="false" action="#{userDetailBean.navigation()}" value="Retur to User Grid" />
                                            </h:panelGrid>
                                        </p:tab>
                                    </p:tabView>
                                </td>
                            </tr>
                        </table>
                        </h:form>
                    </c:if>                 

            </ui:define>
        </ui:composition>        
    </body>

我已经为第二个按钮尝试了所有我能想到的方法:ajax="false"; onclick="提交()"; ajax="假" onclick="提交";我已经尝试了带有重定向的隐式导航以及带有重定向的导航规则。操作方法 userDetailBean.submitChanges() 和 userDetailBean.navigate() 都返回相同的字符串,这对应于我的 faces-config 文件中的导航规则。 submitChanges 方法被调用,但我的导航方法没有。此时我没有用任何数据填充字段,我只是想让基本导航工作。为什么我在第二个 tabView 上的 commandButton 不起作用?

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    没有调用命令按钮的另一个原因是按钮的rendered 属性或其父级之一在表单提交的应用请求值阶段评估了false

    实际上你没有,但是那些&lt;c:if&gt; 标签做类似的事情,只是在视图构建时间而不是视图渲染时间。视图参数在构建视图期间不应用,但仅在之后应用。这正是#{userDetailBean.view} 始终为空的原因。

    我从未在&lt;c:if&gt; 中使用过这样的构造,但我也有类似的看法,其中这一切都只适用于@ViewScoped bean,其中呈现属性应用于完全值得的JSF 组件而不是@ 987654328@.

    第一步,摆脱&lt;c:if&gt;s 并将他们的测试移动到他们正在包装的&lt;h:form&gt;s 的rendered 属性中:

    <h:form rendered="#{empty userDetailBean.view}">
        ...
    </h:form>
    <h:form rendered="#{not empty userDetailBean.view}">
        ...
    </h:form>
    

    如果徒劳(我猜这会失败,因为您没有使用 ajax 并且未保留视图参数)并且 bean 确实是 @RequestScoped,那么您需要将其标记为 @ViewScoped 以保留渲染条件.

    相关问题:


    与问题无关,我宁愿使用&lt;h:panelGroup /&gt; 而不是&lt;h:inputHidden /&gt; 来“填充”表格的空单元格。它们的编码和解码成本更低,而且以更好的 HTML 结尾。

    【讨论】:

    • 好了,告白时间到了。再次浏览我的代码,我尝试在第二个按钮上实现 actionListener 方法。我很绝望。在取出它时,我留下了一个属性的残留物,导致了一个错误。在修复该问题时,我注意到我的操作方法调用与实际方法名称不匹配。实施您的建议,并修复对我的操作方法的引用,导航现在可以工作了。经验教训:使用 NetBeans 自动选择工具(或者只是用我的方法名称仔细检查我的方法调用)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2013-05-03
    相关资源
    最近更新 更多