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

2 comments:

patman said...

Hi Marcel,

The root element should be web-app, not webapp ;-)

SpiderMars said...

Thanks patman, I'll correct it!