Jsf 2 如何在两个ui之间使用单个表单:定义

Jsf 2 如何在两个ui之间使用单个表单:定义,jsf-2,primefaces,facelets,composite-component,Jsf 2,Primefaces,Facelets,Composite Component,我有一个简单的模板,其中定义了两个区域,工具栏和内容。工具栏区域有许多命令按钮,内容有字段。 我的问题是:如何在两个ui:define之间使用单个表单 我提供了示例代码: <ui:composition template="/WEB-INF/templates/layout.xhtml"> <ui:define name="toolbar"> <p:commandButton id="saveButton" value="Save" aja

我有一个简单的模板,其中定义了两个区域,工具栏和内容。工具栏区域有许多命令按钮,内容有字段。 我的问题是:如何在两个ui:define之间使用单个表单

我提供了示例代码:

<ui:composition template="/WEB-INF/templates/layout.xhtml">

    <ui:define name="toolbar">
        <p:commandButton id="saveButton" value="Save" ajax="false" action="#{...}" />
    </ui:define>

    <ui:define name="content">
        <h:form id="registerForm">
            <p:outputLabel for="name" value="Name"/>
            <p:inputText id="name" value="#{...}" required="true" />
            <p:message for="name"/>
         </h:form>
    </ui:define>

</ui:composition>

当触发“取消保存”按钮时,应提交h:form registerForm


有人有什么建议吗?

我用ui:decorator解决了这个问题。像“子模板”一样使用ui:decorator可以将表单放在多个ui:define之间。 我希望这能有所帮助

模板装饰.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:ui="http://java.sun.com/jsf/facelets">

    <ui:define name="toolbar"></ui:define>

    <ui:define name="content"></ui:define>
</html>
<!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:ui="http://java.sun.com/jsf/facelets"

    <ui:composition template="/WEB-INF/templates/layout.xhtml">
        <ui:define name="content">
            <h:form id="registerForm">
                <ui:decorate template="/WEB-INF/templates/layout-decorator.xhtml">

                    <ui:define name="toolbar">
                        <p:commandButton id="saveButton" value="Save" ajax="false" action="#{...}" />
                    </ui:define>

                    <ui:define name="form">
                        <p:outputLabel for="name" value="Name"/>
                        <p:inputText id="name" value="#{...}" required="true" />
                        <p:message for="name"/>
                    </ui:define>
                </ui:decorate>
            </h:form>
        </ui:define>

    </ui:composition>
</html>

register.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:ui="http://java.sun.com/jsf/facelets">

    <ui:define name="toolbar"></ui:define>

    <ui:define name="content"></ui:define>
</html>
<!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:ui="http://java.sun.com/jsf/facelets"

    <ui:composition template="/WEB-INF/templates/layout.xhtml">
        <ui:define name="content">
            <h:form id="registerForm">
                <ui:decorate template="/WEB-INF/templates/layout-decorator.xhtml">

                    <ui:define name="toolbar">
                        <p:commandButton id="saveButton" value="Save" ajax="false" action="#{...}" />
                    </ui:define>

                    <ui:define name="form">
                        <p:outputLabel for="name" value="Name"/>
                        <p:inputText id="name" value="#{...}" required="true" />
                        <p:message for="name"/>
                    </ui:define>
                </ui:decorate>
            </h:form>
        </ui:define>

    </ui:composition>
</html>


使用一个表单的原因是,当单击commandButton时,的值未提交,并且我的操作引发NullPointerException。PS:我不想使用ajax提交值。有理由只使用一个表单吗?没有,如果你有建议用两个表单来解决这个问题,请告诉我。唯一的要求是:模板中有两个区域,不要使用ajax提交内容表单。你能帮我吗?没有Ajax 2表单是不行的。如果你想在一个表单中使用它,你需要在模板中放置你的标签。这种方法的缺点是,您不能再在页面中使用表单标记,因为这样会得到嵌套表单,而这是无效的。