duminică, 1 noiembrie 2015

[OmniFaces utilities (2.0)] Joins all elements of a collection to a single string, separated by the given separator


[OmniFaces utilities] The joinCollection() function joins all elements of the given collection to a single string, separated by the given separator.

Function:
Usage:

Let's suppose that we have a managed bean named PlayersBean that contains the following Set:

@Named
@ViewScoped
public class PlayerBean implements Serializable {

 private Set<String> players = new HashSet<>();

 public PlayerBean() {
  players.add("RAFAEL NADAL");
  players.add("NOVAK DJOKOVIC");
  players.add("STANISLAS WAWRINKA");
  players.add("JUAN MARTIN DEL POTRO");
  players.add("ROGER FEDERER");
 }

 public Set<String> getPlayers() {
  return players;
 }

 public void setPlayers(Set<String> players) {
  this.players = players;
 }
}

Now, in page, we can join this Set items into a String of items delimited by comma, as below:

Join players:
<strong>#{of:joinCollection(playerBean.players, ', ')}</strong>

Outputs:
Join players: RAFAEL NADAL, NOVAK DJOKOVIC, STANISLAS WAWRINKA, ROGER FEDERER, JUAN MARTIN DEL POTRO

Niciun comentariu:

Trimiteți un comentariu