将支持bean作为参数传递给Facelet包括

时间:2013-05-30 17:55:55

标签: jsf-2 parameters include facelets backing-beans

我有一个可能在不同应用程序中使用的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>

2 个答案:

答案 0 :(得分:22)

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

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

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

答案 1 :(得分:0)

因为昨天我会发现它很有帮助,当我在寻找这个时,这里有一个简单的版本如何做到这一点,没有无关的模板,定义和命名空间:

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

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

File2.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>

<小时/> 您也可以以相同的方式进一步嵌套。

File1.xhtml

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

File2.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>

File3.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>
相关问题