11

我有一个可能用于不同应用程序的 Facelet。我不复制它,而是重复使用它。我需要将管理视图的支持 bean 作为参数传递,因为某些逻辑可能会根据使用它的应用程序而有所不同。

我不想使用复合组件,而只是包含 Facelet 并指定哪个 bean 将管理视图。我怎样才能做到这一点?

让我举个例子:

<ui:composition template="/resources/common/templates/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <ui:define name="content">
        <!-- somehow establish the backing bean that will manage formView.xhtml --> 
        <!-- f:set  assign="ParameterBean" value="#{Bean}" / -->
        <ui:include src="formView.xhtml" />
    </ui:define>
</ui:composition>

formView.xhtml:

<ui:composition template="/resources/common/templates/template.xhtml"
    xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core" xmlns:a4j="http://richfaces.org/a4j"
    xmlns:rich="http://richfaces.org/rich" xmlns:fn="http://java.sun.com/jsp/jstl/functions">
    <ui:define name="content">
        <h:outputText value="#{ParameterBean.texto}" />
    </ui:define>
</ui:composition>
4

2 回答 2

24

你可以使用<ui:param>它。它需要嵌套在<ui:include>.

<ui:include src="formView.xhtml">
    <ui:param name="ParameterBean" value="#{Bean}" />
</ui:include>

与具体问题无关,标准Java 命名约定规定实例变量名称必须以小写字母开头。您应该以分别使用parameterBean#{bean}将使用的方式更改您的代码。

于 2013-05-30T18:20:22.113 回答
0

因为昨天我会发现它很有帮助,所以当我在寻找这个时,这里是一个简单的版本,没有多余的模板、定义和命名空间:

File1.xhtml(根标签无关紧要)

<ui:include src="File2.xhtml">
  <ui:param name="person" value="#{whatever_value_you_want_to_pass}" />
</ui:include>

文件2.xhtml

<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <h:outputLabel value="#{person.name}" />
</ui:composition>


您也可以以相同的方式进一步嵌套。

文件1.xhtml

<ui:include src="File2.xhtml">
  <ui:param name="person" value="#{whatever_value_you_want_to_pass}" />
</ui:include>

文件2.xhtml

<ui:composition ... xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <ui:include src="File3.xhtml">
    <ui:param name="name" value="#{person.name}" />
  </ui:include>
</ui:composition>

文件3.xhtml

<ui:composition ... xmlns:h="http://java.sun.com/jsf/html"
  xmlns:ui="http://java.sun.com/jsf/facelets" ... >
  <h:outputLabel value="#{name.length}" />
</ui:composition>
于 2015-05-14T13:09:46.767 回答