将支持 bean 作为参数传递给 Facelet 包括 [英] Passing the backing bean as a parameter to a Facelet include
问题描述
我有一个可能用于不同应用程序的 Facelet.我不会复制它,而是重复使用它.我需要将管理视图的支持 bean 作为参数传递,因为某些逻辑可能会根据使用它的应用程序而有所不同.
I have a Facelet that might be used in different applications. I don't to copy it, but reuse it. I need to pass the backing bean that will manage the view as a parameter, as some logic may vary according to the application where it is used in.
我不想使用复合组件,而只是包含 Facelet 并指定哪个 bean 将管理视图.我怎样才能做到这一点?
I don't want to use a composite component, but just include the Facelet and specify which bean will manage the view. How can I achieve this?
举个例子:
<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 :
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>
推荐答案
You can use <ui:param>
for that. It needs to be nested in the <ui:include>
.
<ui:include src="formView.xhtml">
<ui:param name="ParameterBean" value="#{Bean}" />
</ui:include>
<小时>
无关到具体问题,标准Java命名约定规定实例变量名称必须以小写开头.您应该以分别使用 parameterBean
和 #{bean}
的方式更改代码.
Unrelated to the concrete problem, standard Java Naming Conventions state that instance variable names must start with lower case. You should change your code in such way that respectively parameterBean
and #{bean}
will be used.
这篇关于将支持 bean 作为参数传递给 Facelet 包括的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!