I have tried the method you mention and all other methods mentioned in
http://balusc.blogspot.com/2006/06/communication-in-jsf.html#AccessingAnotherManagedBean
but none of them can see the bean set via ui:param.

Im now resorting to an ugle hack which is to set the managed bean in a
request scoped variable in the constructor. All the hundreds of
managed beans subclass the same base class so I set the variable in
the base class constructor:

                FacesContext facesContext = FacesContext.getCurrentInstance();
                HttpServletRequest req = (HttpServletRequest)
facesContext.getExternalContext().getRequest();
                req.setAttribute("managedbean", this);

Now Im able to get hold of the managed bean in the phaselistener using:

                Base managedbean = (Base)((HttpServletRequest) facesContext
                .getExternalContext().getRequest()).getAttribute("managedbean");

This of course assumes the phaselistener is listening on a phase that
comes after bean construction (which it does in my case). Im also
assuming the phaselistener is running in the same thread that run the
constructor of the managed bean otherwise letting the "this" reference
escape from the constructor without any synchronization could possibly
cause a multithreading problem.





On Tue, Oct 25, 2011 at 11:34 AM, Anton Gavazuk <antongava...@gmail.com> wrote:
>  public static Object accessBeanFromFacesContext(final String beanName,
> final FacesContext facesContext) {
>      debug("accessBeanFromFacesContext start " + beanName);
>      Object ref =
> facesContext.getELContext().getELResolver().getValue(facesContext.getELContext(),
> null, beanName);
>      if (ref == null)
>         throw new PropertyNotFoundException("Cannot find bean " +
> beanName);
>      return ref;
>   }
>
> 2011/10/25 Me Self <wmso...@gmail.com>
>
>> How do I access a managed bean from a phaselistener without it knowing
>> the faces-config.xml name of the bean? I have 1 phaselistener that
>> acts across hundreds of pages that each have one managed bean. All the
>> managed beans subclass the same base class.
>>
>> All pages are a composition of the same template and as a parameter
>> for that template all pages set a <ui:param> with the name
>> "managedbean" that references the actual bean for that page:
>>
>> <ui:composition template="/WEB-INF/template.xhtml">
>>        <ui:param name="managedbean" value="#{someBean}" />
>>        <ui:define name="content">...
>>
>> From the phaselistener I tried accessing the managed bean through the
>> param name "managedbean" but that is not accessible (at least not in
>> the UPDATE_MODEL_VALUES phase):
>>
>>        @Override
>>        public void afterPhase(PhaseEvent event) {
>>                ...
>>                Base neededBean = (Base) facesContext
>>
>>  .getApplication().evaluateExpressionGet(facesContext,"#{managedbean}",Base.class);
>>
>>
>> The line above returns null unless I change the EL to refer directly
>> to a bean name from faces-config.xml. I can't find the "managedbean"
>> through the external context parameters either.
>>
>> I also tried setting "managedbean" in a scope using the Set tag:
>>
>>                <c:set var="scopedmanagedbean" value="#{managedbean}"
>> scope="request"/>
>>
>> But the attribute is not in the request scope when tested from the
>> phaselistener using:
>>
>>                 HttpServletRequest req = (HttpServletRequest) facesContext
>>
>>  .getCurrentInstance().getExternalContext().getRequest().getAttributeNames();
>>
>>
>> Anyone know to get hold of the bean through the ui:param ?
>>
>

Reply via email to