waffel’s Weblog

September 27, 2007

Replacing deprecated ValueBindung stuff from JSF with ELResolver

Filed under: JSF,software — Thomas Wabner @ 11:21 am

I have searched for a solution to replace the as deprecated marked ValutBinding stuff with a better solution.

For example if you have a method to retrieve a JSF Bean from the faces context:


public static Object accessBeanFromFacesContext(final String theBeanName, final FacesContext theFacesContext) {
final StringBuffer valueBinding = new StringBuffer();
valueBinding.append('#');
valueBinding.append('{');
valueBinding.append(theBeanName);
valueBinding.append('}');
final Object returnObject = theFacesContext.getApplication().
createValueBinding(valueBinding.toString()).getValue(theFacesContext);
if (returnObject == null) {
LOG
.error("Bean with name " + theBeanName + " was not found. Check the faces-config.xml file if the given bean name is ok."); //$NON-NLS-1$ //$NON-NLS-2$
}
return returnObject;
}

You can replace this now with following code:

public static Object accessBeanFromFacesContext(final String theBeanName, final FacesContext theFacesContext) {
final Object returnObject = theFacesContext.getELContext().getELResolver().getValue(theFacesContext.getELContext(), null, theBeanName);
if (returnObject == null) {
LOG
.error("Bean with name " + theBeanName + " was not found. Check the faces-config.xml file if the given bean name is ok."); //$NON-NLS-1$ //$NON-NLS-2$
}
return returnObject;
}

This retrieves a bean from the faces context. You can use the method for example to retrieve a TestBean form the faces context like

TestBean testBean = (TestBean)JSFUtils.accessBeanFromFacesContext("testBean", FacesContext.getCurrentInstance());

The TestBean object have to be added to the faces-config.xml as a managed bean. If the bean was not created at the point where you ask for it, the default constructor is called and the bean is created for you from the JSF framework.

6 Kommentare »

  1. Hello,

    .getValue(theFacesContext.getELContext(), null, theBeanName);

    the BeanName is a String and getValue need to receive an „Object“.

    Are you sure of your code ?

    Thanks in advance.

    Kommentar von Laurent — November 24, 2008 @ 7:13 pm | Antworten

  2. nice 🙂

    Kommentar von rondo — Mai 14, 2009 @ 5:11 am | Antworten

  3. […] Replacing deprecated ValueBindung stuff from JSF with ELResolver September 20072 comments 5 […]

    Pingback von 2010 in review « waffel’s Weblog — Januar 3, 2011 @ 1:04 pm | Antworten

  4. good!!

    Kommentar von promero — August 28, 2011 @ 7:11 am | Antworten

  5. Thanks 😉

    Kommentar von Raúl — Januar 3, 2012 @ 12:20 pm | Antworten

  6. Thanks….the other option is facesContext.getApplication().evaluateExpressionGet(facesContext, „#{“ + beanName + „}“, beanClass);

    Kommentar von Sharan Rajendran — Januar 20, 2012 @ 11:04 pm | Antworten


RSS feed for comments on this post. TrackBack URI

Hinterlasse eine Antwort zu rondo Antwort abbrechen

Bloggen auf WordPress.com.