Writing Variable Resolver using JSF 2
Last Updated on Wednesday, 29 June 2011 13:50
|
You probably know that the API variable-resolver has been deprecated after JSF
1.1. JSF
2 use a more generic class called ELResolver
In the following tutorial we will illustrate the creation of a resolver (ex-varaibleresolver) to access the pom.properties located in you webapp. To have this file packaged in your webapp you need to package you application with Maven.
The pom.properties and pom.xml files are both packaged in the following folder :
|-- META-INF | |-- MANIFEST.MF | |-- application.properties | `-- maven | `-- com.mycompany.app | `-- my-app | |-- pom.properties | `-- pom.xml
The content of your pom.properties should look like :
The objective of this JSF
resolver tutorial is to give a way to access those three properties using the unified expression language.
#{maven.pom['groupId']}
#{maven.pom['artifactId']}
#{maven.pom['version']}
To achieve this objective we will :
- Develop a MavenVariableResolver extending the JSF
2 ELResolver class - Configure our webapp to use this variable resolver
- Write a page using the three expressions
Writing our Maven Variable Resolver
In the below source i'm creating an ELResolver called MavenVariableResolver. The POM_FILE field define the location of the Maven pom.properties file in the Web Application. MAVEN and POM define the variable name we support in the resolver.
RESOLVABLE_DESIGN_TIME is self explaining. The pomMap field will contain the Map containing the properties present in the pom.properties file.
The Maven inner class is the "fake" class returned if you call only the maven properties (eg: #{maven})

The abstract class ELResolver requires the following methods to be implemented :
- public abstract Iterator
getFeatureDescriptors( ELContext context, Object base); - public abstract Class<?> getType(ELContext context, Object base, Object property);
- public abstract Object getValue(ELContext context, Object base, Object property);
- public abstract void setValue(ELContext context, Object base, Object property, Object value);
- public abstract boolean isReadOnly(ELContext context, Object base, Object property);
- public abstract Class<?> getCommonPropertyType(ELContext context, Object base);
In the following chapters i will explain and provide the implementation for each method.
EL Resolver Feature descriptor method implementation
The getFeatureDescriptors method returns information about the set of variables that can be resolved for the given baseobject. The Iterator returned must contain two instances of java.beans.FeatureDescriptor One for the maven keyword and the other for the pom keyword.

Return the corresponding variables types
For a given base and property th getType method attempts to identify the most general type that is acceptable for an object to be passed as the value parameter in a future call to the setValue method. This implementation is mandatory but not very usefull in our case because our properties are read only. Anyway we will provide and implementation

IsReadOnly implementation
For a given base and property,isReadOnly method attempts to determine whether a call to setValue will always fail.
If this resolver handles the given (base, property) pair, the propertyResolved property of the ELContext object must be set to true by the resolver, before returning. If this property is not true after this method is called, the caller should ignore the return value.
In our case both variables should be considered read only.

Return the common property type
Returns the most general type that this resolver accepts for the property argument, given a base object. One use for this method is to assist tools in auto-completion.

Set value implementation
Attempts to set the value of the given property object on the given baseobject. Our implementation should throw a PropertyNotWritableException when someone try to set the maven of pom properties.

Get value implementation
getValue() method attempts to resolve the given property object on the given base object.
If this resolver handles the given (base, property) pair, the propertyResolved property of the ELContext object must be set to true by the resolver, before returning. If this property is not true after this method is called, the caller should ignore the return value.

Loading the properties used by the getValue method
To provide the values requrested by the getValue() method we need to read them from the properties. This is the responsability of the method below.

Registering the ELResolver in JSF
configuration
To make JSF
aware of our ELREsolver we need now to configure the JSF
faces-config.xml file.
Include the following snippet in the <build><plugins> section of your project's pom.xml-file:
![]()
Create a JSF
page and display the result
![]()
The result should look like :
Maven Project configuration
| maven.pom['groupId'] | com.mycompany.app |
| maven.pom['artifactId'] | my-app |
| maven.pom['version'] | 1.1-SNAPSHOT |
If you need more details about the execution feel free to configure your logging system or use the following tutorial to configuration the JDK logging system in a Tomcat server : JSF 2 Logging in Tomcat
Conclusion
The aim of this article has been to help new users overcome the initial hurdles necessary for creating an ELResolver. If you found in this article usefull informations or want to have more details or discover and error feel free to add your comment below
Complete source available at : Maven Variable Resolver source
Sébastien Dante Ursini
Sébastien Dante Ursini
Java Specialist
15 Years of experience in Java
Based in Geneva/Switzerland





