My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

duminică, 19 iulie 2015

OmniFaces approach for writing custom ELResolvers

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors