Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing the backing bean as a parameter to a Facelet include

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.

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?

Let me give an example:

<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>
like image 522
Deibys Avatar asked May 30 '13 17:05

Deibys


People also ask

What are backing beans in Java?

A backing bean is created with a constructor with no arguments (like all JavaBeans components) and a set of properties and a set of methods that perform functions for a component. Each of the backing bean properties can be bound to one of the following: A component value. A component instance.

What is the difference between managed bean and backing bean in JSF?

1) BB: A backing bean is any bean that is referenced by a form. MB: A managed bean is a backing bean that has been registered with JSF (in faces-config. xml) and it automatically created (and optionally initialized) by JSF when it is needed.


1 Answers

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>

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.

like image 108
BalusC Avatar answered Sep 28 '22 19:09

BalusC