简体   繁体   中英

PrimeFaces button in Tab Doesn't Navigate

I'm developing this project using JSF 2.0, NetBeans 6.9.1, GlassFish 3.1, Mojarra 2.0.3, and PrimeFaces 2.2-RC2.

My question is similar to this question , but even though I was able to make one of my buttons work, the other one does not. Here's my situation: I have a dataTable with a column containing two links, one to edit line data, one to view line data. Clicking either the "Edit" or "View" link takes you to a detail page with <h:panelGrid> components inside <p:tabView> components. My detail page has two <p:tabView> components, one for editing, one for just viewing data. Which one you see depends on the <c:if> statements. The button in the first <p:tabView> on the page (by that I mean first working your way down from the top) works as I would like it to, this happens to be the "Edit" component. The button in the other <p:tabView> does not work. I set up break points and went through the debugger, the second button doesn't seem to fire an ActionEvent regardless of what I try. Here's my code for the page:

            <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>

I've tried everything I can think of for the second button: ajax="false"; onclick="submit()"; ajax="false" onclick="submit"; I've tried implicit navigation with a redirect as well as a navigation rule with a redirect. The action methods userDetailBean.submitChanges() and userDetailBean.navigate() both return the same string, which corresponds to a navigation rule in my faces-config file. The submitChanges method gets called, but my navigate method does not. I'm not filling the fields with any data at this point, I'm just trying to get the basic navigation to work. Why isn't my commandButton on the second tabView working?

Another reason that a commandbutton isn't been invoked is that the rendered attribute of the button or one of its parents evaluated false during apply request values phase of the form submit.

You have actually none, but those <c:if> tags does similar thing, only during view build time instead of view render time. The view params are not applied during building the view, but only thereafter. That's exactly why #{userDetailBean.view} is always empty.

I have never used such a construct with <c:if> , but I've had similar views like this where this all just works with a @ViewScoped bean where the rendering property is been applied on a fullworthy JSF component instead of <c:if> .

As first step, get rid of <c:if> s and move their tests into rendered attribute of the <h:form> s they are wrapping:

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

If in vain (I guess that this will fail because you aren't using ajax and the viewparams are not preserved) and the bean is indeed @RequestScoped , then you need to mark it @ViewScoped to preserve the rendering conditions.

Related questions:


Unrelated to the problem, I'd rather use <h:panelGroup /> instead of <h:inputHidden /> to "fill" the empty cells of the table. They are cheaper to encode and decode and also ends in better HTML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM