首页 > 解决方案 > 在 XHTML 页面中包含另一个 XHTML 页面的问题

问题描述

我是一名 Java 编程初学者,我正在使用 primefaces 做一个项目。我想在 XHTML 页面中包含另一个 XHTML 页面。包含页面位于 /WEB-INF/facelets/include.xhtml (它有一些来自托管 Bean 的数据)

在我的“page.xhtml”中,首先,我将这一行放在 <ui:define name="content"> 中:

<ui:include src="WEB-INF/facelets/include.xhtml" /> 

但是,它不起作用。

后来,我尝试在 <ui:define name="content">

<ui:include src="WEB-INF/facelets/include.xhtml">
    <ui:param name="fullName" value="#{identityInformationBean.fullName}" />
</ui:include>

在“include.xhtml”中:

<h:outputText
    rendered="#{fullName!=null}"
    value="#{fullName}" />

但是,它也不起作用。不过,如果我这样做:

在“page.xhtml”上

<ui:include src="WEB-INF/facelets/include.xhtml">
    <ui:param name="fullName" value="Helen" />
</ui:include>

“include.xhtml”正确接收信息。

我曾尝试将包含文件注册为标记文件,如此处建议的如何使用 JSF 2.0 Facelets 在 XHTML 中包含另一个 XHTML? 但是,它不起作用。

有什么想法可以解决这个问题吗?谢谢!

这是来自“include.xhtml”的一段代码:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">

    <h:outputText
        rendered="#{identityInformationBean.fullName!=null}"
        value="#{identityInformationBean.fullName}" />
        
</ui:composition>

这是来自“page.xhtml”的一段代码:

<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:c="http://java.sun.com/jstl/core"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:p="http://primefaces.org/ui" template="templates/generaltemplate.xhtml">

    <ui:define name="content">
    
        <h2>
            <h:outputText value="Identity Information"/>
        </h2>
        
    </ui:define>

</ui:composition>

标签: jsfxhtmlfaceletsmanaged-bean

解决方案


我不确定你为什么需要ui:param. 将包含文件视为只是省去了将包含的代码输入到所有可能使用它的页面中的麻烦。您不需要向它传递参数。

使用单行代码怎么样:<ui:include src="WEB-INF/facelets/include.xhtml"/> 包含文件会有<h:outputText value="#{identityInformationBean.fullName}"/>


推荐阅读