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


vineri, 24 aprilie 2015

Expose constants and public static methods in EL via OmniFaces

Let's suppose that we have an enum an a helper class, as below:


package my.artifacts;

public enum PlayerEnum {
 public String getFriendlyName(){
  String firstName = name().substring(0, name().indexOf("_"));
  String secondName = name().substring(name().indexOf("_")+1);
  return firstName.charAt(0) + firstName.substring(1).toLowerCase() + " " + secondName.charAt(0) + secondName.substring(1).toLowerCase();


package my.artifacts;

import java.util.Random;

public final class PlayerHelper {

 public static String addPlayerRank(String name) {
  Random rnd = new Random();
  if (rnd.nextInt(10) < 5) {
      return "Successfully added, " + name + "!";
  return "Ooops! That player " + name + " exist in ATP ranking!";

Now, in the JSF page, we want to invoke the PlayerHelper#addPlayerRank() method with the argument, PlayerEnum#ANDY_MURRAY#getFriendlyName() - this could be also a constant of type public static final ... . The problem is that we cannot access constants  or such helper classes via EL. Of course, we can wrap the enum in a bean property and the helper class methods in custom Facelets taglib functions, but it will be much simpler to use OmniFaces <o:inportConstants> and <o:inportFunctions>, which allows us to do this:

<o:importConstants type="my.artifacts.PlayerEnum"/>
<o:importFunctions type="my.artifacts.PlayerHelper"/>

<h5>Insert Andy Murray (PlayerEnum.ANDY_MURRAY) in ATP</h5><br/>       

or, using a var attribute:

<o:importConstants type="my.artifacts.PlayerEnum" var="pe"/>
<o:importFunctions type="my.artifacts.PlayerHelper" var="ph"/>
<h5>Insert Andy Murray (PlayerEnum.ANDY_MURRAY) in ATP</h5><br/>       

Complete source code in GitHub.
More examples in OmniFaces Showcase.

Niciun comentariu :

Trimiteți un comentariu


Postări populare

OmniFaces/JSF Fans

Follow by Email

Visitors Starting 4 September 2015

Locations of Site Visitors