duminică, 6 septembrie 2015

[OmniFaces utilities 2.0] Concatenate the string representation of the given objects


[OmniFaces utilities] The concat() concatenate the string representation of the given objects.

Function:
Usage:

In order to highlight the OmniFaces of:concat() function let's suppose that we have the following two objects (Foo and Buzz):

public class Foo implements Serializable {

 private String foofat;
 private String fooweight;

 public Foo(String foofat, String fooweight) {
  this.foofat = foofat;
  this.fooweight = fooweight;
 }

 public String getFoofat() {
  return foofat;
 }

 public void setFoofat(String foofat) {
  this.foofat = foofat;
 }

 public String getFooweight() {
  return fooweight;
 }

 public void setFooweight(String fooweight) {
  this.fooweight = fooweight;
 }

 @Override
 public String toString() {
  return "Foo is " + foofat + ", and his weight is " + fooweight + "! ";
 }   
}

public class Buzz implements Serializable {

 private String buzzfat;
 private String buzzweight;

 public Buzz(String buzzfat, String buzzweight) {
  this.buzzfat = buzzfat;
  this.buzzweight = buzzweight;
 }

 public String getBuzzfat() {
  return buzzfat;
 }

 public void setBuzzfat(String buzzfat) {
  this.buzzfat = buzzfat;
 }

 public String getBuzzweight() {
  return buzzweight;
 }

 public void setBuzzweight(String buzzweight) {
  this.buzzweight = buzzweight;
 }

 @Override
 public String toString() {
  return "Buzz is " + buzzfat + ", and his weight is " + buzzweight+ "! ";
 }
}

Now, let's test the EL 2.2 concat() function, += EL 3.0 concatenation operator and the OmniFaces of:concat() function for concatenating Foo and Buzz objects string representations:

<c:set var="foobuzz" value="#{(testBean.fooman.toString()).concat(testBean.buzzman.toString())}"/>

<c:set var="foobuzz" value="#{testBean.fooman += testBean.buzzman}"/>

<c:set var="foobuzz" value="#{of:concat(testBean.fooman, testBean.buzzman)}"/>

·         both objects are declared non-null in a managed bean named, TestBean

Foo fooman = new Foo("well-fed", "105kg");       
Buzz buzzman = new Buzz("corpulent", "92kg");

#{foobuzz} - will render in all three cases:

Foo is well-fed, and his weight is 105kg! Buzz is corpulent, and his weight is 92kg!

·         Foo is non-null, Buzz is null

Foo fooman = new Foo("well-fed", "105kg");       
Buzz buzzman = null;

#{foobuzz} - In case of += EL 3.0 operator we will get a java.lang.NullPointerException.
The EL 2.2 concat() function and the OmniFaces of:concat() function will render:

Foo is well-fed, and his weight is 105kg!

Further, let's try to concatenate the properties of the same object (e.g. Foo):

<c:set var="foo" value="#{testBean.fooman.foofat.concat(testBean.fooman.fooweight)}"/>

<c:set var="foo" value="#{testBean.fooman.foofat += testBean.fooman.fooweight}"/>

<c:set var="foo" value="#{of:concat(testBean.fooman.foofat, testBean.fooman.fooweight)}"/>

·         Foo is non-null, but it contains a null property

Foo fooman = new Foo("well-fed", null);       

#{foo} - In case of += EL 3.0 operator we will get a java.lang.NullPointerException.
The EL 2.2 concat() function and the OmniFaces of:concat() function will render:

well-fed

Niciun comentariu:

Trimiteți un comentariu