Tuesday, July 20, 2004

Checking for null or empty string values

Flow control can be accomplished using the <c:if>
Its test attribute can contain an EL expression.
To test if a value is null you can compare the attribute to null like:
    <c:if test="${valueToTest eq null}">
If the value exists but is empty you can test it using:
    <c:if test="${empty valueToTest}">
I assumed that the empty operator was a binairy operator but after thourough testing it appeared to be a unairy operator.

Tuesday, June 01, 2004

Map iterations using c:forEach

Using the c:forEach custom tag, it is easy to iterate over collections including map's.
Altough you have to keep in mind that the resulting value is a MapItem.

The MapItem has 2 properties named key and value.
These properties can be treated as javabeans theirselves, hence its properties being retrieved according to the javabean specification.
Hereunder follows a sample that iterates over a map, the key is of type String and the value of a type that complies to the javabean specification:

<c:forEach var="item" items="myMap">
<c:out value="${item.key}"/>
<c:out value="${item.value.givenName}"/>
<c:out value="${item.value.surName}"/>
The output of the above sample is:

3Johnny jr.Doe

Friday, May 14, 2004

Using custom objects in JSTL's expression language

If you are going to use JSTL's expression language, take care that the attributes you want to use needs to be within scope.
Usually the necessary attributes wil be available on the request, session or pagecontext. But sometimes you need to use a variable, or constant, that is NOT available yet. In that case you have to add it to the opagecontext yourself.

This can be achieved using a scriptlet:

pageContext.setAttribute("myVar", MyClass.myConstant);

But you can also achieve using the jstl tag c:set:

<c:set var="myVar"><%=MyClass.myConstant%></c:set>

Now it can be used in EL expressions within jstl tags:
<c:out value="${myVar.name}"/>