JSF2:表单AJAX操作使得另一种形式的视图状态丢失视图、表单、形式、状态

2023-09-10 21:04:40 作者:我是天使也是贱货

我有一个简单的页面,包括两种形式。它们不能被同时显示(一个切换其他)。这里是code:

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()和 questionStep2Cancelled()的简单切换中使用的布尔值的渲染的属性。

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.

我看到了this问题,并这个链接,你可以看到,我已经添加的形式IDS在渲染的属性,但它仍然无法正常工作。任何想法?

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?

感谢

推荐答案

您正在有条件地呈现形式本身,因此其它形式是不是在HTML DOM树可用物理此刻的Ajax请求是prepared。如果换在其他形式的内容&LT; H:panelGroup中&GT; 和移动渲染属性到那里,然后应该去工作。

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.