【问题标题】:Does <ui:composition> go before or after <body>?<ui:composition> 是在 <body> 之前还是之后?
【发布时间】:2012-05-04 10:56:00
【问题描述】:

我正在使用 JSF2 facelets。

我正在尝试使用&lt;ui:composition&gt;&lt;ui:insert&gt; 标签将一段代码从一个页面插入另一个页面。

我有页面 A,其中包含页面 B 中的代码。

&lt;h:form id="formIdPageA"&gt; ...

&lt;h:form id="formIdPageB"&gt;

问题似乎是表单 ID,因为我收到错误:

System error: Cannot find component with identifier ":formIdPageA:fileListId" in view.

这是从页面 B 插入到页面 A 的一段代码。这里可以看到 id:

                    <tr>
                        <td colspan="2">
                            <p:selectOneMenu id="locationId" value = "#{PFMultiFileSelectMgmtBean.selectedLocationId}">
                                <p:ajax update=":formIdPageA:fileListId" listener="#{PFMultiFileSelectMgmtBean.LocationChangeEvent}"/>
                                <f:selectItems value="#{PFJobMgmtBean.outputLocationList}"/>                                
                            </p:selectOneMenu>
                        </td>
                        <td>                                
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <p:selectManyCheckbox id="fileListId" value="PFMultiFileSelectMgmtBean.selectedFiles"  layout="pageDirection">  
                                <f:selectItems value="#{PFMultiFileSelectMgmtBean.fileNames}" />  
                            </p:selectManyCheckbox>  
                        </td>
                    </tr>   

这就是我在页面 A 中插入它的方式:

<p:dialog id="basicDialog" header="Select Files" widgetVar="dlgMultiFileSelect" modal="true" height="500" width="500" resizable="false">
    <ui:insert>  
        <ui:include src="/pageB.xhtml"/>
    </ui:insert>  
</p:dialog>

有谁知道是否可以包含这样的页面? &lt;ui:composition&gt; 是在正文标签之前还是在页面 B 之后?可以吗:

<body>
    <f:view>    
        <h:form id="formIdPageB">
                <ui:composition>...

?

好的,找到了:

问题是我使用了PrimeFaces组件&lt;p:dialog&gt;,并把它放在&lt;h:form&gt;之外和&lt;body&gt;之外的页面A中,像这样:

   </h:form>
 </f:view>      
</body>
<p:dialog...>        
   <ui:include src="/pageB.xhtml"/>       
</p:dialog>

这似乎是错误的。现在我将&lt;p:dialog&gt; 移到表单中,一切正常。

【问题讨论】:

    标签: jsf primefaces facelets


    【解决方案1】:

    插入和组合是 JSF 中的模板工具。当您使用 ui:insert 时,您会创建一个插入点,可以使用您的模板在页面中插入(使用 ui:define)。以下是如何正确使用模板和合成的教程:

    http://www.ibm.com/developerworks/java/library/j-jsf2fu2/index.html

    如果我理解得很好,您需要在页面 A 和页面 B 中重用代码块。所以我会将代码提取到单独的文件中,并将其包含在页面 A 和页面 B 中。

    如果你想把你的 放在 里面,它是允许的。您可以查看文档示例:

    http://docs.oracle.com/javaee/6/javaserverfaces/2.0/docs/pdldocs/facelets/ui/composition.html

    【讨论】:

    • 不管怎样我尝试它,我得到错误:System error: Cannot find component with identifier ":formIdPageA:fileListId" in view. 嗯...任何想法?如何引用表单组件?
    • 你能试试 formIdPageA:fileListId 去掉开头的 : 吗?
    • 刚试过,没用。我在其他一些地方使用:并且工作,所以我也在这里使用它。
    • 你可以跳过更新属性来渲染页面吗?进入页面后,在生成的 html 中检查该元素的确切 ID。可能是中间有一个组件。
    • 删除它。现在我得到javax.servlet.ServletException: Value of 'fileListId'must be an array or a collection
    【解决方案2】:

    &lt;ui:composition&gt; 围绕您想要包含的部分。那是什么,以及它是否包含或排除 &lt;body&gt;,完全取决于您。

    【讨论】:

    • 谢谢。问题是无论我尝试什么,都会出现错误:System error: Cannot find component with identifier ":formIdPageA:fileListId" in view. 如何在 B 页中引用表单组件?通过页面 A 中的表单?
    • 您不需要 ui:insert。 ui:include 是插入。
    【解决方案3】:

    我不确定您是否正确使用了 ui:insert。插入是一个占位符。你需要给它一个名字。然后,您使用 ui:define 将代码放入该占位符中。我认为对于您要执行的操作,您只需要删除 ui:insert 标记并将 ui:include 保留在页面 A 中。

    【讨论】:

    • 这没有帮助。无论我尝试什么,都会出错:System error: Cannot find component with identifier ":formIdPageA:fileListId" in view. 如何在 B 页中引用表单组件?通过页面 A 中的表单 ID?为什么它不起作用?
    • 我相信既然都是formIdPageB,你可以参考(例如):fileListId。如果您的页面正在呈现,您可以使用 Firebug 来帮助您确定正确的组件 ID 和引用。
    【解决方案4】:

    问题是我使用了 PrimeFaces 组件并将其放在页面 A 的外部和外部,如下所示:

        </h:form>
      </f:view>      
    </body>
    <p:dialog...>        
       <ui:include src="/pageB.xhtml"/>       
    </p:dialog>
    

    这似乎是错误的。现在我移动到表单内部,一切正常。

    【讨论】:

      猜你喜欢
      • 2011-12-06
      • 1970-01-01
      • 2011-07-27
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 2012-11-18
      相关资源
      最近更新 更多