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

2024-01-21

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

您可以使用<ui:param> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/param.html为了那个原因。它需要嵌套在<ui:include> http://docs.oracle.com/javaee/6/javaserverfaces/2.1/docs/vdldocs/facelets/ui/include.html.

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

无关针对具体问题、标准Java 命名约定 http://www.oracle.com/technetwork/java/javase/documentation/codeconventions-135099.html#367声明实例变量名称必须以小写字母开头。您应该分别更改您的代码parameterBean and #{bean}将会被使用。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将支持 bean 作为参数传递给 Facelet include 的相关文章

随机推荐