【问题标题】:Passing the backing bean as a parameter to a Facelet include将支持 bean 作为参数传递给 Facelet 包括
【发布时间】:2013-05-30 17:55:55
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    您可以为此使用&lt;ui:param&gt;。需要嵌套在&lt;ui:include&gt;中。

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

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

    【讨论】:

    • 如何引用ParameterBean 来调用实例上的方法?
    【解决方案2】:

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

    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>
    

    【讨论】:

    • 我看不出这与下面的答案有何不同?
    • 我很难理解到底应该如何使用答案,所以我试图让它对其他人更明显。
    • 然后您可以“编辑”问题并删除这些内容,并在“评论”中提及您这样做的原因。当其他人“接受”更改时,它将是可见的。无需创建第二个答案。
    猜你喜欢
    • 2016-07-07
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多