This site talks about the many challenges on computer programming.

In the last week I had several issues during the development of a jsf application that could be solved by creating few custom Facelets EL functions, let’s take a look on which scenarios Facelets EL functions helped me.

1. Simple loop over a range of numbers

JSF has a lot of components that can be used to iterate over a model and repeat its own child components several times, some of them are Tomahawk’s dataList and Richfaces repeat.

But in all of these components I couldn’t find a way to do a simple iteration on a range of numbers, like repeat all child components starting from 1 and stopping at n number of times.

Maybe you will say: “What about c:forEach? It can do this kind of iteration!”, oh yes, it can, and I tried to use it but it crashes when I pass EL expressions to both begin and end attributes, it only accepts hardcoded values.
In order to solve my issue, I created a custom Facelets EL function, this EL function takes a integer as argument that will be used to specify the size of a very simple List that will used on a Tomahawk’s dataList tag.

In the example above I realy need repeat the child components only n times and to get it a list with a limited size was created.

2. Converting a value passed to a f:param.

The are some circunstances where a h:outputFormat can help a lot on some ui data translation (e.g. returning a week day name from a number), but it need the number of week day to be passed as param, and for this I’m using a f:param.

But what if this param isn’t a integer? We need convert this data, right? I tried to figure out how can I do it by attaching a converter to a f:param, but I couldn’t find nothing about it anywhere.

How do I solved it? Simple, I just wrote another EL function that can takes any Object as an argument and then I returned a integer value of this object.

The resource string above is expecting a parameter, the appropriate week day name will be picked based on day number passed by param tag attached to outputFormat tag.

As we can see, I’m iterating over a array of strings, each element is a string and when I passed this string to a outputFormat component I got a conversion error, with this EL function I could solve my problem very fast.

Here we have two examples of how we can use EL function to solve some annoying problems, there’s a lot of jsf issues that can be solved in this way too!

Hi,
I am talking about \’Simple loop over a range of numbers\’. I am using richfaces, a4j along with core jsf.
The exception is something like this :
java.lang.NullPointerException at org.apache.el.lang.FunctionMapperImpl$Function.writeExternal(FunctionMapperImpl.java:123) at java.io.ObjectOutputStream.writeExternalData(ObjectOutputStream.java:1310) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1288) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302) at java.util.HashMap.writeObject(HashMap.java:1039) at sun.reflect.GeneratedMethodAccessor34.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:917) at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1339) at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1290) at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1079) at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:302) at org.apache.el.lang.FunctionMapperImpl.writeExternal(FunctionMapperImpl.java:74) at java.io.ObjectOutputStream.writeExternalData(ObjectOutputStream.java:1310)…….

Currently you have JavaScript disabled. In order to post comments, please make sure JavaScript and Cookies are enabled, and reload the page.Click here for instructions on how to enable JavaScript in your browser.