JSF/Primefaces: ui:include中的ui:insert

3
我希望使用ui:insert/ui:define替换模板文件中的某些内容。在模板文件内部,有一个包含另一个文件的语句,并且在该文件内部包含了ui:insert。
在这种情况下,ui:define无法正常工作。但是,如果将footer.xhtml文件的代码包含在template.xhtml中,则ui:define可以正常工作。
在ui:include语句中使用ui:insert/ui:define是否不可行?
template.xhtml:
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:sec="http://www.springframework.org/security/facelets/tags"
      xmlns:debug="http://XYZ/jsf/debug"
      xmlns:util="http://XYZ/ibk/util"
      xmlns:p="http://primefaces.org/ui">

    <h:body >

        <f:view>
            <div id="headerWrapper">
                <!-- META-NAVIGATION -->
                <div id="metanavWrapper"  >
                    <div class="ui-helper-clearfix pagewidth" >
                        <ui:include src="metanav.xhtml" />
                    </div>
                </div>

            </div>

            <div id="contentWrapper" >
                <!--div id="content" class="pagewidth"-->
                <div id="content">
                    <div id="contentMenuLeft">
                        <ui:include src="navigationMenu.xhtml" />
                    </div>
                    <div id="contentDisplay">
                        <ui:insert name="content" />
                        <ui:insert name="help" />

                    </div>
                </div>
            </div>

            <util:footer />

        </f:view>

        <ui:insert name="dialog"/>

    </h:body>
</html>

--

<util:footer /> 

此处也可以写成ui:include,结果相同...

footer.xhtml:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:sec="http://www.springframework.org/security/facelets/tags"
                xmlns:p="http://primefaces.org/ui"
                >

   <div id="footerWrapper">
      <f:subview id="footerWrapper">


         <h:panelGroup id="footer" >
            <div >

               <ui:insert name="replace" />

            </div>
         </h:panelGroup>
      </f:subview>


   </div>



</ui:composition>

another.xhtml: (snippet)

<ui:composition template="template.xhtml">
  <ui:define name="replace">         
    <h:panelGroup>
       <div>
          <p:outputLabel value="test"/>
       </div>
    </h:panelGroup>
  </ui:define>
</ui:composition>
2个回答

2

0

如果我理解正确,您的<ui:define name="replace">将永远无法正常工作,因为相应的<ui:insert name="replace" />在其中,并且仅在包括页脚时才存在。没有插入,也就没有定义。

当您调用另一个.xhtml(或任何其他具有template="..."的站点)时,编译器将首先组装所有的include,然后将所有define代码添加到相应的insert标签的位置。


如果这是正确的,上述代码应该可以工作,因为 footer.xhtml 被包含在 template.xhtml 中。所以要么过程是倒序的(先插入/定义再包含),要么代码存在错误... - OPaczkowski
你尝试过真正的<ui:include src="footer.xhtml" />包含吗? - Dawn
是的,我试过了。也不起作用。这对你有用吗?那么我可能在我的代码中有一个错误。 - OPaczkowski
它在我的项目中运作良好。 我有一个特定于项目的template.xhtml,其中包含一个底层的base-template。在template.xhtml内,定义了参数application。用于“application”的ui:insert嵌套在一个额外的xhtml中,该xhtml包含在base-template中。也许可以尝试: <ui:insert name="footer"> <ui:include src="footer.html" /> </ui:insert> - Dawn
将插入语句放在include中并不能解决问题。你/有人可以发布一下你们的工作代码吗? - OPaczkowski

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接