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

marți, 15 septembrie 2015

[OmniFaces utilities 2.0] Get the first non-null object from the provided two objects


[OmniFaces utilities] The coalesce() function returns the first non-null object from the provided two objects. So, if the first object is not null, then it will be returned, otherwise the second object will be returned.

Function:
Usage:

Let's suppose that we have the following managed bean:

@Named
@RequestScoped
public class MyBean {

 private String first;  // null
 private String second; // not-null

 public MyBean() {
  second = "This is the second String";
 }
   
 // getters and setters
}

So, first string is null and second string is not-null. Further, we want to return the not-null string between these two (e.g. second), and in pure EL we can do this:

#{myBean.first != null ? myBean.first : myBean.second}
#{myBean.first ne null ? myBean.first : myBean.second}

Another approach consist in using the EL empty operator, which checks null and empty values (if a string is null or is empty (e.g. "") then the empty operator return true). The empty operator is a prefix operator that can be used to determine if a value is null or empty.

To evaluate empty A

  • If A is null, return true
  • Otherwise, if A is the empty string, then return true
  • Otherwise, if A is an empty array, then return true
  • Otherwise, if A is an empty Map, return true
  • Otherwise, if A is an empty Collection, return true
  • Otherwise return false

#{not empty myBean.first ? myBean.first : myBean.second}

The above examples will return: This is the second String

OmniFaces comes with an alternative for checking null objects, named of:coalesce() utility function. This can be used as below:

#{of:coalesce(myBean.first, myBean.second)}
#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

If both object are not-null then of:coalesce() returns the value of its first argument. For example, let's modify our bean as below:

@Named
@RequestScoped
public class MyBean {

 private String first;   // not-null
 private String second;  // not-null

 public MyBean() {
  first = "This is the first String";
  second = "This is the second String";
 }
   
 // getters and setters
}

Now, let's perform the same checks as above again:

#{of:coalesce(myBean.first, myBean.second)}
Output: This is the first String

#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

Note The of:coalesce() utility function checks ONLY null values, not empty values like the EL empty operator, so it is not an alternative to this operator!

Notice that of:coalesce() (and "native" solutions also) are affected by the value of the INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL context parameter. There is no need to explain what this flag does since its name is more than suggestive. But, is important to know that this is set to false by default.

For example, let's suppose that we provide the first string via an input text as below (the second string remains, This is the second String):

<h:form>
 First: <h:inputText value="#{myBean.first}"/>  
 <h:commandButton value="Submit"/>
</h:form>

·         submit, This is the first String:

#{of:coalesce(myBean.first, myBean.second)}
Output: This is the first String

#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

·         submit an empty string:

#{of:coalesce(myBean.first, myBean.second)}
Output:

#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

Now, let's set INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL to true and perform the same tests:

<context-param>
 <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
 <param-value>true</param-value>
</context-param>

·         submit, This is the first String:

#{of:coalesce(myBean.first, myBean.second)}
Output: This is the first String

#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

·         submit an empty string:

#{of:coalesce(myBean.first, myBean.second)}
Output: This is the second String  // effect of INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL

#{of:coalesce(myBean.second, myBean.first)}
Output: This is the second String

Note As a note, just as in EL, if both values passed are nullof:coalesce() won’t display anything. Also, this function doesn’t check for empty strings, so you need to be aware if it fits or not in a specific case.

Niciun comentariu :

Trimiteți un comentariu

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors