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

sâmbătă, 4 iulie 2015

OmniFaces 2.1 - ReadOnlyValueExpression that can be set when the "real" value is not yet available

This post presents the features brought by OmniFaces 2.1 for the ReadOnlyValueExpression artifact. If you are not familiar with ReadOnlyValueExpression then please read this post before, "OmniFaces2.0 - ReadOnlyValueExpression that can be set when the "real"value is not yet available".

In OmniFaces 2.0 we can create a ReadOnlyValueExpression via a callback which takes one argument and returns one value (the object is the "real" needed value of this ValueExpression). Starting with OmniFaces 2.1, the Callback.Returning was replaced with the serializable version of it, Callback.SerializableReturning. By the other hand, the Callback.ReturningWithArgument is still there.

So, instead of writing:

ReadOnlyValueExpression readOnlyValueExpression = new 
     ReadOnlyValueExpression(type_of_object, new Returning<Object>() {
 @Override
 public Object invoke() {
  return object;
 }
});

Now, we write:

ReadOnlyValueExpression readOnlyValueExpression = new 
     ReadOnlyValueExpression(type_of_object, new SerializableReturning<Object>() {

 private static final long serialVersionUID = 1L;

 @Override
 public Object invoke() {
  return object;
 }
});

Moreover, note that in OmniFaces 2.0, the Callback can be "attached" only via ReadOnlyValueExpression constructors, while in OmniFaces 2.1 it can be "attached" via constructors, or later, via setCallbackReturning() and setCallbackWithArgument() methods. This adds more flexibility, since we are not restricted to "attach" the Callback when we instantiate the ReadOnlyValueExpression, and we can obtain the functional Callback interface that will be called when the value expression is resolved, via getCallbackReturning() and getCallbackWithArgument(). Below you can see these methods:

·         OmniFaces 2.1 set/getCallbackReturning():

·         OmniFaces 2.1 set/getCallbackWithArgument():


So, in OmniFaces 2.1 we can write the functional Callback interface, as below:

public static class MyCallbakImpl implements SerializableReturning<Object> {

 private static final long serialVersionUID = 1L;

 public MyCallbakImpl () { // empty constructor, not mandatory!
  // NOOP
 }

 // more constructors
 ...

 @Override
 public Object invoke() {
  ...
 }
}

And, whenever we need, we can create the ReadOnlyValueExpression without "attaching" the above functional now, MyCallbakImpl:

// this is just an example, you don't have to pass the UIComponent type, rather your expected type
ReadOnlyValueExpression readOnlyValueExpression = new ReadOnlyValueExpression(UIComponent.class);

Later, at the proper moment, we use the OmniFaces 2.1 setCallbackReturning() to "attach" the readOnlyValueExpression to MyCallbakImpl:

readOnlyValueExpression.setCallbackReturning(new MyCallbakImpl());

Is pretty obvious how to use the getCallbackReturning().

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors