26 Jun 2008 waffel   » (Journeyer)

Replacing deprecated ValueBindung stuff from JSF with ELResolver


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.

Syndicated 2007-09-27 09:21:22 from waffel's Weblog

Latest blog entries     Older blog entries

New Advogato Features

New HTML Parser: The long-awaited libxml2 based HTML parser code is live. It needs further work but already handles most markup better than the original parser.

Keep up with the latest Advogato features by reading the Advogato status blog.

If you're a C programmer with some spare time, take a look at the mod_virgule project page and help us with one of the tasks on the ToDo list!