JSF2: Form ajax action makes another form's view state to be lost

ぐ巨炮叔叔 提交于 2019-12-11 04:57:36

问题


I have a simple page consisting of two forms. They cannot be displayed simultaneously (one toggles the other). Here is the code:

<h:body>

    <ui:composition template="/elements/templateWithMenu.xhtml">            
        <ui:define name="content">

            <p:dialog id="question1DialogId" widgetVar="question1Dialog" resizable="false" closable="false" visible="true">                             

                <h:panelGroup id="questionStep1Group">
                    <h:form id="questionStep1Form" rendered="#{newPoll2.displayQuestionStep1}">

                        <h:commandLink value="next" action="#{newPoll2.questionStep1Completed()}" class="btn" >
                            <f:ajax execute="@form" render=":questionStep2Form :questionStep1Group :questionStep2Group"/>
                        </h:commandLink>

                    </h:form>
                </h:panelGroup>

                <h:panelGroup id="questionStep2Group">
                    <h:form id="questionStep2Form"  rendered="#{newPoll2.displayQuestionStep2}">

                        <h:commandLink value="cancel" action="#{newPoll2.questionStep2Cancelled()}" class="btn" >
                            <f:ajax execute="@form" render=":questionStep1Form :questionStep1Group :questionStep2Group" />
                        </h:commandLink>

                    </h:form>
                </h:panelGroup>

            </p:dialog>

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

</h:body>

questionStep1Completed() and questionStep2Cancelled() simply toggle the booleans used in rendered attribute.

The problem is with the view states. When the page is first loaded, clicking on "next" will work, and it will be replaced with the "cancel" link. But when I click on the "cancel" link, it needs to be clicked twice in order for it to be replaced with with the "next" link. I can see that the first click makes the request with no view state, and the second (successful) one makes a request with it.

I saw this question, and this link, and as you can see, I've added form ids in the render attribute, but it still doesn't work. Any ideas?

Thanks


回答1:


You're conditionally rendering the forms themselves and hence the other form isn't physically available in the HTML DOM tree at the moment the ajax request is been prepared. If you wrap the form's content in another <h:panelGroup> and move the rendered attribute to there, then it ought to work.



来源:https://stackoverflow.com/questions/11164361/jsf2-form-ajax-action-makes-another-forms-view-state-to-be-lost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!