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 ELContexts

If you are in the situation of writing a custom ELContext, then you need to extend the ELContext and override the abstract methods. But, if you have OmniFaces available in your project, then you can extend the org.omnifaces.el.ELContextWrapper, which provides a simple implementation of ELContext that can be sub-classed by developers wishing to provide specialized behavior to an existing ELContext instance. The default implementation of all methods is to call through to the wrapped ELContext. The source code of ELContextWrapper is:

package org.omnifaces.el;

import java.util.Locale;

import javax.el.ELContext;
import javax.el.ELResolver;
import javax.el.FunctionMapper;
import javax.el.VariableMapper;
import javax.faces.FacesWrapper;
import javax.faces.application.ViewHandler;

public class ELContextWrapper extends ELContext implements FacesWrapper<ELContext> {

 private ELContext elContext;

 public ELContextWrapper() {}

 public ELContextWrapper(ELContext elContext) {
  this.elContext = elContext;
 }

 @Override
 public ELContext getWrapped() {
  return elContext;
 }

 @Override
 public void setPropertyResolved(boolean resolved) {
  getWrapped().setPropertyResolved(resolved);
 }

 @Override
 public boolean isPropertyResolved() {
  return getWrapped().isPropertyResolved();
 }

 @Override
 public void putContext(@SuppressWarnings("rawtypes") Class key, Object contextObject) {
  getWrapped().putContext(key, contextObject);
}

 @Override
 public Object getContext(@SuppressWarnings("rawtypes") Class key) {
  return getWrapped().getContext(key);
 }

 @Override
 public ELResolver getELResolver() {
  return getWrapped().getELResolver();
 }

 @Override
 public FunctionMapper getFunctionMapper() {
  return getWrapped().getFunctionMapper();
 }

 @Override
 public Locale getLocale() {
  return getWrapped().getLocale();
 }

 @Override
 public void setLocale(Locale locale) {
  getWrapped().setLocale(locale);
 }

 @Override
 public VariableMapper getVariableMapper() {
  return getWrapped().getVariableMapper();
 }
}

Now, you can write a custom ELContext by following the next two skeletons:

·         extend ELContextWrapper and override #getWrapped() to return the instance wrapped by ELContextWrapper

import javax.el.ELContext;
import org.omnifaces.el.ELContextWrapper;

public class MyELContext extends ELContextWrapper {

 // override the desired ELContext methods
   
 //extend ELContextWrapper and override #getWrapped() to return the instance wrapped by ELContextWrapper
 @Override
 public ELContext getWrapped() {
  return super.getWrapped();
 }
}

·         provide ELContext instance to the overloaded constructor

import javax.el.ELContext;
import org.omnifaces.el.ELContextWrapper;

public class MyELContext extends ELContextWrapper {

 public MyELContext(ELContext elContext) {
  super(elContext);
 }   
   
 // override the desired ELContext methods 
}

Further, you can check the OmniFaces custom ELContexts:


These provides valuable lessons about how to correctly write a custom ELContext.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors