Reuse a facelet in multiple beans

Posted by Seitaridis on Stack Overflow See other posts from Stack Overflow or by Seitaridis
Published on 2012-05-17T13:48:05Z Indexed on 2012/06/05 10:40 UTC
Read the original article Hit count: 236

Filed under:
|
|
|

How do I invoke/access a property of a managed bean when the bean name is known, but is not yet constructed?

For example:

<p:selectOneMenu value="#{eval.evaluateAsBean(bean).text}" >
    <f:selectItems value="#{eval.evaluateAsBean(bean).values}" var="val"
        itemLabel="#{val}" itemValue="#{val}" />
</p:selectOneMenu> 

If there is a managed bean called testBean and in my view bean has the "testBean"value, I want the text or values property of testBean to be called.

EDIT1

The context

An object consists of a list of properties(values). One property is modified with a custom JSF editor, depending on its type.

The list of editors is determined from the object's type, and displayed in a form using custom:include tags. This custom tag is used to dynamically include the editors <custom:include src="#{editor.component}">. The component property points to the location of the JSF editor.

In my example some editors(rendered as select boxes) will use the same facelet(dynamicDropdown.xhtml). Every editor has a session scoped managed bean. I want to reuse the same facelet with multiple beans and to pass the name of the bean to dynamicDropdown.xhtml using the bean param.

genericAccount.xhtml

<p:dataTable value="#{group.editors}" var="editor">
    <p:column headerText="Key">
        <h:outputText value="#{editor.name}" />
    </p:column>
    <p:column headerText="Value">
        <h:panelGroup rendered="#{not editor.href}">
            <h:outputText value="#{editor.component}" escape="false" />
        </h:panelGroup>
        <h:panelGroup rendered="#{editor.href}">
            <custom:include src="#{editor.component}">
                <ui:param name="enabled" value="#{editor.enabled}"/>  
                <ui:param name="bean" value="#{editor.bean}"/>  
            <custom:include>
       </h:panelGroup>
    </p:column>
</p:dataTable>

#{editor.component} refers to a dynamicDropdown.xhtml file.

dynamicDropdown.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.prime.com.tr/ui">
    <p:selectOneMenu value="#{eval.evaluateAsBean(bean).text}" >
    <f:selectItems value="#{eval.evaluateAsBean(bean).values}" var="val"
        itemLabel="#{val}" itemValue="#{val}" />
    </p:selectOneMenu>  
</ui:composition>   

eval is a managed bean:

@ManagedBean(name = "eval")
@ApplicationScoped
public class ELEvaluator {
    ...

    public Object evaluateAsBean(String el) {
        FacesContext context = FacesContext.getCurrentInstance();
        Object bean = context.getELContext()
            .getELResolver().getValue(context.getELContext(), null, el);
        return bean;
    }

    ...

}

© Stack Overflow or respective owner

Related posts about java

Related posts about jsf