EL
flexibility can be tested by extending it with custom implicit variables, properties,
and method calls. This is possible if we extend the VariableResolver or PropertyResolver class, or
even better, the ELResolver class that give us flexibility to
reuse the same implementation for different tasks. The following are three
simple steps to add custom implicit variables:
1. Create
your own class that extends the ELResolver class.
2.
Implement the inherited abstract methods.
3. Add the
ELResolver class in faces-config.xml.
But, if
you have OmniFaces available in your project, then you can extend the org.omnifaces.el.ELResolverWrapper, which provides
a simple implementation of ELResolver that can be sub-classed by developers
wishing to provide specialized behavior to an existing ELResolver instance.
The default implementation of all methods is to call through to the wrapped ELResolver. The
source code of ELResolverWrapper is:
package
org.omnifaces.el;
import
java.beans.FeatureDescriptor;
import
java.util.Iterator;
import
javax.el.ELContext;
import
javax.el.ELResolver;
import
javax.faces.FacesWrapper;
public class
ELResolverWrapper extends ELResolver implements FacesWrapper<ELResolver>
{
private ELResolver elResolver;
public ELResolverWrapper() {}
public ELResolverWrapper(ELResolver
elResolver) {
this.elResolver = elResolver;
}
@Override
public ELResolver getWrapped() {
return elResolver;
}
@Override
public Object getValue(ELContext context,
Object base, Object property) {
return getWrapped().getValue(context, base,
property);
}
@Override
public Object invoke(ELContext context, Object
base, Object method, Class<?>[] paramTypes, Object[] params) {
return getWrapped().invoke(context, base,
method, paramTypes, params);
}
@Override
public Class<?> getType(ELContext
context, Object base, Object property) {
return getWrapped().getType(context, base,
property);
}
@Override
public void setValue(ELContext context, Object
base, Object property, Object value) {
getWrapped().setValue(context, base,
property, value);
}
@Override
public boolean isReadOnly(ELContext context,
Object base, Object property) {
return getWrapped().isReadOnly(context, base,
property);
}
@Override
public Iterator<FeatureDescriptor>
getFeatureDescriptors(ELContext context, Object base) {
return
getWrapped().getFeatureDescriptors(context, base);
}
@Override
public Class<?>
getCommonPropertyType(ELContext context, Object base) {
return
getWrapped().getCommonPropertyType(context, base);
}
}
Now, you
can write a custom ELResolver by following the next two skeletons:
·
extend ELResolverWrapper and override #getWrapped() to return the instance wrapped by ELResolverWrapper
import
javax.el.ELResolver;
import
org.omnifaces.el.ELResolverWrapper;
public class
MyELResolver extends ELResolverWrapper {
// override the desired ELResolver methods
//extend ELResolverWrapper and override
#getWrapped() to return the instance wrapped by ELResolverWrapper
@Override
public ELResolver getWrapped() {
return super.getWrapped();
}
}
·
provide ELResolver instance
to the overloaded constructor
import
javax.el.ELResolver;
import
org.omnifaces.el.ELResolverWrapper;
public class
MyELResolver extends ELResolverWrapper {
public MyELResolver(ELResolver elResolver) {
super(elResolver);
}
// override the desired ELResolver
methods
}
Further,
you can check the OmniFaces custom ELResolvers:
These
provides valuable lessons about how to correctly write a custom ELResolver.
Niciun comentariu :
Trimiteți un comentariu