Note that there are some explanatory texts on larger screens.

plurals
  1. POReuse a facelet in multiple beans
    text
    copied!<p>How do I invoke/access a property of a managed bean when the bean name is known, but is not yet constructed?</p> <p>For example: </p> <pre><code>&lt;p:selectOneMenu value="#{eval.evaluateAsBean(bean).text}" &gt; &lt;f:selectItems value="#{eval.evaluateAsBean(bean).values}" var="val" itemLabel="#{val}" itemValue="#{val}" /&gt; &lt;/p:selectOneMenu&gt; </code></pre> <p>If there is a managed bean called testBean and in my view <em>bean</em> has the <code>"testBean"</code>value, I want the <em>text</em> or <em>values</em> property of testBean to be called.</p> <p><strong>EDIT1</strong></p> <p><strong>The context</strong></p> <p>An object consists of a list of properties(values). One property is modified with a custom JSF editor, depending on its type.</p> <p>The list of editors is determined from the object's type, and displayed in a form using <code>custom:include</code> tags. This custom tag is used to dynamically include the editors <code>&lt;custom:include src="#{editor.component}"&gt;</code>. The <em>component</em> property points to the location of the JSF editor. </p> <p>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 <em>bean</em> param.</p> <p><code>genericAccount.xhtml</code></p> <pre><code>&lt;p:dataTable value="#{group.editors}" var="editor"&gt; &lt;p:column headerText="Key"&gt; &lt;h:outputText value="#{editor.name}" /&gt; &lt;/p:column&gt; &lt;p:column headerText="Value"&gt; &lt;h:panelGroup rendered="#{not editor.href}"&gt; &lt;h:outputText value="#{editor.component}" escape="false" /&gt; &lt;/h:panelGroup&gt; &lt;h:panelGroup rendered="#{editor.href}"&gt; &lt;custom:include src="#{editor.component}"&gt; &lt;ui:param name="bean" value="#{editor.bean}"/&gt; &lt;custom:include&gt; &lt;/h:panelGroup&gt; &lt;/p:column&gt; &lt;/p:dataTable&gt; </code></pre> <p><code>#{editor.component}</code> refers to a dynamicDropdown.xhtml file.</p> <p><code>dynamicDropdown.xhtml</code></p> <pre><code>&lt;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"&gt; &lt;p:selectOneMenu value="#{eval.evaluateAsBean(bean).text}" &gt; &lt;f:selectItems value="#{eval.evaluateAsBean(bean).values}" var="val" itemLabel="#{val}" itemValue="#{val}" /&gt; &lt;/p:selectOneMenu&gt; &lt;/ui:composition&gt; </code></pre> <p><code>eval</code> is a managed bean:</p> <pre><code>@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; } ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload