How to find JSF bean programmatically?

12.08.2015 11:33:48

I needed to get request-scoped bean from view-scoped bean. It's not possible using @ManagedProperty annotation, so how to do that?

first create this method:

	@SuppressWarnings("unchecked")
	public static  T getBean(final String beanName, final Class clazz) {
	    ELContext elContext = FacesContext.getCurrentInstance().getELContext();
	    return (T) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, beanName);
	}

next just call:

getBean("beanName", BeanName.class);

Note: You need this dependency to make it work: http://javalibs.com/artifact/javax.el/javax.el-api




© Jiri Pinkas | this project on GitHub | related: JavaVids | Maven Central Repository Search | Java školení | Top SQL Blogs | monitored using: sitemonitoring

Top Java Blogs is a Java blog aggregator (with English-written blogs only) focused on Java SE, Java EE, Spring Framework and Hibernate.