【问题标题】:Problem including another XHTML page in an XHTML page在 XHTML 页面中包含另一个 XHTML 页面的问题
【发布时间】:2020-12-14 23:56:32
【问题描述】:

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

在我的“page.xhtml”中,首先,我将这一行放在 :

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

但是,它不起作用。

后来,我尝试在

<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”正确接收信息。

我曾尝试将包含文件注册为标记文件,正如此处How to include another XHTML in XHTML using JSF 2.0 Facelets? 所建议的那样 但是,它不起作用。

有解决这个问题的办法吗?谢谢!

这是来自“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>

【问题讨论】:

  • 很难推断和理解“不起作用”的确切含义。看来您是在谈论在包含文件中显示特定的 bean 属性?换句话说,包含操作工作得非常好(即,当您将一些随机文本(例如“test”)添加到包含文件时,它会出现在您希望它出现的位置),但是变量并没有按照您的方式解析预计?请在描述问题时少含糊,更具体。 “它不起作用”是您能想到的最糟糕的问题描述。
  • @BalusC 感谢您的回答。好吧,我的包含文件 (include.xhtml) 包含一些 beans 属性,当我尝试在 page.xhtml 中包含“include.xhtml”时(按照我提到的方式),这个属性不会出现。

标签: jsf xhtml facelets managed-bean


【解决方案1】:

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

使用单行代码怎么样:&lt;ui:include src="WEB-INF/facelets/include.xhtml"/&gt; 并且包含文件将具有 &lt;h:outputText value="#{identityInformationBean.fullName}"/&gt;

【讨论】:

  • 是的,我在“page.xhtml”和“include.xhtml”中包含了这行代码 当我加载页面时,这个参数和其他我没有出现。
  • 也许您应该在 bean 中的 getFullName 处放置一个断点。也许它是 null 而你不知道。
  • 感谢@Tom T...但是,如果我从“include.xhtml”复制代码并将其粘贴到“page.xhtml”中,则会出现参数。但是,我需要“include.xhtml”中的这些参数,因为我想在其他页面上重用它们
  • page.xhtml 是你的主页,还是在用户做某事时显示?
  • 当用户搜索某些东西时我会显示它。
猜你喜欢
  • 2012-09-15
  • 2014-06-21
  • 2012-12-12
  • 1970-01-01
  • 2010-11-11
  • 1970-01-01
  • 2015-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多