Java Server Faces: Accessing Managed Bean Programmatically

We have seen how to access properties of managed bean from both pages and faces configuration files. It is also possible to do the same using Java code from an event listener class or another managed bean. There are many ways to do this. For all cases, we must first have a reference to the current

javax.faces.context.FacesContext as follows:

FacesContext fc = FacesContext.getCurrentInstance();

1. Using javax.faces.context.ExternalContext

If the scope of the bean is known, we can query the containing application environment to have a Map object containing registered beans as follows:

ExternalContext ec = fc.getExternalContext();

Map m = ec.getApplicationMap();

In case of session and request scoped bean, use getSessionMap() and getRequestMap() methods respectively. A reference to the bean having the name “myBean” can then be obtained as follows:

MyBean mb = (MyBean) m.get(”myBean”);

Programmers often love to use the following concise code:

MyBean mb=(MyBean)

FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().get(“myBean”);

We can now invoke its methods as usual. Note that this method works fine if the scope of the bean is not changed subsequently this code is written. Alternatively, the following methods may be used.

2. Using javax.el.ELContext

Any EL expression can be evaluated from a Java code with the help of ELContext which is obtained as follows:

ELContext ec = fc.getELContext();

Note that this class in not included in faces JAR file. Download a suitable JAR file containing this class. We downloaded javax.el-api-2.2.4.jar from http://www.java2s.com/Code/Jar/j/ Downloadjavaxelapi224jar.htm. The bean instance can be then obtained as follows:

MyBean mb = (MyBean) ec.getELResolver().getValue(ec, null,”myBean”);

If you want to work with arbitrary EL value expression, we may create a VaiueExpression object as follows:

ExpressionFactory ef = fc.getApplication().getExpressionFactory();

VaiueExpression ve = ef.createValueExpression(ec, ”#{myBean}”, MyBean.class);

Note that the expression #{myBean} refers to the entire bean instance which can be obtained as shown here:

MyBean mb = (MyBean) ve.getValue(ec);

The individual properties such as message of the bean can also be obtained as follows:

VaiueExpression ve = ef.createValueExpression(ec, ”#{myBean.message}”, String.class);

String msg = (String) ve.getValue(ec);

This advantage of this method is that it also allows us to set the value of EL expression as follows:

ve.setValue(ec, ”abc”);

Like ValueExpression, a MethodExpression can be used to invoke a method of a managed bean. For example, the following shows how to invoke the getMessage() method of myBean:

MethodExpression me =

ef.createMethodExpression(ec,”#{myBean.getMessage}”,String.class, new Class[]{});

String msg = (String)me.invoke(ec, null);

3. Using evaluateExpressionGet() Method

In JSF 2.0, there is a concise way to evaluate an EL expression using evaluateExpressionGet() method as follows:

Application a = fc.getApplication();

MyBean mb = (MyBean)a.evaluateExpressionGet(fc, ”#{myBean}”, MyBean.class);

However, this method cannot set the value of the EL expression.

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *