waffel’s Weblog

September 27, 2007

Replacing deprecated ValueBindung stuff from JSF with ELResolver

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

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.

About these ads

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 nachmittags | Antwort

  2. nice :)

    Kommentar von rondo — Mai 14, 2009 @ 5:11 vormittags | Antwort

  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 nachmittags | Antwort

  4. good!!

    Kommentar von promero — August 28, 2011 @ 7:11 vormittags | Antwort

  5. Thanks ;)

    Kommentar von Raúl — Januar 3, 2012 @ 12:20 nachmittags | Antwort

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

    Kommentar von Sharan Rajendran — Januar 20, 2012 @ 11:04 nachmittags | Antwort


RSS-Feed für Kommentare zu diesem Beitrag. TrackBack URI

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ photo

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s

The Rubric Theme. Erstelle eine kostenlose Website oder einen kostenlosen Blog – auf WordPress.com!.

Folgen

Erhalte jeden neuen Beitrag in deinen Posteingang.

Schließe dich 28 Followern an

%d Bloggern gefällt das: