【问题标题】:How to use a single form between two ui:define如何在两个 ui:define 之间使用单个表单
【发布时间】:2013-06-02 10:58:55
【问题描述】:

我有一个简单的模板,其中定义了两个区域,工具栏和内容。工具栏区域有许多命令按钮,内容有字段。 我的问题是:如何在两个 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>

de saveButton触发时,需要提交h:form registerForm。

有人有建议吗?

【问题讨论】:

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

标签: jsf-2 primefaces facelets composite-component


【解决方案1】:

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

模板装饰.xhml

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

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

欲了解更多信息,请参阅:http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ui/decorate.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    相关资源
    最近更新 更多