Showing posts with label JSTL. Show all posts
Showing posts with label JSTL. Show all posts

Wednesday, March 21, 2007

JSTL and JSP Expression Language used in JSP 2.0

The release of the JSP 2.0 and servlet 2.4 specification has changed the way expressions are used within JSP pages.
From now on you can use expressions throughout the whole JSP page.
There appeared to be some limitations though, when mixing expressions with tags, you can only use expressions within tag attribute values.
So name is ${your.name}. is perfectly legal, as well as href="${link.url}" />But ${attrName} is not.

When trying to figure this out, I stumbled over several issues before I got the desired output. First you must declare your application as a 2.4 webapp using the following declaration:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd"
version="2.4" />


When using the JSTL taglibrary, you must be aware that the taglib url is different, jsp is added prior to jstl within the URL.

For 2.3 webapps you should use taglib <%@taglib uri="http://java.sun.com/jstl/core" prefix="c"%> while for 2.4 webapps you use <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> />

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:

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

1JohnDoe
2JaneDoe
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}"/>