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}"/>