0

I have a backing bean method (session scoped) within two nested forms which will not fire.

I set out the question with a generic example which exhibits the problem.

I would appreciate an insight into how/why the forms, dialogs and appendToBody tag are causing the problem.

To clarify, the action in contentsOfDialogTwo.xhtml - nestedDialogsBean.actionOnClickOfDialogTwoItem() - does not fire. The javascript alert is displayed, but a breakpoint on the action on the backing bean shows that this is not called.

Template View:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
<ui:composition>
    <ui:insert name="content"/>
</ui:composition>
</html>

Parent View:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<ui:composition template="../templates/layout.xhtml">
    <ui:define name="content">
        <h:form>
            <p:panel>
                <p:tabView id="exampleTabView" dynamic="false">
                    <p:tab id="nestedDialogsTab" title="Nested Dialogs Tab">
                        <ui:insert>
                            <ui:include src="childView.xhtml"></ui:include>
                        </ui:insert>
                    </p:tab>
               </p:tabView>
            </p:panel>
         </h:form>
    </ui:define>
</ui:composition>
</html>

Child view:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:p="http://primefaces.org/ui">
    <ui:composition>
    <p:commandButton 
    id="openDialog1" 
    value="Open Dialog 1"
    action="#{nestedDialogsBean.actionOnOpenOfDialog1()}"
    oncomplete="dialogOneVar.show()">
     </p:commandButton>
    <p:dialog id="dialogOne" header="Panel One" widgetVar="dialogOneVar"
            resizable="false" modal="true" closable="true" dynamic="true"
            hideEffect="fold" draggable="false" height="700" width="1000">
            <ui:insert>
                <ui:include src="contentsOfDialogOne.xhtml"></ui:include>
            </ui:insert>
        </p:dialog>
    </ui:composition>
    </html>

contentsOfDialogOne.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
    <p:commandButton 
    id="openDialog2" 
    value="Open Dialog 2"
    action="#{nestedDialogsBean.actionOnOpenOfDialog2()}"
    oncomplete="dialogTwoVar.show()">
     </p:commandButton>
    <p:dialog id="dialogTwo"
        header="Dialog Two"
        widgetVar="dialogTwoVar" 
        closable="true"
        dynamic="true" 
            resizable="false" draggable="false" height="700"
        width="1000" hideEffect="fold" appendToBody="true" >
        <ui:insert>
            <ui:include src="contentsOfDialogTwo.xhtml"></ui:include>
        </ui:insert>
    </p:dialog>
</ui:composition>
</html>

contentsOfDialogTwo.xhtml

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui">
<ui:composition>
    <h:form id="innerWrapperFormForDialogTwoDueToAppendToBody">
         <p:commandButton id="actionInsideDialogTwouButton"
         action="#{nestedDialogsBean.actionOnClickOfDialogTwoItem()}"
                onclick="alert('Button clicked')"
                value="Invoke Backing Bean Action">
            </p:commandButton>
</h:form>
</ui:composition>
</html>
8bitjunkie
  • 12,793
  • 9
  • 57
  • 70

1 Answers1

2

Never use nested forms

appendToBody : Appends dialog as a child of document body.

When using it the rendered content of the dialog is being appended to the body , so if for example in your xhtml the dialog was wrapped by h:form and you using appendToBody="true" in the generated page the dialog wont be wrapped by the form tag


This is illegal in HTML and the behaviour is unspecified and dependent on the webbrowser used. The ajax link doesn't submit the form, it just collects the input values by JavaScript and then sends a XMLHttpRequest in the background. why a:commandLink's action attribute works but h:commandLink's does not?

Multiple h:form in a JSF Page

Community
  • 1
  • 1
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • Hi Daniel. Thanks for your response and the linked resource. I understand that the appendToBody attributes moves the Dialog to outside of the 'parent' form, however I have read resources on the web that intimate that wrapping the contents of any Dialog inside a new form should allow the backing beans to continue to respond, whilst not breaking the "form within a form" illegality that you have mentioned in your answer (both forms are siblings rather than parent-child). This is what I have attempted in the code above. Is this accurate? – 8bitjunkie Nov 13 '12 at 15:13
  • best practice is to place the form inside the dialog (that way you ensure that no matter what the dialog content will be inside a form) , now to the appendToBody : when you build you xhtml page , like this ...... , unless you will use `appendToBody` you will end up with nested forms... – Daniel Nov 13 '12 at 19:48