- 

Available since OmniFaces 1.4

The <o:importFunctions> taghandler allows the developer to have access to all functions of the given fully qualified name of a type in the Facelet scope using the usual EL functions syntax without the need to register them in .taglib.xml file. The functions are those public static methods with a non-void return type.

Usage

For example:

<o:importFunctions type="java.lang.Math" var="m" />
...
#{m:abs(-10)}
#{m:max(bean.number1, bean.number2)}

The functions prefix becomes by default the simple name of the type. You can override this by explicitly specifying the var attribute.

The resolved functions are by reference stored in the cache to improve retrieving performance.

Precaution as to multiple functions with exactly the same method name

EL functions does not support method overloading. It's therefore not possible to provide overloaded methods like Math.abs(int), Math.abs(long), Math.abs(float) and Math.abs(double) in four separate EL functions.

If there are multiple function methods discovered with exactly the same name, then the one with the least amount of parameters will be used. If there are multiple function methods with exactly the same name and amount of parameters, then the choice is unspecified (technically, JVM-dependent, the first one in the methods array as found by reflection would be picked up) and should not be relied upon. So if you absolutely need to differentiate functions in such case, give them each a different name.

Since version 4.3, you can use the loader attribute to specify an object whose class loader will be used to load the class specified in the type attribute. The class loader of the given object is resolved as specified in Utils.getClassLoader(Object). In the end this should allow you to use a more specific class when there are duplicate instances in the runtime classpath, e.g. via multiple (plugin) libraries.

Design notes

Note that the colon : operator to invoke the method is as required by EL functions spec. It's by design not easily possible to change it to the period . operator. Also note that in case of org.omnifaces.util.Faces it's considered poor practice if the same functionality is already available through the implicit EL objects #{faces}, #{facesContext}, #{view}, #{request}, etc such as #{faces.development} or #{request.contextPath} which should be preferred over #{Faces:isDevelopment()} or #{Faces:getRequestContextPath()}.

Demo

The functions of java.lang.Math

  • abs(-10): 10
  • min(-10, 20): -10
  • max(-10, 20): 20
  • floor(12.34): 12.0
  • ceil(12.34): 13.0
  • random(): 0.6911249241480166
Demo source code
<o:importFunctions type="java.lang.Math" var="m" />

<h3>The functions of <code>java.lang.Math</code></h3>
<ul>
    <li><code>abs(-10)</code>: #{m:abs(-10)}</li>
    <li><code>min(-10, 20)</code>: #{m:min(-10, 20)}</li>
    <li><code>max(-10, 20)</code>: #{m:max(-10, 20)}</li>
    <li><code>floor(12.34)</code>: #{m:floor(12.34)}</li>
    <li><code>ceil(12.34)</code>: #{m:ceil(12.34)}</li>
    <li><code>random()</code>: #{m:random()}</li>
</ul>
Documentation & Sources