-------------- Original message ----------------------
From: "Jacob Mathew" <[EMAIL PROTECTED]>
> I'm looking for documentation on implementing functions that can be
> used in EL in jsf pages. Maybe I'm missing something, but the best
> info I can find is that the a FunctionMapper object is used to resolve
> functions that show up in EL. But how can I define my own
> FunctionMapper? how do I set it? Can the default FunctionMapper be
> configured through an config file? I can't seem to find any info on
> this. Any reference will be much appreciated.
>

Using the EL functions in JSP is simpler than trying to use them in late bound 
EL in JSF.  First you need to create a static function and then register it in 
a TLD [3].  Next, add the namespace to the page [4].

For example:
        <c:set value="${tfun:formatDate(tfun:currentDate(), 'MM/dd/yyyy')}"



Invoking custom functions using JSF EL is more difficult.  You can create your 
own function mapper [1] and then use the ELContextWrapper [2] to register the 
function mapper [1].

Using it would look something like this:


    FacesContext context = FacesContext.getCurrentInstance();

    // load function mapper with static methods from the TagFunctions
    Method[] functions = TagFunctions.class.getMethods();
    FunctionMapperImpl fm = new FunctionMapperImpl();
    for (int i = 0; i < functions.length; i++)
    {
      if (Modifier.isStatic(functions[i].getModifiers()) && 
          !Modifier.isNative(functions[i].getModifiers()))
      {
        String name = functions[i].getName();
        fm.mapFunction("tfun", name, functions[i]);
      }
    }

    //create an el context that implements a custom FunctionMapper; JSF 1.2
    //RI implements a no-op function mapper

    ELContextWrapper elContext = 
      new ELContextWrapper(context.getELContext(), fm);

    // grap a reference to the EL factory
    ExpressionFactory ef = context.getApplication().getExpressionFactory();

    //create a value expression using the custom EL context wrapper
    ValueExpression vs = 
      ef.createValueExpression(elContext, 
"#{tfun:formatDate(tfun:currentDate(), 'MM/dd/yyyy')}", 
                               String.class);

    // return the value of the custom method el
    return (String) vs.getValue(elContext);
  }

[1] 
https://svn.apache.org/viewvc/shale/framework/trunk/shale-test/src/main/java/org/apache/shale/test/el/MockFunctionMapper.java?view=markup
[2] 
https://svn.apache.org/viewvc/tomcat/jasper/tc6.0.x/src/share/org/apache/jasper/el/ELContextWrapper.java?view=markup
[3] 
  <function>
    <description>
       Returns a formated date using the specified pattern.
    </description>
    <name>formatDate</name>
    <function-class>demo.tfun.TagFunctions</function-class>
    <function-signature>java.lang.String formatDate(java.util.Date,
                        java.lang.String)</function-signature>
    <example>
       tfun:formatDate( date , "MM/dd/yyyy")
    </example>
  </function>
[4] 
<%@ taglib uri="http://demo.tfun"; prefix="tfun"%>




 
> -Jacob

Reply via email to