Francesco
Francesco

Reputation: 2382

Accessing a controller through a PhaseListener

I wrote a PhaseListener and would like to know if it is possible to access a controller through it.

Upvotes: 1

Views: 149

Answers (1)

BalusC
BalusC

Reputation: 1108782

You can evaluate an EL expression programmatically by Application#evaluateExpressionGet(). Wrap this in a convenience method like follows:

@SuppressWarnings("unchecked")
public static <T> T findBean(String beanName) {
    FacesContext context = FacesContext.getCurrentInstance();
    return (T) context.getApplication().evaluateExpressionGet(context, "#{" + beanName + "}", Object.class);
}

Use it in your JSF code as follows:

MyBackingBean myManagedBean = JSF.findBean("myManagedBeanName");
// ...

Upvotes: 1

Related Questions