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 <[email protected]>
> 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 ?
>