问题描述
我是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>
解决方法
我不确定您为什么需要ui:param
。将包含文件视为节省您在将包含的代码键入可能使用它的所有页面中的麻烦。您无需向其传递参数。
使用一行代码怎么办:<ui:include src="WEB-INF/facelets/include.xhtml"/>
并且包含文件将包含<h:outputText value="#{identityInformationBean.fullName}"/>