You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.
| Online: | 15 Members of 4063 |
In the WebContent/META-INF/ (JBDS) or view/META-INF/ (seam-gen) create a file called elfunctions.taglib.xml. If the META-INF/ dir does not exist, create it also. The content of elfunctions.taglib.xml should be:
<?xml version="1.0"?> <!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN" "facelet-taglib_1_0.dtd"> <facelet-taglib> <library-class>org.el.func.FnLibrary</library-class> </facelet-taglib>
Create another src package src/facelets with the packagename: org.el.func and add
package org.el.func;
import com.sun.facelets.tag.AbstractTagLibrary;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
public class FnLibrary extends AbstractTagLibrary {
public final static String Namespace = "http://org.el.func/SeamFunc";
public static final FnLibrary INSTANCE = new FnLibrary();
public FnLibrary() {
super(Namespace);
try {
Method[] methods = SeamFunc.class.getMethods();
for (int i = 0; i < methods.length; i++) {
if (Modifier.isStatic(methods[i].getModifiers())) {
this.addFunction(methods[i].getName(), methods[i]);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
The above will dynamically add any of the static methods in a class called SeamFunc.java to be available in the EL. Now create org.el.func.SeamFunc.java:
package org.el.func;
import org.jboss.seam.core.Expressions;
public class SeamFunc {
public static String concat(String... strings) {
StringBuilder buff = new StringBuilder();
if (strings != null) {
for (String str : strings) {
buff.append(str);
}
}
return buff.toString();
}
public static Object evalEl(String expression) {
String framedExpr = "#{" + expression + "}";
Object value = Expressions.instance().createValueExpression(framedExpr).getValue();
return value;
}
}
Now the last piece is to define the location of the elfunctions.taglib.xml in web.xml:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>
/META-INF/elfunctions.taglib.xml
</param-value>
</context-param>
Create a test.xhtml page and add:
xmlns:e="http://org.el.func/SeamFunc"
and to the body:
#{e:concat('Hello ', 'World!')} / #{e:evalEl('conversation')}
These are just two examples of functions, you can create as many as you'd like. The evalEL seems useless, until you realize you can do something like:
#{e:evalEL(e:concat('person', 'Home'))}
which correctly resolves the value of the Seam component named personHome.
I receive this error in JBoss upon hitting the login page of our application (Seam 2.0GA, RichFaces 3.1.4). I have followed these instructions exactly but removed the import of the class.
Caused by: java.lang.NoClassDefFoundError: com/sun/facelets/tag/AbstractTagLibrary at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:620) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)Has anyone experienced this problem?
ran into same problem, for me it was class loader issue as the custom EL was part of internal reusable library that is loaded as part of ear. Moving to resolved class loader issue..
Thanks
This post may be very obvious to many but to some perhaps not.
You do not have to follow the package conventions in the post. You may use whatever package names and library names you feel like.
In addition, you can add multiple classes that contain static helper functions that may exists in your code.
Here is something very similar to what I ended up doing:
/** * */ package com.Foo.facelets; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import com.sun.facelets.tag.AbstractTagLibrary; /** * Foo Custom Facelets Tag Library * * Currently it supports the StringUtil methods available in Foo and FooBar libs * plus additional Foo Enterprise methods and SEAM specific methods * related to EL Expressions * * @see http://www.seamframework.org/Documentation/CreatingCustomELFunctions * */ public class FooFaceletsLibray extends AbstractTagLibrary { public final static String Namespace = "http://Foo.com/jsf/facelets"; public static final FooFaceletsLibray Instance = new FooFaceletsLibray(); public FooFaceletsLibray() { super(Namespace); try { addStaticMethods(SeamFunctions.class); addStaticMethods(FooFunctions.class); addStaticMethods(com.Foo.somePackage.util.StringUtils.class); addStaticMethods(com.Foo.anotherPackage.util.StringUtils.class); } catch (Exception e) { throw new RuntimeException(e); } } protected void addStaticMethods(Class<?> clazz) { Method[] methods = clazz.getMethods(); for (int i = 0; i < methods.length; i++) { if (Modifier.isStatic(methods[i].getModifiers())) { this.addFunction(methods[i].getName(), methods[i]); } } } }