【问题标题】:Losing inputs submitted in composite component丢失在复合组件中提交的输入
【发布时间】:2012-10-21 10:18:41
【问题描述】:

我在提交复合组件时遇到问题。

我的大多数复合组件都包含输入组件和“提交”按钮。 当我试图将按钮仍然放在同一个 h:form 但不在同一个复合组件中时,提交的值似乎在某个地方“丢失”了。而且,例如,我的验证器被调用原始值。

例子:

<composite:interface>
  <composite:attribute name="titreContext" required="true"/>
</composite:interface>
<composite:implementation>

    <p:outputPanel id="selectionTitreDetailsPanel" styleClass="selectionTitreDetails">
    <p:outputPanel id="selectionTitreDetailsPanelInner" rendered="#{not empty cc.attrs.titreContext.selected}">

    <p:panelGrid columns="2" id="panelId">
      <h:outputText id="idLabel" value="Id :"/>
      <h:outputText id="id" value="#{cc.attrs.titreContext.selected.titeluid}"/>
      <p:tooltip for="id" value="Identifiant unique"/>
    </p:panelGrid>

    <p:panelGrid columns="2" id="titelePanel">
        <p:outputLabel for="selectTitele" value="Titre :"/>
        <p:selectOneMenu id="selectTitele" value="#{cc.attrs.titreContext.selected.titele}" effect="fold" styleClass="fullWidth">
            <f:selectItems value="#{constants.getTitelesForTypman(cc.attrs.titreContext.selected.titele.typman)}" var="titele" itemLabel="#{titele.titelelil}" itemValue="#{titele}" styleClass="fullWidth"/>
                <p:column styleClass="fullWidth">#{titele.titelelil}</p:column>
        </p:selectOneMenu>      
    </p:panelGrid>

    [...]
    <p:commandButton id="confirmerModifications" icon="small_edit" type="submit" value="Confirmer les modifications" 
                    action="#{elutersEditionContext.confirmeModifsSelection}" process="mandatsTerritorial" 
                    update="mandatsTerritorial #{cc.attrs.notifUpdates}"/>

</composite:implementation>

有效。

但是将 p:commandButton 从组合中取出:

<h:form>
<mylib:mycomponent /*parameters *//>
<p:commandButton /*parameters*/ />
</h:form>

不起作用。当我调试我的验证器时,我可以看到修改后的值甚至没有提交。 getLocalValue、getSubmittedValue 和 getValue 均未更改。

复合组件声明中是否有用于纠正这种情况的语法? 顺便说一句:当我将组件编写为复合组件而不是自定义组件时,在支持 bean 中检索 #{asen} 就可以了。

提前致谢。

我正在使用:

  • PrimeFaces 3.4.1
  • CODI 1.0.5
  • OpenWebBeans 1.1.6
  • MyFaces 2.1.9
  • Tomcat 7.0.32

(更新)这个很奇怪的问题是由 h:form 嵌套引起的。

很奇怪,因为 h:form 嵌套并没有扰乱第一级复合组件的处理,而是在嵌套复合中造成了这种奇怪的“输入丢失”。

嵌套看起来像这样:

<h:form>
...
    <p:tabView ...>
        <p:tab>
        <h:form>
            <my:composite ....>
        </h:form>
    </p:tabView>
</h:form>

【问题讨论】:

    标签: jsf primefaces submit composite-component


    【解决方案1】:

    您在&lt;p:commandButton&gt;process 属性中使用了相对客户端ID:

    <p:commandButton ... process="mandatsTerritorial" />
    

    相对客户端 ID 相对于父 NamingContainer 组件。它将作为NamingContainer 组件的direct 子组件进行搜索。如果子元素本身是 NamingContainer,则不会搜索其子元素。

    复合组件本身实际上也是NamingContainer 组件。如果按钮放置在组合中,那么它将作为&lt;cc:implementation&gt;direct 子项进行搜索。在您的特定情况下,只有带有 id="mandatsTerritorial" 的组件将在表单提交时被处理,包括它的所有子组件(请注意,到目前为止发布的代码中看不到这个组件,但我想您为了简洁而省略了它)。

    如果按钮位于&lt;h:form&gt; 中,那么它将作为&lt;h:form&gt;direct 子项进行搜索。然而,由于这显然被放置在复合材料中(如前所述,它是另一个 NamingContainer 组件),因此找不到它,因此基本上不会处理任何内容。您需要修复 process 以指向正确的客户端 ID。例如

    <h:form>
        <mylib:mycomponent id="mycomponent" />
        <p:commandButton ... process="@this mycomponent:mandatsTerritorial" />
    </h:form>
    

    这样,它将处理自己(必须调用该操作!)以及带有id="mandatsTerritorial" 的组件在带有id="mycomponent" 的组合的&lt;cc:implementation&gt; 内。

    作为一个完全不同的替代方案,它在这个特定的结构中工作得很好,就是完全删除process 属性。它已经默认为@form,它将处理整个表单。


    更新 根据您的问题更新:嵌套表单在 HTML 中无效。使用 JSF &lt;h:form&gt; 表示不会改变这一点;您仍然会在 HTML 中使用嵌套表单。浏览器行为未指定将哪些数据提交给服务器。确保您也不要在 JSF 中嵌套 &lt;h:form&gt;

    【讨论】:

    • 我知道这不是 Stack Overflow 中的工作方式,但我不知道如何在这里与您联系。我今天早些时候在这里问了一个问题,但不幸的是它只有 12 次观看。也许那是一天中糟糕的时刻。但我知道你会知道我的问题的答案,我真的在寻找专业人士的一些见解。如果您能花几秒钟时间阅读我的帖子,我将不胜感激。谢谢先生stackoverflow.com/questions/13161747
    • @gmu:我最近不再明确地查看 JDBC 和 Tomcat 标记(尽管有时只在周末),因为最近已经有“足够”的 JSF/JSP/Servlet 问题需要回答。
    • @BalusC :感谢您的回复。我的问题不够清楚。将 commandButton 从组合中取出时,我不使用 process="" 。由于它不起作用,我尝试为 process 显式设置默认值 @form。这不起作用。最后,你确认我应该...... :-)
    • 嗯好的,我可能需要补充一下,我正在使用 Mojarra。但如果它确实是 MyFaces 中的一个错误,那就太尴尬了。
    • 我也强烈怀疑这是 myfaces 中的一个错误,因为它会是一个非常大的错误。我将不得不进一步调查。我希望这是pbkac。 :-)
    猜你喜欢
    • 2015-08-17
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 2012-06-29
    • 2019-06-15
    相关资源
    最近更新 更多